Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1 | /* USSD Filter Code */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | 9a9b3d1 | 2011-04-01 17:32:21 +0200 | [diff] [blame] | 4 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * 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 Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * 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 Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/bsc_nat.h> |
| 24 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 25 | #include <openbsc/ipaccess.h> |
| 26 | #include <openbsc/socket.h> |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 27 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 28 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 29 | #include <osmocom/gsm/gsm0480.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 30 | #include <osmocom/core/talloc.h> |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 31 | #include <osmocom/gsm/tlv.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 33 | #include <osmocom/sccp/sccp.h> |
| 34 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 35 | #include <osmocom/abis/ipa.h> |
| 36 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 37 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 38 | #include <string.h> |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 39 | #include <unistd.h> |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 41 | static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *); |
| 42 | |
| 43 | static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat) |
| 44 | { |
| 45 | struct bsc_nat_ussd_con *con; |
| 46 | |
| 47 | con = talloc_zero(nat, struct bsc_nat_ussd_con); |
| 48 | if (!con) |
| 49 | return NULL; |
| 50 | |
| 51 | con->nat = nat; |
| 52 | return con; |
| 53 | } |
| 54 | |
| 55 | static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con) |
| 56 | { |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 57 | if (con->nat->ussd_con == con) { |
| 58 | bsc_close_ussd_connections(con->nat); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 59 | con->nat->ussd_con = NULL; |
Holger Hans Peter Freyther | d39f310 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 62 | close(con->queue.bfd.fd); |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 63 | osmo_fd_unregister(&con->queue.bfd); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 64 | osmo_timer_del(&con->auth_timeout); |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 65 | osmo_wqueue_clear(&con->queue); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 66 | talloc_free(con); |
| 67 | } |
| 68 | |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 69 | static int forward_sccp(struct bsc_nat *nat, struct msgb *msg) |
| 70 | { |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 71 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 72 | struct bsc_nat_parsed *parsed; |
| 73 | |
| 74 | |
| 75 | parsed = bsc_nat_parse(msg); |
| 76 | if (!parsed) { |
| 77 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n"); |
| 78 | msgb_free(msg); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (!parsed->dest_local_ref) { |
| 83 | LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n"); |
| 84 | msgb_free(msg); |
| 85 | return -1; |
| 86 | } |
| 87 | |
| 88 | con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref); |
| 89 | if (!con || !con->bsc) { |
| 90 | LOGP(DNAT, LOGL_ERROR, "No active connection found.\n"); |
| 91 | msgb_free(msg); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | talloc_free(parsed); |
| 96 | bsc_write_msg(&con->bsc->write_queue, msg); |
| 97 | return 0; |
| 98 | } |
| 99 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 100 | static int ussd_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 101 | { |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 102 | struct bsc_nat_ussd_con *conn = bfd->data; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 103 | struct msgb *msg; |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 104 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 105 | int ret; |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 106 | |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 107 | ret = ipa_msg_recv(bfd->fd, &msg); |
| 108 | if (ret <= 0) { |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 109 | LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n"); |
| 110 | bsc_nat_ussd_destroy(conn); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n", |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 115 | osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 116 | hh = (struct ipaccess_head *) msg->data; |
| 117 | |
| 118 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 119 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 120 | struct tlv_parsed tvp; |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 121 | int ret; |
| 122 | ret = ipaccess_idtag_parse(&tvp, |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 123 | (unsigned char *) msg->l2h + 2, |
| 124 | msgb_l2len(msg) - 2); |
Pablo Neira Ayuso | c056c4a | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 125 | if (ret < 0) { |
| 126 | LOGP(DNAT, LOGL_ERROR, "ignoring IPA response " |
| 127 | "message with malformed TLVs\n"); |
| 128 | return ret; |
| 129 | } |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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"); |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 174 | osmo_timer_del(&conn->auth_timeout); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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; |
Pablo Neira Ayuso | 840ccf6 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 185 | osmo_timer_schedule(&conn->auth_timeout, conn->nat->auth_timeout, 0); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 197 | static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what) |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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; |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 215 | osmo_counter_inc(nat->stats.ussd.reconn); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 224 | osmo_wqueue_init(&conn->queue, 10); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 231 | if (osmo_fd_register(&conn->queue.bfd) < 0) { |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 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, |
Holger Hans Peter Freyther | abb2e47 | 2011-04-11 10:27:10 +0200 | [diff] [blame] | 255 | ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat); |
Holger Hans Peter Freyther | 1ff40d8 | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 256 | } |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 257 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 258 | static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input) |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 259 | { |
| 260 | struct msgb *copy; |
| 261 | struct bsc_nat_ussd_con *ussd; |
| 262 | |
| 263 | if (!con->bsc->nat->ussd_con) |
| 264 | return -1; |
| 265 | |
| 266 | copy = msgb_alloc_headroom(4096, 128, "forward bts"); |
| 267 | if (!copy) { |
| 268 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | /* copy the data into the copy */ |
| 273 | copy->l2h = msgb_put(copy, msgb_l2len(input)); |
| 274 | memcpy(copy->l2h, input->l2h, msgb_l2len(input)); |
| 275 | |
| 276 | /* send it out */ |
| 277 | ussd = con->bsc->nat->ussd_con; |
| 278 | bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP); |
| 279 | return 0; |
| 280 | } |
| 281 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 282 | static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_request *req, |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 283 | struct msgb *input) |
| 284 | { |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 285 | struct msgb *msg, *copy; |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 286 | struct ipac_msgt_sccp_state *state; |
| 287 | struct bsc_nat_ussd_con *ussd; |
| 288 | |
| 289 | if (!con->bsc->nat->ussd_con) |
| 290 | return -1; |
| 291 | |
| 292 | msg = msgb_alloc_headroom(4096, 128, "forward ussd"); |
| 293 | if (!msg) { |
| 294 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 295 | return -1; |
| 296 | } |
| 297 | |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 298 | copy = msgb_alloc_headroom(4096, 128, "forward bts"); |
| 299 | if (!copy) { |
| 300 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 301 | msgb_free(msg); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | copy->l2h = msgb_put(copy, msgb_l2len(input)); |
| 306 | memcpy(copy->l2h, input->l2h, msgb_l2len(input)); |
| 307 | |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 308 | msg->l2h = msgb_put(msg, 1); |
Holger Hans Peter Freyther | a63d35c | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 309 | msg->l2h[0] = IPAC_MSGT_SCCP_OLD; |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 310 | |
| 311 | /* fill out the data */ |
| 312 | state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state)); |
| 313 | state->trans_id = req->transaction_id; |
| 314 | state->invoke_id = req->invoke_id; |
| 315 | memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref)); |
| 316 | memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref)); |
| 317 | memcpy(state->imsi, con->imsi, strlen(con->imsi)); |
| 318 | |
| 319 | ussd = con->bsc->nat->ussd_con; |
| 320 | bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 8ef837f | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 321 | bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 5cd2fae | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Holger Hans Peter Freyther | 3a370eb | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 326 | int bsc_check_ussd(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 327 | struct msgb *msg) |
| 328 | { |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 329 | uint32_t len; |
| 330 | uint8_t msg_type; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 331 | uint8_t proto; |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 332 | uint8_t ti; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 333 | struct gsm48_hdr *hdr48; |
| 334 | struct bsc_nat_acc_lst *lst; |
| 335 | struct ussd_request req; |
| 336 | |
| 337 | /* |
| 338 | * various checks to avoid the decoding work. Right now we only want to |
| 339 | * decode if the connection was created for USSD, we do have a USSD access |
| 340 | * list, a query, a IMSI and such... |
| 341 | */ |
| 342 | if (con->con_type != NAT_CON_TYPE_SSA) |
| 343 | return 0; |
| 344 | |
| 345 | if (!con->imsi) |
| 346 | return 0; |
| 347 | |
Holger Hans Peter Freyther | dfc767e | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 348 | /* We have not verified the IMSI yet */ |
| 349 | if (!con->authorized) |
| 350 | return 0; |
| 351 | |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 352 | if (!con->bsc->nat->ussd_lst_name) |
| 353 | return 0; |
| 354 | if (!con->bsc->nat->ussd_query) |
| 355 | return 0; |
| 356 | |
| 357 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 358 | return 0; |
| 359 | |
| 360 | if (strlen(con->imsi) > GSM_IMSI_LENGTH) |
| 361 | return 0; |
| 362 | |
| 363 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 364 | if (!hdr48) |
| 365 | return 0; |
| 366 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 367 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 368 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 369 | ti = (hdr48->proto_discr & 0x70) >> 4; |
| 370 | if (proto != GSM48_PDISC_NC_SS) |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 371 | return 0; |
| 372 | |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 373 | if (msg_type == GSM0480_MTYPE_REGISTER) { |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 374 | |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 375 | /* now check if it is a IMSI we care about */ |
| 376 | lst = bsc_nat_acc_lst_find(con->bsc->nat, |
| 377 | con->bsc->nat->ussd_lst_name); |
| 378 | if (!lst) |
| 379 | return 0; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 380 | |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 381 | if (bsc_nat_lst_check_allow(lst, con->imsi) != 0) |
| 382 | return 0; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 383 | |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 384 | /* now decode the message and see if we really want to handle it */ |
| 385 | memset(&req, 0, sizeof(req)); |
| 386 | if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1) |
| 387 | return 0; |
| 388 | if (req.text[0] == 0xff) |
| 389 | return 0; |
Holger Hans Peter Freyther | 6f744c3 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 390 | |
Holger Hans Peter Freyther | 60ab41b | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 391 | if (regexec(&con->bsc->nat->ussd_query_re, |
| 392 | req.text, 0, NULL, 0) == REG_NOMATCH) |
| 393 | return 0; |
| 394 | |
| 395 | /* found a USSD query for our subscriber */ |
| 396 | LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi); |
| 397 | con->ussd_ti[ti] = 1; |
| 398 | if (forward_ussd(con, &req, msg) != 0) |
| 399 | return 0; |
| 400 | return 1; |
| 401 | } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) { |
| 402 | LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n", |
| 403 | ti, con->imsi); |
| 404 | if (forward_ussd_simple(con, msg) != 0) |
| 405 | return 0; |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | return 0; |
Holger Hans Peter Freyther | 908bc9b | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 410 | } |