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 | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 34 | #include <osmocom/sccp/sccp.h> |
| 35 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 36 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 37 | #include <string.h> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 38 | #include <unistd.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 40 | struct bsc_nat_ussd_con { |
| 41 | struct write_queue queue; |
| 42 | struct bsc_nat *nat; |
| 43 | int authorized; |
| 44 | |
| 45 | struct timer_list auth_timeout; |
| 46 | }; |
| 47 | |
| 48 | static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *); |
| 49 | |
| 50 | static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat) |
| 51 | { |
| 52 | struct bsc_nat_ussd_con *con; |
| 53 | |
| 54 | con = talloc_zero(nat, struct bsc_nat_ussd_con); |
| 55 | if (!con) |
| 56 | return NULL; |
| 57 | |
| 58 | con->nat = nat; |
| 59 | return con; |
| 60 | } |
| 61 | |
| 62 | static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con) |
| 63 | { |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 64 | if (con->nat->ussd_con == con) { |
| 65 | bsc_close_ussd_connections(con->nat); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 66 | con->nat->ussd_con = NULL; |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 69 | close(con->queue.bfd.fd); |
| 70 | bsc_unregister_fd(&con->queue.bfd); |
| 71 | bsc_del_timer(&con->auth_timeout); |
| 72 | write_queue_clear(&con->queue); |
| 73 | talloc_free(con); |
| 74 | } |
| 75 | |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 76 | static int forward_sccp(struct bsc_nat *nat, struct msgb *msg) |
| 77 | { |
| 78 | struct sccp_connections *con; |
| 79 | struct bsc_nat_parsed *parsed; |
| 80 | |
| 81 | |
| 82 | parsed = bsc_nat_parse(msg); |
| 83 | if (!parsed) { |
| 84 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n"); |
| 85 | msgb_free(msg); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | if (!parsed->dest_local_ref) { |
| 90 | LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n"); |
| 91 | msgb_free(msg); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref); |
| 96 | if (!con || !con->bsc) { |
| 97 | LOGP(DNAT, LOGL_ERROR, "No active connection found.\n"); |
| 98 | msgb_free(msg); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | talloc_free(parsed); |
| 103 | bsc_write_msg(&con->bsc->write_queue, msg); |
| 104 | return 0; |
| 105 | } |
| 106 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 107 | static int ussd_read_cb(struct bsc_fd *bfd) |
| 108 | { |
| 109 | int error; |
| 110 | struct bsc_nat_ussd_con *conn = bfd->data; |
| 111 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 112 | struct ipaccess_head *hh; |
| 113 | |
| 114 | if (!msg) { |
| 115 | LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n"); |
| 116 | bsc_nat_ussd_destroy(conn); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n", |
| 121 | hexdump(msg->data, msg->len), msg->l2h[0]); |
| 122 | hh = (struct ipaccess_head *) msg->data; |
| 123 | |
| 124 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 125 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 126 | struct tlv_parsed tvp; |
| 127 | ipaccess_idtag_parse(&tvp, |
| 128 | (unsigned char *) msg->l2h + 2, |
| 129 | msgb_l2len(msg) - 2); |
| 130 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 131 | ussd_auth_con(&tvp, conn); |
| 132 | } |
| 133 | |
| 134 | msgb_free(msg); |
| 135 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 136 | forward_sccp(conn->nat, msg); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 137 | } else { |
| 138 | msgb_free(msg); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | static void ussd_auth_cb(void *_data) |
| 145 | { |
| 146 | LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n"); |
| 147 | bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data); |
| 148 | } |
| 149 | |
| 150 | static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn) |
| 151 | { |
| 152 | const char *token; |
| 153 | int len; |
| 154 | if (!conn->nat->ussd_token) { |
| 155 | LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n"); |
| 156 | bsc_nat_ussd_destroy(conn); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 161 | len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
| 162 | if (strncmp(conn->nat->ussd_token, token, len) != 0) { |
| 163 | LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n", |
| 164 | conn->queue.bfd.fd); |
| 165 | bsc_nat_ussd_destroy(conn); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | /* it is authenticated now */ |
| 170 | if (conn->nat->ussd_con && conn->nat->ussd_con != conn) |
| 171 | bsc_nat_ussd_destroy(conn->nat->ussd_con); |
| 172 | |
| 173 | LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n"); |
| 174 | bsc_del_timer(&conn->auth_timeout); |
| 175 | conn->authorized = 1; |
| 176 | conn->nat->ussd_con = conn; |
| 177 | } |
| 178 | |
| 179 | static void ussd_start_auth(struct bsc_nat_ussd_con *conn) |
| 180 | { |
| 181 | struct msgb *msg; |
| 182 | |
| 183 | conn->auth_timeout.data = conn; |
| 184 | conn->auth_timeout.cb = ussd_auth_cb; |
| 185 | bsc_schedule_timer(&conn->auth_timeout, conn->nat->auth_timeout, 0); |
| 186 | |
| 187 | msg = msgb_alloc_headroom(4096, 128, "auth message"); |
| 188 | if (!msg) { |
| 189 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n"); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | msgb_v_put(msg, IPAC_MSGT_ID_GET); |
| 194 | bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS); |
| 195 | } |
| 196 | |
| 197 | static int ussd_listen_cb(struct bsc_fd *bfd, unsigned int what) |
| 198 | { |
| 199 | struct bsc_nat_ussd_con *conn; |
| 200 | struct bsc_nat *nat; |
| 201 | struct sockaddr_in sa; |
| 202 | socklen_t sa_len = sizeof(sa); |
| 203 | int fd; |
| 204 | |
| 205 | if (!(what & BSC_FD_READ)) |
| 206 | return 0; |
| 207 | |
| 208 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 209 | if (fd < 0) { |
| 210 | perror("accept"); |
| 211 | return fd; |
| 212 | } |
| 213 | |
| 214 | nat = (struct bsc_nat *) bfd->data; |
| 215 | counter_inc(nat->stats.ussd.reconn); |
| 216 | |
| 217 | conn = bsc_nat_ussd_alloc(nat); |
| 218 | if (!conn) { |
| 219 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n"); |
| 220 | close(fd); |
| 221 | return -1; |
| 222 | } |
| 223 | |
| 224 | write_queue_init(&conn->queue, 10); |
| 225 | conn->queue.bfd.data = conn; |
| 226 | conn->queue.bfd.fd = fd; |
| 227 | conn->queue.bfd.when = BSC_FD_READ; |
| 228 | conn->queue.read_cb = ussd_read_cb; |
| 229 | conn->queue.write_cb = bsc_write_cb; |
| 230 | |
| 231 | if (bsc_register_fd(&conn->queue.bfd) < 0) { |
| 232 | LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n"); |
| 233 | bsc_nat_ussd_destroy(conn); |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n", |
| 238 | fd, inet_ntoa(sa.sin_addr)); |
| 239 | |
| 240 | /* do authentication */ |
| 241 | ussd_start_auth(conn); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int bsc_ussd_init(struct bsc_nat *nat) |
| 246 | { |
| 247 | struct in_addr addr; |
| 248 | |
| 249 | addr.s_addr = INADDR_ANY; |
| 250 | if (nat->ussd_local) |
| 251 | inet_aton(nat->ussd_local, &addr); |
| 252 | |
| 253 | nat->ussd_listen.data = nat; |
| 254 | return make_sock(&nat->ussd_listen, IPPROTO_TCP, |
| 255 | ntohl(addr.s_addr), 5001, ussd_listen_cb); |
| 256 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 257 | |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 258 | static int forward_ussd(struct sccp_connections *con, const struct ussd_request *req, |
| 259 | struct msgb *input) |
| 260 | { |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 261 | struct msgb *msg, *copy; |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 262 | struct ipac_msgt_sccp_state *state; |
| 263 | struct bsc_nat_ussd_con *ussd; |
| 264 | |
| 265 | if (!con->bsc->nat->ussd_con) |
| 266 | return -1; |
| 267 | |
| 268 | msg = msgb_alloc_headroom(4096, 128, "forward ussd"); |
| 269 | if (!msg) { |
| 270 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 271 | return -1; |
| 272 | } |
| 273 | |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 274 | copy = msgb_alloc_headroom(4096, 128, "forward bts"); |
| 275 | if (!copy) { |
| 276 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 277 | msgb_free(msg); |
| 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | copy->l2h = msgb_put(copy, msgb_l2len(input)); |
| 282 | memcpy(copy->l2h, input->l2h, msgb_l2len(input)); |
| 283 | |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 284 | msg->l2h = msgb_put(msg, 1); |
| 285 | msg->l2h[0] = IPAC_MSGT_SCCP_STATE; |
| 286 | |
| 287 | /* fill out the data */ |
| 288 | state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state)); |
| 289 | state->trans_id = req->transaction_id; |
| 290 | state->invoke_id = req->invoke_id; |
| 291 | memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref)); |
| 292 | memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref)); |
| 293 | memcpy(state->imsi, con->imsi, strlen(con->imsi)); |
| 294 | |
| 295 | ussd = con->bsc->nat->ussd_con; |
| 296 | bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 297 | bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 302 | int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, |
| 303 | struct msgb *msg) |
| 304 | { |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 305 | uint32_t len; |
| 306 | uint8_t msg_type; |
| 307 | struct gsm48_hdr *hdr48; |
| 308 | struct bsc_nat_acc_lst *lst; |
| 309 | struct ussd_request req; |
| 310 | |
| 311 | /* |
| 312 | * various checks to avoid the decoding work. Right now we only want to |
| 313 | * decode if the connection was created for USSD, we do have a USSD access |
| 314 | * list, a query, a IMSI and such... |
| 315 | */ |
| 316 | if (con->con_type != NAT_CON_TYPE_SSA) |
| 317 | return 0; |
| 318 | |
| 319 | if (!con->imsi) |
| 320 | return 0; |
| 321 | |
| 322 | if (!con->bsc->nat->ussd_lst_name) |
| 323 | return 0; |
| 324 | if (!con->bsc->nat->ussd_query) |
| 325 | return 0; |
| 326 | |
| 327 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 328 | return 0; |
| 329 | |
| 330 | if (strlen(con->imsi) > GSM_IMSI_LENGTH) |
| 331 | return 0; |
| 332 | |
| 333 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 334 | if (!hdr48) |
| 335 | return 0; |
| 336 | |
| 337 | msg_type = hdr48->msg_type & 0xbf; |
| 338 | if (hdr48->proto_discr != GSM48_PDISC_NC_SS || msg_type != GSM0480_MTYPE_REGISTER) |
| 339 | return 0; |
| 340 | |
| 341 | /* now check if it is a IMSI we care about */ |
| 342 | lst = bsc_nat_acc_lst_find(con->bsc->nat, con->bsc->nat->ussd_lst_name); |
| 343 | if (!lst) |
| 344 | return 0; |
| 345 | |
| 346 | if (bsc_nat_lst_check_allow(lst, con->imsi) != 0) |
| 347 | return 0; |
| 348 | |
| 349 | /* now decode the message and see if we really want to handle it */ |
| 350 | memset(&req, 0, sizeof(req)); |
| 351 | if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1) |
| 352 | return 0; |
| 353 | if (req.text[0] == 0xff) |
| 354 | return 0; |
| 355 | |
| 356 | if (strcmp(req.text, con->bsc->nat->ussd_query) != 0) |
| 357 | return 0; |
| 358 | |
| 359 | /* found a USSD query for our subscriber */ |
| 360 | LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi); |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 361 | if (forward_ussd(con, &req, msg) != 0) |
| 362 | return 0; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 363 | return 1; |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 364 | } |