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