Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1 | /* USSD Filter Code */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010 by On-Waves |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (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 |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_nat.h> |
| 25 | #include <openbsc/bsc_nat_sccp.h> |
| 26 | |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 27 | #include <osmocore/protocol/gsm_08_08.h> |
| 28 | |
| 29 | #include <osmocore/gsm0480.h> |
| 30 | |
| 31 | #include <string.h> |
| 32 | |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 33 | |
| 34 | int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, |
| 35 | struct msgb *msg) |
| 36 | { |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 37 | uint32_t len; |
| 38 | uint8_t msg_type; |
| 39 | struct gsm48_hdr *hdr48; |
| 40 | struct bsc_nat_acc_lst *lst; |
| 41 | struct ussd_request req; |
| 42 | |
| 43 | /* |
| 44 | * various checks to avoid the decoding work. Right now we only want to |
| 45 | * decode if the connection was created for USSD, we do have a USSD access |
| 46 | * list, a query, a IMSI and such... |
| 47 | */ |
| 48 | if (con->con_type != NAT_CON_TYPE_SSA) |
| 49 | return 0; |
| 50 | |
| 51 | if (!con->imsi) |
| 52 | return 0; |
| 53 | |
| 54 | if (!con->bsc->nat->ussd_lst_name) |
| 55 | return 0; |
| 56 | if (!con->bsc->nat->ussd_query) |
| 57 | return 0; |
| 58 | |
| 59 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 60 | return 0; |
| 61 | |
| 62 | if (strlen(con->imsi) > GSM_IMSI_LENGTH) |
| 63 | return 0; |
| 64 | |
| 65 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 66 | if (!hdr48) |
| 67 | return 0; |
| 68 | |
| 69 | msg_type = hdr48->msg_type & 0xbf; |
| 70 | if (hdr48->proto_discr != GSM48_PDISC_NC_SS || msg_type != GSM0480_MTYPE_REGISTER) |
| 71 | return 0; |
| 72 | |
| 73 | /* now check if it is a IMSI we care about */ |
| 74 | lst = bsc_nat_acc_lst_find(con->bsc->nat, con->bsc->nat->ussd_lst_name); |
| 75 | if (!lst) |
| 76 | return 0; |
| 77 | |
| 78 | if (bsc_nat_lst_check_allow(lst, con->imsi) != 0) |
| 79 | return 0; |
| 80 | |
| 81 | /* now decode the message and see if we really want to handle it */ |
| 82 | memset(&req, 0, sizeof(req)); |
| 83 | if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1) |
| 84 | return 0; |
| 85 | if (req.text[0] == 0xff) |
| 86 | return 0; |
| 87 | |
| 88 | if (strcmp(req.text, con->bsc->nat->ussd_query) != 0) |
| 89 | return 0; |
| 90 | |
| 91 | /* found a USSD query for our subscriber */ |
| 92 | LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi); |
| 93 | return 1; |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 94 | } |