blob: 5f073bf4fb009d034d2b24e384d1179938093cca [file] [log] [blame]
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08001/* USSD Filter Code */
2
3/*
Holger Hans Peter Freythera18b1162011-04-01 17:32:21 +02004 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +08006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080011 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080020 *
21 */
22
23#include <openbsc/bsc_nat.h>
24#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020025#include <openbsc/ipaccess.h>
26#include <openbsc/socket.h>
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080027
Harald Welted36ff762011-03-23 18:26:56 +010028#include <osmocom/gsm/protocol/gsm_08_08.h>
29#include <osmocom/gsm/gsm0480.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010030#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010031#include <osmocom/gsm/tlv.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020032
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +020033#include <osmocom/sccp/sccp.h>
34
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020035#include <osmocom/abis/ipa.h>
36
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020037#include <sys/socket.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020038#include <string.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020039#include <unistd.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020040
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +020041#define USSD_LAC_IE 0
42#define USSD_CI_IE 1
43
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020044static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *);
45
46static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat)
47{
48 struct bsc_nat_ussd_con *con;
49
50 con = talloc_zero(nat, struct bsc_nat_ussd_con);
51 if (!con)
52 return NULL;
53
54 con->nat = nat;
55 return con;
56}
57
58static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con)
59{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020060 if (con->nat->ussd_con == con) {
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +020061 bsc_ussd_close_connections(con->nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020062 con->nat->ussd_con = NULL;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020063 }
64
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020065 close(con->queue.bfd.fd);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020066 osmo_fd_unregister(&con->queue.bfd);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020067 osmo_timer_del(&con->auth_timeout);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +020068 osmo_wqueue_clear(&con->queue);
Jacob Erlbecke8278122014-03-31 13:42:11 +020069
70 msgb_free(con->pending_msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020071 talloc_free(con);
72}
73
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020074static void ussd_pong(struct bsc_nat_ussd_con *conn)
75{
76 struct msgb *msg;
77
78 msg = msgb_alloc_headroom(4096, 128, "pong message");
79 if (!msg) {
80 LOGP(DNAT, LOGL_ERROR, "Failed to allocate pong msg\n");
81 return;
82 }
83
84 msgb_v_put(msg, IPAC_MSGT_PONG);
85 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
86}
87
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020088static int forward_sccp(struct bsc_nat *nat, struct msgb *msg)
89{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +020090 struct nat_sccp_connection *con;
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020091 struct bsc_nat_parsed *parsed;
92
93
94 parsed = bsc_nat_parse(msg);
95 if (!parsed) {
96 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n");
97 msgb_free(msg);
98 return -1;
99 }
100
101 if (!parsed->dest_local_ref) {
102 LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n");
103 msgb_free(msg);
104 return -1;
105 }
106
107 con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref);
108 if (!con || !con->bsc) {
109 LOGP(DNAT, LOGL_ERROR, "No active connection found.\n");
110 msgb_free(msg);
111 return -1;
112 }
113
114 talloc_free(parsed);
115 bsc_write_msg(&con->bsc->write_queue, msg);
116 return 0;
117}
118
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200119static int ussd_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200120{
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200121 struct bsc_nat_ussd_con *conn = bfd->data;
Jacob Erlbecke8278122014-03-31 13:42:11 +0200122 struct msgb *msg = NULL;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200123 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200124 int ret;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200125
Jacob Erlbecke8278122014-03-31 13:42:11 +0200126 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &conn->pending_msg);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200127 if (ret <= 0) {
Jacob Erlbecke8278122014-03-31 13:42:11 +0200128 if (ret == -EAGAIN)
129 return 0;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200130 LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n");
131 bsc_nat_ussd_destroy(conn);
132 return -1;
133 }
134
135 LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200136 osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200137 hh = (struct ipaccess_head *) msg->data;
138
139 if (hh->proto == IPAC_PROTO_IPACCESS) {
140 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
141 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200142 int ret;
143 ret = ipaccess_idtag_parse(&tvp,
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200144 (unsigned char *) msg->l2h + 2,
145 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200146 if (ret < 0) {
147 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
148 "message with malformed TLVs\n");
Holger Hans Peter Freyther27876a22013-08-13 14:48:44 +0200149 msgb_free(msg);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200150 return ret;
151 }
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200152 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
153 ussd_auth_con(&tvp, conn);
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200154 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
155 LOGP(DNAT, LOGL_DEBUG, "Got USSD ping request.\n");
156 ussd_pong(conn);
157 } else {
158 LOGP(DNAT, LOGL_NOTICE, "Got unknown IPACCESS message 0x%02x.\n", msg->l2h[0]);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200159 }
160
161 msgb_free(msg);
162 } else if (hh->proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200163 forward_sccp(conn->nat, msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200164 } else {
165 msgb_free(msg);
166 }
167
168 return 0;
169}
170
171static void ussd_auth_cb(void *_data)
172{
173 LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n");
174 bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data);
175}
176
177static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn)
178{
179 const char *token;
180 int len;
181 if (!conn->nat->ussd_token) {
182 LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n");
183 bsc_nat_ussd_destroy(conn);
184 return;
185 }
186
187 token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
188 len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200189
190 /* last byte should be a NULL */
191 if (strlen(conn->nat->ussd_token) != len - 1)
192 goto disconnect;
193 /* compare everything including the null byte */
194 if (memcmp(conn->nat->ussd_token, token, len) != 0)
195 goto disconnect;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200196
197 /* it is authenticated now */
198 if (conn->nat->ussd_con && conn->nat->ussd_con != conn)
199 bsc_nat_ussd_destroy(conn->nat->ussd_con);
200
201 LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n");
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200202 osmo_timer_del(&conn->auth_timeout);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200203 conn->authorized = 1;
204 conn->nat->ussd_con = conn;
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200205 return;
206
207disconnect:
208 LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n",
209 conn->queue.bfd.fd);
210 bsc_nat_ussd_destroy(conn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200211}
212
213static void ussd_start_auth(struct bsc_nat_ussd_con *conn)
214{
215 struct msgb *msg;
216
217 conn->auth_timeout.data = conn;
218 conn->auth_timeout.cb = ussd_auth_cb;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200219 osmo_timer_schedule(&conn->auth_timeout, conn->nat->auth_timeout, 0);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200220
221 msg = msgb_alloc_headroom(4096, 128, "auth message");
222 if (!msg) {
223 LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n");
224 return;
225 }
226
227 msgb_v_put(msg, IPAC_MSGT_ID_GET);
228 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
229}
230
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200231static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200232{
233 struct bsc_nat_ussd_con *conn;
234 struct bsc_nat *nat;
235 struct sockaddr_in sa;
236 socklen_t sa_len = sizeof(sa);
237 int fd;
238
239 if (!(what & BSC_FD_READ))
240 return 0;
241
242 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
243 if (fd < 0) {
244 perror("accept");
245 return fd;
246 }
247
248 nat = (struct bsc_nat *) bfd->data;
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200249 osmo_counter_inc(nat->stats.ussd.reconn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200250
251 conn = bsc_nat_ussd_alloc(nat);
252 if (!conn) {
253 LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n");
254 close(fd);
255 return -1;
256 }
257
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200258 osmo_wqueue_init(&conn->queue, 10);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200259 conn->queue.bfd.data = conn;
260 conn->queue.bfd.fd = fd;
261 conn->queue.bfd.when = BSC_FD_READ;
262 conn->queue.read_cb = ussd_read_cb;
263 conn->queue.write_cb = bsc_write_cb;
264
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200265 if (osmo_fd_register(&conn->queue.bfd) < 0) {
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200266 LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n");
267 bsc_nat_ussd_destroy(conn);
268 return -1;
269 }
270
271 LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n",
272 fd, inet_ntoa(sa.sin_addr));
273
274 /* do authentication */
275 ussd_start_auth(conn);
276 return 0;
277}
278
279int bsc_ussd_init(struct bsc_nat *nat)
280{
281 struct in_addr addr;
282
283 addr.s_addr = INADDR_ANY;
284 if (nat->ussd_local)
285 inet_aton(nat->ussd_local, &addr);
286
287 nat->ussd_listen.data = nat;
288 return make_sock(&nat->ussd_listen, IPPROTO_TCP,
Holger Hans Peter Freyther0d93fb42011-04-11 10:27:10 +0200289 ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200290}
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800291
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200292static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input)
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200293{
294 struct msgb *copy;
295 struct bsc_nat_ussd_con *ussd;
296
297 if (!con->bsc->nat->ussd_con)
298 return -1;
299
300 copy = msgb_alloc_headroom(4096, 128, "forward bts");
301 if (!copy) {
302 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
303 return -1;
304 }
305
306 /* copy the data into the copy */
307 copy->l2h = msgb_put(copy, msgb_l2len(input));
308 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
309
310 /* send it out */
311 ussd = con->bsc->nat->ussd_con;
312 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
313 return 0;
314}
315
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200316static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_request *req,
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200317 struct msgb *input)
318{
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200319 struct msgb *msg, *copy;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200320 struct ipac_msgt_sccp_state *state;
321 struct bsc_nat_ussd_con *ussd;
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200322 uint16_t lac, ci;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200323
324 if (!con->bsc->nat->ussd_con)
325 return -1;
326
327 msg = msgb_alloc_headroom(4096, 128, "forward ussd");
328 if (!msg) {
329 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
330 return -1;
331 }
332
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200333 copy = msgb_alloc_headroom(4096, 128, "forward bts");
334 if (!copy) {
335 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
336 msgb_free(msg);
337 return -1;
338 }
339
340 copy->l2h = msgb_put(copy, msgb_l2len(input));
341 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
342
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200343 msg->l2h = msgb_put(msg, 1);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100344 msg->l2h[0] = IPAC_MSGT_SCCP_OLD;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200345
346 /* fill out the data */
347 state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state));
348 state->trans_id = req->transaction_id;
349 state->invoke_id = req->invoke_id;
350 memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref));
351 memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref));
352 memcpy(state->imsi, con->imsi, strlen(con->imsi));
353
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200354 /* add additional tag/values */
355 lac = htons(con->lac);
356 ci = htons(con->ci);
Holger Hans Peter Freythera164d522013-07-03 16:18:37 +0200357 msgb_tv_fixed_put(msg, USSD_LAC_IE, sizeof(lac), (const uint8_t *) &lac);
358 msgb_tv_fixed_put(msg, USSD_CI_IE, sizeof(ci), (const uint8_t *) &ci);
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200359
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200360 ussd = con->bsc->nat->ussd_con;
361 bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200362 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200363
364 return 0;
365}
366
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +0200367int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed,
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800368 struct msgb *msg)
369{
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200370 uint32_t len;
371 uint8_t msg_type;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200372 uint8_t proto;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200373 uint8_t ti;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200374 struct gsm48_hdr *hdr48;
375 struct bsc_nat_acc_lst *lst;
376 struct ussd_request req;
377
378 /*
379 * various checks to avoid the decoding work. Right now we only want to
380 * decode if the connection was created for USSD, we do have a USSD access
381 * list, a query, a IMSI and such...
382 */
383 if (con->con_type != NAT_CON_TYPE_SSA)
384 return 0;
385
386 if (!con->imsi)
387 return 0;
388
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200389 /* We have not verified the IMSI yet */
390 if (!con->authorized)
391 return 0;
392
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200393 if (!con->bsc->nat->ussd_lst_name)
394 return 0;
395 if (!con->bsc->nat->ussd_query)
396 return 0;
397
398 if (parsed->bssap != BSSAP_MSG_DTAP)
399 return 0;
400
401 if (strlen(con->imsi) > GSM_IMSI_LENGTH)
402 return 0;
403
404 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
405 if (!hdr48)
406 return 0;
407
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200408 proto = hdr48->proto_discr & 0x0f;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200409 msg_type = hdr48->msg_type & 0xbf;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200410 ti = (hdr48->proto_discr & 0x70) >> 4;
411 if (proto != GSM48_PDISC_NC_SS)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200412 return 0;
413
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200414 if (msg_type == GSM0480_MTYPE_REGISTER) {
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200415
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200416 /* now check if it is a IMSI we care about */
417 lst = bsc_nat_acc_lst_find(con->bsc->nat,
418 con->bsc->nat->ussd_lst_name);
419 if (!lst)
420 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200421
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200422 if (bsc_nat_lst_check_allow(lst, con->imsi) != 0)
423 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200424
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200425 /* now decode the message and see if we really want to handle it */
426 memset(&req, 0, sizeof(req));
427 if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1)
428 return 0;
429 if (req.text[0] == 0xff)
430 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200431
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200432 if (regexec(&con->bsc->nat->ussd_query_re,
433 req.text, 0, NULL, 0) == REG_NOMATCH)
434 return 0;
435
436 /* found a USSD query for our subscriber */
437 LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi);
438 con->ussd_ti[ti] = 1;
439 if (forward_ussd(con, &req, msg) != 0)
440 return 0;
441 return 1;
442 } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) {
443 LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n",
444 ti, con->imsi);
445 if (forward_ussd_simple(con, msg) != 0)
446 return 0;
447 return 1;
448 }
449
450 return 0;
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800451}