blob: 29b4feef90c9b944b8ebf4f1d28ad35197d8c4c2 [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 Freyther4579bb12015-04-04 21:55:08 +020025#include <openbsc/bsc_msg_filter.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020026#include <openbsc/ipaccess.h>
27#include <openbsc/socket.h>
Neels Hofmeyrbecfc4c2017-07-19 16:48:42 +020028#include <openbsc/debug.h>
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +080029
Harald Welted36ff762011-03-23 18:26:56 +010030#include <osmocom/gsm/protocol/gsm_08_08.h>
31#include <osmocom/gsm/gsm0480.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010032#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010033#include <osmocom/gsm/tlv.h>
Harald Welte4a88a492014-08-20 23:46:40 +020034#include <osmocom/gsm/ipa.h>
Neels Hofmeyr7b656882017-07-09 22:09:18 +020035#include <osmocom/gsm/gsm0480.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020036
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +020037#include <osmocom/sccp/sccp.h>
38
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020039#include <osmocom/abis/ipa.h>
40
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020041#include <sys/socket.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020042#include <string.h>
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020043#include <unistd.h>
Neels Hofmeyr7b656882017-07-09 22:09:18 +020044#include <errno.h>
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +020045
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +020046#define USSD_LAC_IE 0
47#define USSD_CI_IE 1
48
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020049static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *);
50
51static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat)
52{
53 struct bsc_nat_ussd_con *con;
54
55 con = talloc_zero(nat, struct bsc_nat_ussd_con);
56 if (!con)
57 return NULL;
58
59 con->nat = nat;
60 return con;
61}
62
63static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con)
64{
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020065 if (con->nat->ussd_con == con) {
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +020066 bsc_ussd_close_connections(con->nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020067 con->nat->ussd_con = NULL;
Holger Hans Peter Freyther54f53522010-10-27 11:01:55 +020068 }
69
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020070 close(con->queue.bfd.fd);
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +020071 osmo_fd_unregister(&con->queue.bfd);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +020072 osmo_timer_del(&con->auth_timeout);
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +020073 osmo_wqueue_clear(&con->queue);
Jacob Erlbecke8278122014-03-31 13:42:11 +020074
75 msgb_free(con->pending_msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +020076 talloc_free(con);
77}
78
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +020079static void ussd_pong(struct bsc_nat_ussd_con *conn)
80{
81 struct msgb *msg;
82
83 msg = msgb_alloc_headroom(4096, 128, "pong message");
84 if (!msg) {
85 LOGP(DNAT, LOGL_ERROR, "Failed to allocate pong msg\n");
86 return;
87 }
88
89 msgb_v_put(msg, IPAC_MSGT_PONG);
90 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
91}
92
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020093static int forward_sccp(struct bsc_nat *nat, struct msgb *msg)
94{
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +020095 struct nat_sccp_connection *con;
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +020096 struct bsc_nat_parsed *parsed;
97
98
99 parsed = bsc_nat_parse(msg);
100 if (!parsed) {
101 LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n");
102 msgb_free(msg);
103 return -1;
104 }
105
106 if (!parsed->dest_local_ref) {
107 LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n");
108 msgb_free(msg);
109 return -1;
110 }
111
112 con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref);
113 if (!con || !con->bsc) {
114 LOGP(DNAT, LOGL_ERROR, "No active connection found.\n");
115 msgb_free(msg);
116 return -1;
117 }
118
119 talloc_free(parsed);
120 bsc_write_msg(&con->bsc->write_queue, msg);
121 return 0;
122}
123
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200124static int ussd_read_cb(struct osmo_fd *bfd)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200125{
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200126 struct bsc_nat_ussd_con *conn = bfd->data;
Jacob Erlbecke8278122014-03-31 13:42:11 +0200127 struct msgb *msg = NULL;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200128 struct ipaccess_head *hh;
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200129 int ret;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200130
Jacob Erlbecke8278122014-03-31 13:42:11 +0200131 ret = ipa_msg_recv_buffered(bfd->fd, &msg, &conn->pending_msg);
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200132 if (ret <= 0) {
Jacob Erlbecke8278122014-03-31 13:42:11 +0200133 if (ret == -EAGAIN)
134 return 0;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200135 LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n");
136 bsc_nat_ussd_destroy(conn);
137 return -1;
138 }
139
140 LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200141 osmo_hexdump(msg->data, msg->len), msg->l2h[0]);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200142 hh = (struct ipaccess_head *) msg->data;
143
144 if (hh->proto == IPAC_PROTO_IPACCESS) {
145 if (msg->l2h[0] == IPAC_MSGT_ID_RESP) {
146 struct tlv_parsed tvp;
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200147 int ret;
Harald Welte4a88a492014-08-20 23:46:40 +0200148 ret = ipa_ccm_idtag_parse(&tvp,
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200149 (unsigned char *) msg->l2h + 2,
150 msgb_l2len(msg) - 2);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200151 if (ret < 0) {
152 LOGP(DNAT, LOGL_ERROR, "ignoring IPA response "
153 "message with malformed TLVs\n");
Holger Hans Peter Freyther27876a22013-08-13 14:48:44 +0200154 msgb_free(msg);
Pablo Neira Ayusoca05d432011-04-11 16:32:50 +0200155 return ret;
156 }
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200157 if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME))
158 ussd_auth_con(&tvp, conn);
Jacob Erlbeck6cb2ccc2013-08-14 11:10:34 +0200159 } else if (msg->l2h[0] == IPAC_MSGT_PING) {
160 LOGP(DNAT, LOGL_DEBUG, "Got USSD ping request.\n");
161 ussd_pong(conn);
162 } else {
163 LOGP(DNAT, LOGL_NOTICE, "Got unknown IPACCESS message 0x%02x.\n", msg->l2h[0]);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200164 }
165
166 msgb_free(msg);
167 } else if (hh->proto == IPAC_PROTO_SCCP) {
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200168 forward_sccp(conn->nat, msg);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200169 } else {
170 msgb_free(msg);
171 }
172
173 return 0;
174}
175
176static void ussd_auth_cb(void *_data)
177{
178 LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n");
179 bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data);
180}
181
182static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn)
183{
184 const char *token;
185 int len;
186 if (!conn->nat->ussd_token) {
187 LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n");
188 bsc_nat_ussd_destroy(conn);
189 return;
190 }
191
192 token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME);
193 len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME);
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200194
195 /* last byte should be a NULL */
196 if (strlen(conn->nat->ussd_token) != len - 1)
197 goto disconnect;
198 /* compare everything including the null byte */
199 if (memcmp(conn->nat->ussd_token, token, len) != 0)
200 goto disconnect;
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200201
202 /* it is authenticated now */
203 if (conn->nat->ussd_con && conn->nat->ussd_con != conn)
204 bsc_nat_ussd_destroy(conn->nat->ussd_con);
205
206 LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n");
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200207 osmo_timer_del(&conn->auth_timeout);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200208 conn->authorized = 1;
209 conn->nat->ussd_con = conn;
Holger Hans Peter Freyther2bc90c22013-04-22 10:54:02 +0200210 return;
211
212disconnect:
213 LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n",
214 conn->queue.bfd.fd);
215 bsc_nat_ussd_destroy(conn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200216}
217
218static void ussd_start_auth(struct bsc_nat_ussd_con *conn)
219{
220 struct msgb *msg;
221
Pablo Neira Ayuso51215762017-05-08 20:57:52 +0200222 osmo_timer_setup(&conn->auth_timeout, ussd_auth_cb, conn);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200223 osmo_timer_schedule(&conn->auth_timeout, conn->nat->auth_timeout, 0);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200224
225 msg = msgb_alloc_headroom(4096, 128, "auth message");
226 if (!msg) {
227 LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n");
228 return;
229 }
230
231 msgb_v_put(msg, IPAC_MSGT_ID_GET);
232 bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS);
233}
234
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200235static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what)
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200236{
237 struct bsc_nat_ussd_con *conn;
238 struct bsc_nat *nat;
239 struct sockaddr_in sa;
240 socklen_t sa_len = sizeof(sa);
241 int fd;
242
243 if (!(what & BSC_FD_READ))
244 return 0;
245
246 fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len);
247 if (fd < 0) {
248 perror("accept");
249 return fd;
250 }
251
252 nat = (struct bsc_nat *) bfd->data;
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200253 osmo_counter_inc(nat->stats.ussd.reconn);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200254
255 conn = bsc_nat_ussd_alloc(nat);
256 if (!conn) {
257 LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n");
258 close(fd);
259 return -1;
260 }
261
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200262 osmo_wqueue_init(&conn->queue, 10);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200263 conn->queue.bfd.data = conn;
264 conn->queue.bfd.fd = fd;
265 conn->queue.bfd.when = BSC_FD_READ;
266 conn->queue.read_cb = ussd_read_cb;
267 conn->queue.write_cb = bsc_write_cb;
268
Pablo Neira Ayuso4db92992011-05-06 12:11:23 +0200269 if (osmo_fd_register(&conn->queue.bfd) < 0) {
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200270 LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n");
271 bsc_nat_ussd_destroy(conn);
272 return -1;
273 }
274
275 LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n",
276 fd, inet_ntoa(sa.sin_addr));
277
278 /* do authentication */
279 ussd_start_auth(conn);
280 return 0;
281}
282
283int bsc_ussd_init(struct bsc_nat *nat)
284{
285 struct in_addr addr;
286
287 addr.s_addr = INADDR_ANY;
288 if (nat->ussd_local)
289 inet_aton(nat->ussd_local, &addr);
290
291 nat->ussd_listen.data = nat;
292 return make_sock(&nat->ussd_listen, IPPROTO_TCP,
Holger Hans Peter Freyther0d93fb42011-04-11 10:27:10 +0200293 ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
Holger Hans Peter Freytherc16c2dc2010-10-13 20:22:36 +0200294}
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800295
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200296static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input)
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200297{
298 struct msgb *copy;
299 struct bsc_nat_ussd_con *ussd;
300
301 if (!con->bsc->nat->ussd_con)
302 return -1;
303
304 copy = msgb_alloc_headroom(4096, 128, "forward bts");
305 if (!copy) {
306 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
307 return -1;
308 }
309
310 /* copy the data into the copy */
311 copy->l2h = msgb_put(copy, msgb_l2len(input));
312 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
313
314 /* send it out */
315 ussd = con->bsc->nat->ussd_con;
316 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
317 return 0;
318}
319
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200320static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_request *req,
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200321 struct msgb *input)
322{
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200323 struct msgb *msg, *copy;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200324 struct ipac_msgt_sccp_state *state;
325 struct bsc_nat_ussd_con *ussd;
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200326 uint16_t lac, ci;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200327
328 if (!con->bsc->nat->ussd_con)
329 return -1;
330
331 msg = msgb_alloc_headroom(4096, 128, "forward ussd");
332 if (!msg) {
333 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
334 return -1;
335 }
336
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200337 copy = msgb_alloc_headroom(4096, 128, "forward bts");
338 if (!copy) {
339 LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n");
340 msgb_free(msg);
341 return -1;
342 }
343
344 copy->l2h = msgb_put(copy, msgb_l2len(input));
345 memcpy(copy->l2h, input->l2h, msgb_l2len(input));
346
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200347 msg->l2h = msgb_put(msg, 1);
Holger Hans Peter Freyther368a0a72011-01-07 16:54:46 +0100348 msg->l2h[0] = IPAC_MSGT_SCCP_OLD;
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200349
350 /* fill out the data */
351 state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state));
352 state->trans_id = req->transaction_id;
353 state->invoke_id = req->invoke_id;
354 memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref));
355 memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref));
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200356 memcpy(state->imsi, con->filter_state.imsi, strlen(con->filter_state.imsi));
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200357
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200358 /* add additional tag/values */
359 lac = htons(con->lac);
360 ci = htons(con->ci);
Holger Hans Peter Freythera164d522013-07-03 16:18:37 +0200361 msgb_tv_fixed_put(msg, USSD_LAC_IE, sizeof(lac), (const uint8_t *) &lac);
362 msgb_tv_fixed_put(msg, USSD_CI_IE, sizeof(ci), (const uint8_t *) &ci);
Holger Hans Peter Freyther7a0010b2013-04-29 20:40:44 +0200363
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200364 ussd = con->bsc->nat->ussd_con;
365 bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS);
Holger Hans Peter Freyther90bbccf2010-10-16 17:34:37 +0200366 bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP);
Holger Hans Peter Freyther4c401e72010-10-15 10:09:31 +0200367
368 return 0;
369}
370
Holger Hans Peter Freyther6fcc3a92013-08-26 14:04:43 +0200371int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed,
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800372 struct msgb *msg)
373{
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200374 uint32_t len;
375 uint8_t msg_type;
Holger Hans Peter Freyther5cde92c2011-04-13 18:56:13 +0200376 uint8_t proto;
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200377 uint8_t ti;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200378 struct gsm48_hdr *hdr48;
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200379 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200380 struct ussd_request req;
381
382 /*
383 * various checks to avoid the decoding work. Right now we only want to
384 * decode if the connection was created for USSD, we do have a USSD access
385 * list, a query, a IMSI and such...
386 */
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200387 if (con->filter_state.con_type != FLT_CON_TYPE_SSA)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200388 return 0;
389
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200390 if (!con->filter_state.imsi)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200391 return 0;
392
Holger Hans Peter Freytheref38e852011-04-06 11:27:52 +0200393 /* We have not verified the IMSI yet */
394 if (!con->authorized)
395 return 0;
396
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200397 if (!con->bsc->nat->ussd_lst_name)
398 return 0;
399 if (!con->bsc->nat->ussd_query)
400 return 0;
401
402 if (parsed->bssap != BSSAP_MSG_DTAP)
403 return 0;
404
Harald Welted3fa84d2016-04-20 17:50:17 +0200405 if (strlen(con->filter_state.imsi) > GSM23003_IMSI_MAX_DIGITS)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200406 return 0;
407
408 hdr48 = bsc_unpack_dtap(parsed, msg, &len);
409 if (!hdr48)
410 return 0;
411
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100412 proto = gsm48_hdr_pdisc(hdr48);
413 msg_type = gsm48_hdr_msg_type(hdr48);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +0100414 ti = gsm48_hdr_trans_id_no_ti(hdr48);
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200415 if (proto != GSM48_PDISC_NC_SS)
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200416 return 0;
417
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200418 if (msg_type == GSM0480_MTYPE_REGISTER) {
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200419
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200420 /* now check if it is a IMSI we care about */
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200421 lst = bsc_msg_acc_lst_find(&con->bsc->nat->access_lists,
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200422 con->bsc->nat->ussd_lst_name);
423 if (!lst)
424 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200425
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200426 if (bsc_msg_acc_lst_check_allow(lst, con->filter_state.imsi) != 0)
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200427 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200428
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200429 /* now decode the message and see if we really want to handle it */
430 memset(&req, 0, sizeof(req));
431 if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1)
432 return 0;
433 if (req.text[0] == 0xff)
434 return 0;
Holger Hans Peter Freyther3229f442010-10-11 10:07:37 +0200435
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200436 if (regexec(&con->bsc->nat->ussd_query_re,
437 req.text, 0, NULL, 0) == REG_NOMATCH)
438 return 0;
439
440 /* found a USSD query for our subscriber */
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200441 LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n",
442 con->filter_state.imsi);
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200443 con->ussd_ti[ti] = 1;
444 if (forward_ussd(con, &req, msg) != 0)
445 return 0;
446 return 1;
447 } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) {
448 LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n",
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200449 ti, con->filter_state.imsi);
Holger Hans Peter Freyther123bc322011-04-16 14:06:18 +0200450 if (forward_ussd_simple(con, msg) != 0)
451 return 0;
452 return 1;
453 }
454
455 return 0;
Holger Hans Peter Freyther17870cf2010-09-29 19:32:55 +0800456}