Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <openbsc/bsc_nat.h> |
| 26 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 27 | #include <openbsc/bssap.h> |
| 28 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 29 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 30 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 31 | #include <osmocore/linuxlist.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 32 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 1395948 | 2010-06-15 18:50:57 +0800 | [diff] [blame] | 33 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 35 | #include <sccp/sccp.h> |
| 36 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 37 | #include <netinet/in.h> |
| 38 | #include <arpa/inet.h> |
| 39 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 40 | struct bsc_nat *bsc_nat_alloc(void) |
| 41 | { |
| 42 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 43 | if (!nat) |
| 44 | return NULL; |
| 45 | |
| 46 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 47 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 48 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 49 | nat->stats.sccp.conn = counter_alloc("nat.sccp.conn"); |
| 50 | nat->stats.sccp.calls = counter_alloc("nat.sccp.calls"); |
| 51 | nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn"); |
| 52 | nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail"); |
| 53 | nat->stats.msc.reconn = counter_alloc("nat.msc.conn"); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 54 | nat->msc_ip = talloc_strdup(nat, "127.0.0.1"); |
Holger Hans Peter Freyther | 8139553 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 55 | nat->msc_port = 5000; |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 56 | return nat; |
| 57 | } |
| 58 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 59 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 60 | { |
| 61 | if (nat->msc_ip) |
| 62 | talloc_free(nat->msc_ip); |
| 63 | nat->msc_ip = talloc_strdup(nat, ip); |
| 64 | } |
| 65 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 66 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 67 | { |
| 68 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 69 | if (!con) |
| 70 | return NULL; |
| 71 | |
Holger Hans Peter Freyther | f8048d9 | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 72 | con->nat = nat; |
Holger Hans Peter Freyther | 8151973 | 2010-04-22 12:05:23 +0800 | [diff] [blame] | 73 | write_queue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 74 | return con; |
| 75 | } |
| 76 | |
| 77 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac) |
| 78 | { |
| 79 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 80 | if (!conf) |
| 81 | return NULL; |
| 82 | |
| 83 | conf->token = talloc_strdup(conf, token); |
| 84 | conf->lac = lac; |
| 85 | conf->nr = nat->num_bsc; |
| 86 | conf->nat = nat; |
| 87 | |
Holger Hans Peter Freyther | d1278c1 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 88 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 89 | ++nat->num_bsc; |
| 90 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 91 | conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn"); |
| 92 | conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls"); |
| 93 | conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects"); |
| 94 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 95 | return conf; |
| 96 | } |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 97 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 98 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 99 | { |
| 100 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 101 | sccp_src_ref_to_int(&conn->real_ref), |
| 102 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 103 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 104 | llist_del(&conn->list_entry); |
| 105 | talloc_free(conn); |
| 106 | } |
| 107 | |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 108 | struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out) |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 109 | { |
| 110 | struct bsc_connection *bsc; |
| 111 | int data_length; |
| 112 | const u_int8_t *data; |
| 113 | struct tlv_parsed tp; |
| 114 | int i = 0; |
| 115 | |
Holger Hans Peter Freyther | 7a77369 | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 116 | *lac_out = -1; |
| 117 | |
Holger Hans Peter Freyther | e9be517 | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 118 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 119 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
| 120 | return NULL; |
| 121 | } |
| 122 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 123 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 124 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 125 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 130 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | a4376ad | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 131 | |
| 132 | /* No need to try a different BSS */ |
| 133 | if (data[0] == CELL_IDENT_BSS) { |
| 134 | return NULL; |
| 135 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 530c4b1 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 136 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]); |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /* Currently we only handle one BSC */ |
| 141 | for (i = 1; i < data_length - 1; i += 2) { |
| 142 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 143 | *lac_out = _lac; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 144 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 47dd494 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 145 | if (!bsc->cfg) |
| 146 | continue; |
| 147 | if (!bsc->authenticated || _lac != bsc->cfg->lac) |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 148 | continue; |
| 149 | |
| 150 | return bsc; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return NULL; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length) |
| 158 | { |
| 159 | struct msgb *msg; |
| 160 | |
| 161 | if (length > 4096 - 128) { |
| 162 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 167 | if (!msg) { |
| 168 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | /* copy the data */ |
| 173 | msg->l3h = msgb_put(msg, length); |
| 174 | memcpy(msg->l3h, data, length); |
| 175 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 176 | return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 179 | int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 180 | { |
| 181 | /* prepend the header */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 182 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 183 | |
| 184 | if (write_queue_enqueue(&bsc->write_queue, msg) != 0) { |
| 185 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 186 | msgb_free(msg); |
| 187 | return -1; |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |