Holger Hans Peter Freyther | 908bc9b | 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> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame^] | 26 | #include <openbsc/ipaccess.h> |
| 27 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 28 | |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 29 | #include <osmocore/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 30 | #include <osmocore/gsm0480.h> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame^] | 31 | #include <osmocore/talloc.h> |
| 32 | #include <osmocore/tlv.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame^] | 34 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 35 | #include <string.h> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame^] | 36 | #include <unistd.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 37 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame^] | 38 | struct bsc_nat_ussd_con { |
| 39 | struct write_queue queue; |
| 40 | struct bsc_nat *nat; |
| 41 | int authorized; |
| 42 | |
| 43 | struct timer_list auth_timeout; |
| 44 | }; |
| 45 | |
| 46 | static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *); |
| 47 | |
| 48 | static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat) |
| 49 | { |
| 50 | struct bsc_nat_ussd_con *con; |
| 51 | |
| 52 | con = talloc_zero(nat, struct bsc_nat_ussd_con); |
| 53 | if (!con) |
| 54 | return NULL; |
| 55 | |
| 56 | con->nat = nat; |
| 57 | return con; |
| 58 | } |
| 59 | |
| 60 | static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con) |
| 61 | { |
| 62 | if (con->nat->ussd_con == con) |
| 63 | con->nat->ussd_con = NULL; |
| 64 | close(con->queue.bfd.fd); |
| 65 | bsc_unregister_fd(&con->queue.bfd); |
| 66 | bsc_del_timer(&con->auth_timeout); |
| 67 | write_queue_clear(&con->queue); |
| 68 | talloc_free(con); |
| 69 | } |
| 70 | |
| 71 | static int ussd_read_cb(struct bsc_fd *bfd) |
| 72 | { |
| 73 | int error; |
| 74 | struct bsc_nat_ussd_con *conn = bfd->data; |
| 75 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 76 | struct ipaccess_head *hh; |
| 77 | |
| 78 | if (!msg) { |
| 79 | LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n"); |
| 80 | bsc_nat_ussd_destroy(conn); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n", |
| 85 | hexdump(msg->data, msg->len), msg->l2h[0]); |
| 86 | hh = (struct ipaccess_head *) msg->data; |
| 87 | |
| 88 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 89 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 90 | struct tlv_parsed tvp; |
| 91 | ipaccess_idtag_parse(&tvp, |
| 92 | (unsigned char *) msg->l2h + 2, |
| 93 | msgb_l2len(msg) - 2); |
| 94 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 95 | ussd_auth_con(&tvp, conn); |
| 96 | } |
| 97 | |
| 98 | msgb_free(msg); |
| 99 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
| 100 | LOGP(DNAT, LOGL_ERROR, "USSD SCCP is not handled\n"); |
| 101 | msgb_free(msg); |
| 102 | } else { |
| 103 | msgb_free(msg); |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void ussd_auth_cb(void *_data) |
| 110 | { |
| 111 | LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n"); |
| 112 | bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data); |
| 113 | } |
| 114 | |
| 115 | static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn) |
| 116 | { |
| 117 | const char *token; |
| 118 | int len; |
| 119 | if (!conn->nat->ussd_token) { |
| 120 | LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n"); |
| 121 | bsc_nat_ussd_destroy(conn); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 126 | len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
| 127 | if (strncmp(conn->nat->ussd_token, token, len) != 0) { |
| 128 | LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n", |
| 129 | conn->queue.bfd.fd); |
| 130 | bsc_nat_ussd_destroy(conn); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | /* it is authenticated now */ |
| 135 | if (conn->nat->ussd_con && conn->nat->ussd_con != conn) |
| 136 | bsc_nat_ussd_destroy(conn->nat->ussd_con); |
| 137 | |
| 138 | LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n"); |
| 139 | bsc_del_timer(&conn->auth_timeout); |
| 140 | conn->authorized = 1; |
| 141 | conn->nat->ussd_con = conn; |
| 142 | } |
| 143 | |
| 144 | static void ussd_start_auth(struct bsc_nat_ussd_con *conn) |
| 145 | { |
| 146 | struct msgb *msg; |
| 147 | |
| 148 | conn->auth_timeout.data = conn; |
| 149 | conn->auth_timeout.cb = ussd_auth_cb; |
| 150 | bsc_schedule_timer(&conn->auth_timeout, conn->nat->auth_timeout, 0); |
| 151 | |
| 152 | msg = msgb_alloc_headroom(4096, 128, "auth message"); |
| 153 | if (!msg) { |
| 154 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | msgb_v_put(msg, IPAC_MSGT_ID_GET); |
| 159 | bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS); |
| 160 | } |
| 161 | |
| 162 | static int ussd_listen_cb(struct bsc_fd *bfd, unsigned int what) |
| 163 | { |
| 164 | struct bsc_nat_ussd_con *conn; |
| 165 | struct bsc_nat *nat; |
| 166 | struct sockaddr_in sa; |
| 167 | socklen_t sa_len = sizeof(sa); |
| 168 | int fd; |
| 169 | |
| 170 | if (!(what & BSC_FD_READ)) |
| 171 | return 0; |
| 172 | |
| 173 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 174 | if (fd < 0) { |
| 175 | perror("accept"); |
| 176 | return fd; |
| 177 | } |
| 178 | |
| 179 | nat = (struct bsc_nat *) bfd->data; |
| 180 | counter_inc(nat->stats.ussd.reconn); |
| 181 | |
| 182 | conn = bsc_nat_ussd_alloc(nat); |
| 183 | if (!conn) { |
| 184 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n"); |
| 185 | close(fd); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | write_queue_init(&conn->queue, 10); |
| 190 | conn->queue.bfd.data = conn; |
| 191 | conn->queue.bfd.fd = fd; |
| 192 | conn->queue.bfd.when = BSC_FD_READ; |
| 193 | conn->queue.read_cb = ussd_read_cb; |
| 194 | conn->queue.write_cb = bsc_write_cb; |
| 195 | |
| 196 | if (bsc_register_fd(&conn->queue.bfd) < 0) { |
| 197 | LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n"); |
| 198 | bsc_nat_ussd_destroy(conn); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n", |
| 203 | fd, inet_ntoa(sa.sin_addr)); |
| 204 | |
| 205 | /* do authentication */ |
| 206 | ussd_start_auth(conn); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int bsc_ussd_init(struct bsc_nat *nat) |
| 211 | { |
| 212 | struct in_addr addr; |
| 213 | |
| 214 | addr.s_addr = INADDR_ANY; |
| 215 | if (nat->ussd_local) |
| 216 | inet_aton(nat->ussd_local, &addr); |
| 217 | |
| 218 | nat->ussd_listen.data = nat; |
| 219 | return make_sock(&nat->ussd_listen, IPPROTO_TCP, |
| 220 | ntohl(addr.s_addr), 5001, ussd_listen_cb); |
| 221 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 222 | |
| 223 | int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, |
| 224 | struct msgb *msg) |
| 225 | { |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 226 | uint32_t len; |
| 227 | uint8_t msg_type; |
| 228 | struct gsm48_hdr *hdr48; |
| 229 | struct bsc_nat_acc_lst *lst; |
| 230 | struct ussd_request req; |
| 231 | |
| 232 | /* |
| 233 | * various checks to avoid the decoding work. Right now we only want to |
| 234 | * decode if the connection was created for USSD, we do have a USSD access |
| 235 | * list, a query, a IMSI and such... |
| 236 | */ |
| 237 | if (con->con_type != NAT_CON_TYPE_SSA) |
| 238 | return 0; |
| 239 | |
| 240 | if (!con->imsi) |
| 241 | return 0; |
| 242 | |
| 243 | if (!con->bsc->nat->ussd_lst_name) |
| 244 | return 0; |
| 245 | if (!con->bsc->nat->ussd_query) |
| 246 | return 0; |
| 247 | |
| 248 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 249 | return 0; |
| 250 | |
| 251 | if (strlen(con->imsi) > GSM_IMSI_LENGTH) |
| 252 | return 0; |
| 253 | |
| 254 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 255 | if (!hdr48) |
| 256 | return 0; |
| 257 | |
| 258 | msg_type = hdr48->msg_type & 0xbf; |
| 259 | if (hdr48->proto_discr != GSM48_PDISC_NC_SS || msg_type != GSM0480_MTYPE_REGISTER) |
| 260 | return 0; |
| 261 | |
| 262 | /* now check if it is a IMSI we care about */ |
| 263 | lst = bsc_nat_acc_lst_find(con->bsc->nat, con->bsc->nat->ussd_lst_name); |
| 264 | if (!lst) |
| 265 | return 0; |
| 266 | |
| 267 | if (bsc_nat_lst_check_allow(lst, con->imsi) != 0) |
| 268 | return 0; |
| 269 | |
| 270 | /* now decode the message and see if we really want to handle it */ |
| 271 | memset(&req, 0, sizeof(req)); |
| 272 | if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1) |
| 273 | return 0; |
| 274 | if (req.text[0] == 0xff) |
| 275 | return 0; |
| 276 | |
| 277 | if (strcmp(req.text, con->bsc->nat->ussd_query) != 0) |
| 278 | return 0; |
| 279 | |
| 280 | /* found a USSD query for our subscriber */ |
| 281 | LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi); |
| 282 | return 1; |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 283 | } |