Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 1 | /* USSD Filter Code */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | a18b116 | 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 | 17870cf | 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 | 9af6ddf | 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 | 17870cf | 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 | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 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 | 17870cf | 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 | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 25 | #include <openbsc/bsc_msg_filter.h> |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 26 | #include <openbsc/ipaccess.h> |
| 27 | #include <openbsc/socket.h> |
Neels Hofmeyr | 979cd26 | 2017-07-19 16:48:42 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 29 | |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 31 | #include <osmocom/gsm/gsm0480.h> |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 33 | #include <osmocom/gsm/tlv.h> |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 34 | #include <osmocom/gsm/ipa.h> |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 36 | #include <osmocom/sccp/sccp.h> |
| 37 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 38 | #include <osmocom/abis/ipa.h> |
| 39 | |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 40 | #include <sys/socket.h> |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 41 | #include <string.h> |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | 7a0010b | 2013-04-29 20:40:44 +0200 | [diff] [blame] | 44 | #define USSD_LAC_IE 0 |
| 45 | #define USSD_CI_IE 1 |
| 46 | |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 47 | static void ussd_auth_con(struct tlv_parsed *, struct bsc_nat_ussd_con *); |
| 48 | |
| 49 | static struct bsc_nat_ussd_con *bsc_nat_ussd_alloc(struct bsc_nat *nat) |
| 50 | { |
| 51 | struct bsc_nat_ussd_con *con; |
| 52 | |
| 53 | con = talloc_zero(nat, struct bsc_nat_ussd_con); |
| 54 | if (!con) |
| 55 | return NULL; |
| 56 | |
| 57 | con->nat = nat; |
| 58 | return con; |
| 59 | } |
| 60 | |
| 61 | static void bsc_nat_ussd_destroy(struct bsc_nat_ussd_con *con) |
| 62 | { |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 63 | if (con->nat->ussd_con == con) { |
Holger Hans Peter Freyther | 6fcc3a9 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 64 | bsc_ussd_close_connections(con->nat); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 65 | con->nat->ussd_con = NULL; |
Holger Hans Peter Freyther | 54f5352 | 2010-10-27 11:01:55 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 68 | close(con->queue.bfd.fd); |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 69 | osmo_fd_unregister(&con->queue.bfd); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 70 | osmo_timer_del(&con->auth_timeout); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 71 | osmo_wqueue_clear(&con->queue); |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 72 | |
| 73 | msgb_free(con->pending_msg); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 74 | talloc_free(con); |
| 75 | } |
| 76 | |
Jacob Erlbeck | 6cb2ccc | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 77 | static void ussd_pong(struct bsc_nat_ussd_con *conn) |
| 78 | { |
| 79 | struct msgb *msg; |
| 80 | |
| 81 | msg = msgb_alloc_headroom(4096, 128, "pong message"); |
| 82 | if (!msg) { |
| 83 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate pong msg\n"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | msgb_v_put(msg, IPAC_MSGT_PONG); |
| 88 | bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS); |
| 89 | } |
| 90 | |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 91 | static int forward_sccp(struct bsc_nat *nat, struct msgb *msg) |
| 92 | { |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 93 | struct nat_sccp_connection *con; |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 94 | struct bsc_nat_parsed *parsed; |
| 95 | |
| 96 | |
| 97 | parsed = bsc_nat_parse(msg); |
| 98 | if (!parsed) { |
| 99 | LOGP(DNAT, LOGL_ERROR, "Can not parse msg from USSD.\n"); |
| 100 | msgb_free(msg); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | if (!parsed->dest_local_ref) { |
| 105 | LOGP(DNAT, LOGL_ERROR, "No destination local reference.\n"); |
| 106 | msgb_free(msg); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | con = bsc_nat_find_con_by_bsc(nat, parsed->dest_local_ref); |
| 111 | if (!con || !con->bsc) { |
| 112 | LOGP(DNAT, LOGL_ERROR, "No active connection found.\n"); |
| 113 | msgb_free(msg); |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | talloc_free(parsed); |
| 118 | bsc_write_msg(&con->bsc->write_queue, msg); |
| 119 | return 0; |
| 120 | } |
| 121 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 122 | static int ussd_read_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 123 | { |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 124 | struct bsc_nat_ussd_con *conn = bfd->data; |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 125 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 126 | struct ipaccess_head *hh; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 127 | int ret; |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 128 | |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 129 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &conn->pending_msg); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 130 | if (ret <= 0) { |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 131 | if (ret == -EAGAIN) |
| 132 | return 0; |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 133 | LOGP(DNAT, LOGL_ERROR, "USSD Connection was lost.\n"); |
| 134 | bsc_nat_ussd_destroy(conn); |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | LOGP(DNAT, LOGL_NOTICE, "MSG from USSD: %s proto: %d\n", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 139 | osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 140 | hh = (struct ipaccess_head *) msg->data; |
| 141 | |
| 142 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 143 | if (msg->l2h[0] == IPAC_MSGT_ID_RESP) { |
| 144 | struct tlv_parsed tvp; |
Pablo Neira Ayuso | ca05d43 | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 145 | int ret; |
Harald Welte | 4a88a49 | 2014-08-20 23:46:40 +0200 | [diff] [blame] | 146 | ret = ipa_ccm_idtag_parse(&tvp, |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 147 | (unsigned char *) msg->l2h + 2, |
| 148 | msgb_l2len(msg) - 2); |
Pablo Neira Ayuso | ca05d43 | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 149 | if (ret < 0) { |
| 150 | LOGP(DNAT, LOGL_ERROR, "ignoring IPA response " |
| 151 | "message with malformed TLVs\n"); |
Holger Hans Peter Freyther | 27876a2 | 2013-08-13 14:48:44 +0200 | [diff] [blame] | 152 | msgb_free(msg); |
Pablo Neira Ayuso | ca05d43 | 2011-04-11 16:32:50 +0200 | [diff] [blame] | 153 | return ret; |
| 154 | } |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 155 | if (TLVP_PRESENT(&tvp, IPAC_IDTAG_UNITNAME)) |
| 156 | ussd_auth_con(&tvp, conn); |
Jacob Erlbeck | 6cb2ccc | 2013-08-14 11:10:34 +0200 | [diff] [blame] | 157 | } else if (msg->l2h[0] == IPAC_MSGT_PING) { |
| 158 | LOGP(DNAT, LOGL_DEBUG, "Got USSD ping request.\n"); |
| 159 | ussd_pong(conn); |
| 160 | } else { |
| 161 | LOGP(DNAT, LOGL_NOTICE, "Got unknown IPACCESS message 0x%02x.\n", msg->l2h[0]); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | msgb_free(msg); |
| 165 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 166 | forward_sccp(conn->nat, msg); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 167 | } else { |
| 168 | msgb_free(msg); |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void ussd_auth_cb(void *_data) |
| 175 | { |
| 176 | LOGP(DNAT, LOGL_ERROR, "USSD module didn't authenticate\n"); |
| 177 | bsc_nat_ussd_destroy((struct bsc_nat_ussd_con *) _data); |
| 178 | } |
| 179 | |
| 180 | static void ussd_auth_con(struct tlv_parsed *tvp, struct bsc_nat_ussd_con *conn) |
| 181 | { |
| 182 | const char *token; |
| 183 | int len; |
| 184 | if (!conn->nat->ussd_token) { |
| 185 | LOGP(DNAT, LOGL_ERROR, "No USSD token set. Closing\n"); |
| 186 | bsc_nat_ussd_destroy(conn); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | token = (const char *) TLVP_VAL(tvp, IPAC_IDTAG_UNITNAME); |
| 191 | len = TLVP_LEN(tvp, IPAC_IDTAG_UNITNAME); |
Holger Hans Peter Freyther | 2bc90c2 | 2013-04-22 10:54:02 +0200 | [diff] [blame] | 192 | |
| 193 | /* last byte should be a NULL */ |
| 194 | if (strlen(conn->nat->ussd_token) != len - 1) |
| 195 | goto disconnect; |
| 196 | /* compare everything including the null byte */ |
| 197 | if (memcmp(conn->nat->ussd_token, token, len) != 0) |
| 198 | goto disconnect; |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 199 | |
| 200 | /* it is authenticated now */ |
| 201 | if (conn->nat->ussd_con && conn->nat->ussd_con != conn) |
| 202 | bsc_nat_ussd_destroy(conn->nat->ussd_con); |
| 203 | |
| 204 | LOGP(DNAT, LOGL_ERROR, "USSD token specified. USSD provider is connected.\n"); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 205 | osmo_timer_del(&conn->auth_timeout); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 206 | conn->authorized = 1; |
| 207 | conn->nat->ussd_con = conn; |
Holger Hans Peter Freyther | 2bc90c2 | 2013-04-22 10:54:02 +0200 | [diff] [blame] | 208 | return; |
| 209 | |
| 210 | disconnect: |
| 211 | LOGP(DNAT, LOGL_ERROR, "Wrong USSD token by client: %d\n", |
| 212 | conn->queue.bfd.fd); |
| 213 | bsc_nat_ussd_destroy(conn); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void ussd_start_auth(struct bsc_nat_ussd_con *conn) |
| 217 | { |
| 218 | struct msgb *msg; |
| 219 | |
Pablo Neira Ayuso | 5121576 | 2017-05-08 20:57:52 +0200 | [diff] [blame] | 220 | osmo_timer_setup(&conn->auth_timeout, ussd_auth_cb, conn); |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 221 | osmo_timer_schedule(&conn->auth_timeout, conn->nat->auth_timeout, 0); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 222 | |
| 223 | msg = msgb_alloc_headroom(4096, 128, "auth message"); |
| 224 | if (!msg) { |
| 225 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate auth msg\n"); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | msgb_v_put(msg, IPAC_MSGT_ID_GET); |
| 230 | bsc_do_write(&conn->queue, msg, IPAC_PROTO_IPACCESS); |
| 231 | } |
| 232 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 233 | static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what) |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 234 | { |
| 235 | struct bsc_nat_ussd_con *conn; |
| 236 | struct bsc_nat *nat; |
| 237 | struct sockaddr_in sa; |
| 238 | socklen_t sa_len = sizeof(sa); |
| 239 | int fd; |
| 240 | |
| 241 | if (!(what & BSC_FD_READ)) |
| 242 | return 0; |
| 243 | |
| 244 | fd = accept(bfd->fd, (struct sockaddr *) &sa, &sa_len); |
| 245 | if (fd < 0) { |
| 246 | perror("accept"); |
| 247 | return fd; |
| 248 | } |
| 249 | |
| 250 | nat = (struct bsc_nat *) bfd->data; |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 251 | osmo_counter_inc(nat->stats.ussd.reconn); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 252 | |
| 253 | conn = bsc_nat_ussd_alloc(nat); |
| 254 | if (!conn) { |
| 255 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate USSD con struct.\n"); |
| 256 | close(fd); |
| 257 | return -1; |
| 258 | } |
| 259 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 260 | osmo_wqueue_init(&conn->queue, 10); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 261 | conn->queue.bfd.data = conn; |
| 262 | conn->queue.bfd.fd = fd; |
| 263 | conn->queue.bfd.when = BSC_FD_READ; |
| 264 | conn->queue.read_cb = ussd_read_cb; |
| 265 | conn->queue.write_cb = bsc_write_cb; |
| 266 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 267 | if (osmo_fd_register(&conn->queue.bfd) < 0) { |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 268 | LOGP(DNAT, LOGL_ERROR, "Failed to register USSD fd.\n"); |
| 269 | bsc_nat_ussd_destroy(conn); |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | LOGP(DNAT, LOGL_NOTICE, "USSD Connection on %d with IP: %s\n", |
| 274 | fd, inet_ntoa(sa.sin_addr)); |
| 275 | |
| 276 | /* do authentication */ |
| 277 | ussd_start_auth(conn); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | int bsc_ussd_init(struct bsc_nat *nat) |
| 282 | { |
| 283 | struct in_addr addr; |
| 284 | |
| 285 | addr.s_addr = INADDR_ANY; |
| 286 | if (nat->ussd_local) |
| 287 | inet_aton(nat->ussd_local, &addr); |
| 288 | |
| 289 | nat->ussd_listen.data = nat; |
| 290 | return make_sock(&nat->ussd_listen, IPPROTO_TCP, |
Holger Hans Peter Freyther | 0d93fb4 | 2011-04-11 10:27:10 +0200 | [diff] [blame] | 291 | ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 292 | } |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 293 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 294 | static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input) |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 295 | { |
| 296 | struct msgb *copy; |
| 297 | struct bsc_nat_ussd_con *ussd; |
| 298 | |
| 299 | if (!con->bsc->nat->ussd_con) |
| 300 | return -1; |
| 301 | |
| 302 | copy = msgb_alloc_headroom(4096, 128, "forward bts"); |
| 303 | if (!copy) { |
| 304 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 305 | return -1; |
| 306 | } |
| 307 | |
| 308 | /* copy the data into the copy */ |
| 309 | copy->l2h = msgb_put(copy, msgb_l2len(input)); |
| 310 | memcpy(copy->l2h, input->l2h, msgb_l2len(input)); |
| 311 | |
| 312 | /* send it out */ |
| 313 | ussd = con->bsc->nat->ussd_con; |
| 314 | bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP); |
| 315 | return 0; |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 318 | static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_request *req, |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 319 | struct msgb *input) |
| 320 | { |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 321 | struct msgb *msg, *copy; |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 322 | struct ipac_msgt_sccp_state *state; |
| 323 | struct bsc_nat_ussd_con *ussd; |
Holger Hans Peter Freyther | 7a0010b | 2013-04-29 20:40:44 +0200 | [diff] [blame] | 324 | uint16_t lac, ci; |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 325 | |
| 326 | if (!con->bsc->nat->ussd_con) |
| 327 | return -1; |
| 328 | |
| 329 | msg = msgb_alloc_headroom(4096, 128, "forward ussd"); |
| 330 | if (!msg) { |
| 331 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 335 | copy = msgb_alloc_headroom(4096, 128, "forward bts"); |
| 336 | if (!copy) { |
| 337 | LOGP(DNAT, LOGL_ERROR, "Allocation failed, not forwarding.\n"); |
| 338 | msgb_free(msg); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | copy->l2h = msgb_put(copy, msgb_l2len(input)); |
| 343 | memcpy(copy->l2h, input->l2h, msgb_l2len(input)); |
| 344 | |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 345 | msg->l2h = msgb_put(msg, 1); |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 346 | msg->l2h[0] = IPAC_MSGT_SCCP_OLD; |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 347 | |
| 348 | /* fill out the data */ |
| 349 | state = (struct ipac_msgt_sccp_state *) msgb_put(msg, sizeof(*state)); |
| 350 | state->trans_id = req->transaction_id; |
| 351 | state->invoke_id = req->invoke_id; |
| 352 | memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref)); |
| 353 | memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref)); |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 354 | memcpy(state->imsi, con->filter_state.imsi, strlen(con->filter_state.imsi)); |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 355 | |
Holger Hans Peter Freyther | 7a0010b | 2013-04-29 20:40:44 +0200 | [diff] [blame] | 356 | /* add additional tag/values */ |
| 357 | lac = htons(con->lac); |
| 358 | ci = htons(con->ci); |
Holger Hans Peter Freyther | a164d52 | 2013-07-03 16:18:37 +0200 | [diff] [blame] | 359 | msgb_tv_fixed_put(msg, USSD_LAC_IE, sizeof(lac), (const uint8_t *) &lac); |
| 360 | msgb_tv_fixed_put(msg, USSD_CI_IE, sizeof(ci), (const uint8_t *) &ci); |
Holger Hans Peter Freyther | 7a0010b | 2013-04-29 20:40:44 +0200 | [diff] [blame] | 361 | |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 362 | ussd = con->bsc->nat->ussd_con; |
| 363 | bsc_do_write(&ussd->queue, msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | 90bbccf | 2010-10-16 17:34:37 +0200 | [diff] [blame] | 364 | bsc_do_write(&ussd->queue, copy, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 4c401e7 | 2010-10-15 10:09:31 +0200 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Holger Hans Peter Freyther | 6fcc3a9 | 2013-08-26 14:04:43 +0200 | [diff] [blame] | 369 | int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 370 | struct msgb *msg) |
| 371 | { |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 372 | uint32_t len; |
| 373 | uint8_t msg_type; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 374 | uint8_t proto; |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 375 | uint8_t ti; |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 376 | struct gsm48_hdr *hdr48; |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 377 | struct bsc_msg_acc_lst *lst; |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 378 | struct ussd_request req; |
| 379 | |
| 380 | /* |
| 381 | * various checks to avoid the decoding work. Right now we only want to |
| 382 | * decode if the connection was created for USSD, we do have a USSD access |
| 383 | * list, a query, a IMSI and such... |
| 384 | */ |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 385 | if (con->filter_state.con_type != FLT_CON_TYPE_SSA) |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 386 | return 0; |
| 387 | |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 388 | if (!con->filter_state.imsi) |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 389 | return 0; |
| 390 | |
Holger Hans Peter Freyther | ef38e85 | 2011-04-06 11:27:52 +0200 | [diff] [blame] | 391 | /* We have not verified the IMSI yet */ |
| 392 | if (!con->authorized) |
| 393 | return 0; |
| 394 | |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 395 | if (!con->bsc->nat->ussd_lst_name) |
| 396 | return 0; |
| 397 | if (!con->bsc->nat->ussd_query) |
| 398 | return 0; |
| 399 | |
| 400 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 401 | return 0; |
| 402 | |
Harald Welte | d3fa84d | 2016-04-20 17:50:17 +0200 | [diff] [blame] | 403 | if (strlen(con->filter_state.imsi) > GSM23003_IMSI_MAX_DIGITS) |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 404 | return 0; |
| 405 | |
| 406 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 407 | if (!hdr48) |
| 408 | return 0; |
| 409 | |
Neels Hofmeyr | 531734a | 2016-03-14 16:13:24 +0100 | [diff] [blame] | 410 | proto = gsm48_hdr_pdisc(hdr48); |
| 411 | msg_type = gsm48_hdr_msg_type(hdr48); |
Neels Hofmeyr | 961bd0b | 2016-03-14 16:13:25 +0100 | [diff] [blame] | 412 | ti = gsm48_hdr_trans_id_no_ti(hdr48); |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 413 | if (proto != GSM48_PDISC_NC_SS) |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 414 | return 0; |
| 415 | |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 416 | if (msg_type == GSM0480_MTYPE_REGISTER) { |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 417 | |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 418 | /* now check if it is a IMSI we care about */ |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 419 | lst = bsc_msg_acc_lst_find(&con->bsc->nat->access_lists, |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 420 | con->bsc->nat->ussd_lst_name); |
| 421 | if (!lst) |
| 422 | return 0; |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 423 | |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 424 | if (bsc_msg_acc_lst_check_allow(lst, con->filter_state.imsi) != 0) |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 425 | return 0; |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 426 | |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 427 | /* now decode the message and see if we really want to handle it */ |
| 428 | memset(&req, 0, sizeof(req)); |
| 429 | if (gsm0480_decode_ussd_request(hdr48, len, &req) != 1) |
| 430 | return 0; |
| 431 | if (req.text[0] == 0xff) |
| 432 | return 0; |
Holger Hans Peter Freyther | 3229f44 | 2010-10-11 10:07:37 +0200 | [diff] [blame] | 433 | |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 434 | if (regexec(&con->bsc->nat->ussd_query_re, |
| 435 | req.text, 0, NULL, 0) == REG_NOMATCH) |
| 436 | return 0; |
| 437 | |
| 438 | /* found a USSD query for our subscriber */ |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 439 | LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", |
| 440 | con->filter_state.imsi); |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 441 | con->ussd_ti[ti] = 1; |
| 442 | if (forward_ussd(con, &req, msg) != 0) |
| 443 | return 0; |
| 444 | return 1; |
| 445 | } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) { |
| 446 | LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n", |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 447 | ti, con->filter_state.imsi); |
Holger Hans Peter Freyther | 123bc32 | 2011-04-16 14:06:18 +0200 | [diff] [blame] | 448 | if (forward_ussd_simple(con, msg) != 0) |
| 449 | return 0; |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | return 0; |
Holger Hans Peter Freyther | 17870cf | 2010-09-29 19:32:55 +0800 | [diff] [blame] | 454 | } |