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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 25 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 20ee312 | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 26 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 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 | 3e9a7f8 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 30 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 31 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/linuxlist.h> |
| 33 | #include <osmocom/core/talloc.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 35 | |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 69d801e | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 37 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 38 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 40 | #include <netinet/in.h> |
| 41 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 44 | static const struct rate_ctr_desc bsc_cfg_ctr_description[] = { |
Holger Hans Peter Freyther | 71d36b3 | 2010-06-17 18:31:18 +0800 | [diff] [blame] | 45 | [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "}, |
| 46 | [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "}, |
| 47 | [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "}, |
| 48 | [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."}, |
| 49 | [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "}, |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 50 | [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "}, |
| 51 | [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "}, |
| 52 | [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "}, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 53 | [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "}, |
| 54 | [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "}, |
| 55 | [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "}, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 56 | [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "}, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 57 | [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "}, |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = { |
| 61 | .group_name_prefix = "nat.bsc", |
| 62 | .group_description = "NAT BSC Statistics", |
| 63 | .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description), |
| 64 | .ctr_desc = bsc_cfg_ctr_description, |
| 65 | }; |
| 66 | |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 67 | static const struct rate_ctr_desc acc_list_ctr_description[] = { |
| 68 | [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"}, |
| 69 | [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"}, |
| 70 | }; |
| 71 | |
| 72 | static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { |
| 73 | .group_name_prefix = "nat.filter", |
| 74 | .group_description = "NAT Access-List Statistics", |
| 75 | .num_ctr = ARRAY_SIZE(acc_list_ctr_description), |
| 76 | .ctr_desc = acc_list_ctr_description, |
| 77 | }; |
| 78 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 79 | struct bsc_nat *bsc_nat_alloc(void) |
| 80 | { |
| 81 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 82 | if (!nat) |
| 83 | return NULL; |
| 84 | |
| 85 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 86 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 87 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 88 | INIT_LLIST_HEAD(&nat->access_lists); |
| 89 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 90 | nat->stats.sccp.conn = counter_alloc("nat.sccp.conn"); |
| 91 | nat->stats.sccp.calls = counter_alloc("nat.sccp.calls"); |
| 92 | nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn"); |
| 93 | nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail"); |
| 94 | nat->stats.msc.reconn = counter_alloc("nat.msc.conn"); |
Holger Hans Peter Freyther | c16c2dc | 2010-10-13 20:22:36 +0200 | [diff] [blame] | 95 | nat->stats.ussd.reconn = counter_alloc("nat.ussd.conn"); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 96 | nat->msc_ip = talloc_strdup(nat, "127.0.0.1"); |
Holger Hans Peter Freyther | 8139553 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 97 | nat->msc_port = 5000; |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 98 | nat->auth_timeout = 2; |
| 99 | nat->ping_timeout = 20; |
| 100 | nat->pong_timeout = 5; |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 101 | return nat; |
| 102 | } |
| 103 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 104 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 105 | { |
Holger Hans Peter Freyther | 3e9a7f8 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 106 | bsc_replace_string(nat, &nat->msc_ip, ip); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 109 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 110 | { |
| 111 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 112 | if (!con) |
| 113 | return NULL; |
| 114 | |
Holger Hans Peter Freyther | f8048d9 | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 115 | con->nat = nat; |
Holger Hans Peter Freyther | 8151973 | 2010-04-22 12:05:23 +0800 | [diff] [blame] | 116 | write_queue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 117 | return con; |
| 118 | } |
| 119 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 120 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token) |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 121 | { |
| 122 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 123 | if (!conf) |
| 124 | return NULL; |
| 125 | |
| 126 | conf->token = talloc_strdup(conf, token); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 127 | conf->nr = nat->num_bsc; |
| 128 | conf->nat = nat; |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 129 | conf->max_endpoints = 32; |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 130 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 131 | INIT_LLIST_HEAD(&conf->lac_list); |
| 132 | |
Holger Hans Peter Freyther | d1278c1 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 133 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 134 | ++nat->num_bsc; |
| 135 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 136 | conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr); |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 137 | if (!conf->stats.ctrg) { |
| 138 | talloc_free(conf); |
| 139 | return NULL; |
| 140 | } |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 141 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 142 | return conf; |
| 143 | } |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 144 | |
Holger Hans Peter Freyther | 9212d9d | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 145 | void bsc_config_free(struct bsc_config *cfg) |
| 146 | { |
| 147 | rate_ctr_group_free(cfg->stats.ctrg); |
| 148 | } |
| 149 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 150 | void bsc_config_add_lac(struct bsc_config *cfg, int _lac) |
| 151 | { |
| 152 | struct bsc_lac_entry *lac; |
| 153 | |
| 154 | llist_for_each_entry(lac, &cfg->lac_list, entry) |
| 155 | if (lac->lac == _lac) |
| 156 | return; |
| 157 | |
| 158 | lac = talloc_zero(cfg, struct bsc_lac_entry); |
| 159 | if (!lac) { |
| 160 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | lac->lac = _lac; |
| 165 | llist_add_tail(&lac->entry, &cfg->lac_list); |
| 166 | } |
| 167 | |
| 168 | void bsc_config_del_lac(struct bsc_config *cfg, int _lac) |
| 169 | { |
| 170 | struct bsc_lac_entry *lac; |
| 171 | |
| 172 | llist_for_each_entry(lac, &cfg->lac_list, entry) |
| 173 | if (lac->lac == _lac) { |
| 174 | llist_del(&lac->entry); |
| 175 | talloc_free(lac); |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr) |
| 181 | { |
| 182 | struct bsc_lac_entry *entry; |
| 183 | |
| 184 | llist_for_each_entry(entry, &cfg->lac_list, entry) |
| 185 | if (entry->lac == lac_nr) |
| 186 | return 1; |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 191 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 192 | { |
| 193 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 194 | sccp_src_ref_to_int(&conn->real_ref), |
| 195 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 196 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 197 | llist_del(&conn->list_entry); |
| 198 | talloc_free(conn); |
| 199 | } |
| 200 | |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 201 | 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] | 202 | { |
| 203 | struct bsc_connection *bsc; |
| 204 | int data_length; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 205 | const uint8_t *data; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 206 | struct tlv_parsed tp; |
| 207 | int i = 0; |
| 208 | |
Holger Hans Peter Freyther | 7a77369 | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 209 | *lac_out = -1; |
| 210 | |
Holger Hans Peter Freyther | e9be517 | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 211 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 212 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 216 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 217 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 218 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 223 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | a4376ad | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 224 | |
| 225 | /* No need to try a different BSS */ |
| 226 | if (data[0] == CELL_IDENT_BSS) { |
| 227 | return NULL; |
| 228 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 530c4b1 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 229 | 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] | 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | /* Currently we only handle one BSC */ |
| 234 | for (i = 1; i < data_length - 1; i += 2) { |
| 235 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 236 | *lac_out = _lac; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 237 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 47dd494 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 238 | if (!bsc->cfg) |
| 239 | continue; |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 240 | if (!bsc->authenticated) |
| 241 | continue; |
| 242 | if (!bsc_config_handles_lac(bsc->cfg, _lac)) |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 243 | continue; |
| 244 | |
| 245 | return bsc; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return NULL; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 252 | int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 253 | { |
| 254 | struct msgb *msg; |
| 255 | |
| 256 | if (length > 4096 - 128) { |
| 257 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 262 | if (!msg) { |
| 263 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | /* copy the data */ |
| 268 | msg->l3h = msgb_put(msg, length); |
| 269 | memcpy(msg->l3h, data, length); |
| 270 | |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 271 | return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 274 | 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] | 275 | { |
Holger Hans Peter Freyther | 4d44fc5 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 276 | return bsc_do_write(&bsc->write_queue, msg, proto); |
| 277 | } |
| 278 | |
| 279 | int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto) |
| 280 | { |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 281 | /* prepend the header */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 282 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | 7e8da13 | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 283 | return bsc_write_msg(queue, msg); |
| 284 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 285 | |
Holger Hans Peter Freyther | 7e8da13 | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 286 | int bsc_write_msg(struct write_queue *queue, struct msgb *msg) |
| 287 | { |
Holger Hans Peter Freyther | 4d44fc5 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 288 | if (write_queue_enqueue(queue, msg) != 0) { |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 289 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 290 | msgb_free(msg); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 296 | |
Holger Hans Peter Freyther | c1cac1e | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 297 | int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 298 | { |
| 299 | struct bsc_nat_acc_lst_entry *entry; |
| 300 | |
| 301 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 302 | if (!entry->imsi_allow) |
| 303 | continue; |
| 304 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 312 | { |
| 313 | struct bsc_nat_acc_lst_entry *entry; |
| 314 | |
| 315 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 316 | if (!entry->imsi_deny) |
| 317 | continue; |
| 318 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | return 1; |
| 323 | } |
| 324 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 325 | /* apply white/black list */ |
| 326 | static int auth_imsi(struct bsc_connection *bsc, const char *mi_string) |
| 327 | { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 328 | /* |
| 329 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 330 | * 1.) Allow directly if the IMSI is allowed at the BSC |
| 331 | * 2.) Reject if the IMSI is not allowed at the BSC |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 332 | * 3.) Reject if the IMSI not allowed at the global level. |
| 333 | * 4.) Allow directly if the IMSI is allowed at the global level |
| 334 | */ |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 335 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 336 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 337 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 338 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 339 | nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 340 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 341 | |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 342 | if (bsc_lst) { |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 343 | /* 1. BSC allow */ |
Holger Hans Peter Freyther | c1cac1e | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 344 | if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0) |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 345 | return 1; |
| 346 | |
| 347 | /* 2. BSC deny */ |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 348 | if (lst_check_deny(bsc_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 349 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 48945b1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 350 | "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 351 | rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 352 | return -2; |
| 353 | } |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 354 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /* 3. NAT deny */ |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 358 | if (nat_lst) { |
| 359 | if (lst_check_deny(nat_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 360 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 48945b1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 361 | "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 0c35b5b | 2010-10-06 00:18:20 +0800 | [diff] [blame] | 362 | rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 363 | return -3; |
| 364 | } |
| 365 | } |
| 366 | |
Holger Hans Peter Freyther | 909e61f | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 367 | return 1; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 368 | } |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 369 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 370 | static int _cr_check_loc_upd(struct bsc_connection *bsc, |
| 371 | uint8_t *data, unsigned int length, |
| 372 | char **imsi) |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 373 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 374 | uint8_t mi_type; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 375 | struct gsm48_loc_upd_req *lu; |
| 376 | char mi_string[GSM48_MI_SIZE]; |
| 377 | |
Holger Hans Peter Freyther | f830322 | 2010-05-14 19:24:06 +0800 | [diff] [blame] | 378 | if (length < sizeof(*lu)) { |
| 379 | LOGP(DNAT, LOGL_ERROR, |
| 380 | "LU does not fit. Length is %d \n", length); |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | lu = (struct gsm48_loc_upd_req *) data; |
| 385 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 386 | |
| 387 | /* |
| 388 | * We can only deal with the IMSI. This will fail for a phone that |
| 389 | * will send the TMSI of a previous network to us. |
| 390 | */ |
| 391 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 392 | return 0; |
| 393 | |
| 394 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 395 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 396 | return auth_imsi(bsc, mi_string); |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 397 | } |
| 398 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 399 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, |
| 400 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 401 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 402 | { |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 403 | static const uint32_t classmark_offset = |
| 404 | offsetof(struct gsm48_service_request, classmark); |
| 405 | |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 406 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 407 | uint8_t mi_type; |
| 408 | int rc; |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 409 | struct gsm48_service_request *req; |
| 410 | |
| 411 | /* unfortunately in Phase1 the classmark2 length is variable */ |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 412 | |
| 413 | if (length < sizeof(*req)) { |
| 414 | LOGP(DNAT, LOGL_ERROR, |
| 415 | "CM Serv Req does not fit. Length is %d\n", length); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | req = (struct gsm48_service_request *) data; |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 420 | if (req->cm_service_type == 0x8) |
| 421 | *con_type = NAT_CON_TYPE_SSA; |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 422 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 423 | length - classmark_offset, mi_string, &mi_type); |
| 424 | if (rc < 0) { |
| 425 | LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 429 | /* we have to let the TMSI or such pass */ |
| 430 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 431 | return 0; |
| 432 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 433 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 434 | return auth_imsi(bsc, mi_string); |
| 435 | } |
| 436 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 437 | static int _cr_check_pag_resp(struct bsc_connection *bsc, |
| 438 | uint8_t *data, unsigned int length, |
| 439 | char **imsi) |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 440 | { |
| 441 | struct gsm48_pag_resp *resp; |
| 442 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 443 | uint8_t mi_type; |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 444 | |
| 445 | if (length < sizeof(*resp)) { |
| 446 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | resp = (struct gsm48_pag_resp *) data; |
| 451 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 452 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 453 | return -1; |
| 454 | } |
| 455 | |
| 456 | /* we need to let it pass for now */ |
| 457 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 458 | return 0; |
| 459 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 460 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 461 | return auth_imsi(bsc, mi_string); |
| 462 | } |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 463 | |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 464 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 465 | uint8_t *data, unsigned int length, |
| 466 | struct sccp_connections *con) |
| 467 | { |
| 468 | char mi_string[GSM48_MI_SIZE]; |
| 469 | uint8_t mi_type; |
| 470 | int ret; |
| 471 | |
| 472 | if (length < 2) { |
| 473 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | if (data[0] < length - 1) { |
| 478 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 479 | return -2; |
| 480 | } |
| 481 | |
| 482 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 483 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 484 | |
| 485 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 486 | return 0; |
| 487 | |
| 488 | ret = auth_imsi(bsc, mi_string); |
| 489 | con->imsi_checked = 1; |
Holger Hans Peter Freyther | 8c78b48 | 2010-09-29 01:00:46 +0800 | [diff] [blame] | 490 | con->imsi = talloc_strdup(con, mi_string); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 491 | return ret; |
| 492 | } |
| 493 | |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 494 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 495 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 496 | struct bsc_nat_parsed *parsed, int *con_type, |
| 497 | char **imsi) |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 498 | { |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 499 | struct tlv_parsed tp; |
| 500 | struct gsm48_hdr *hdr48; |
| 501 | int hdr48_len; |
| 502 | int len; |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 503 | uint8_t msg_type; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 504 | |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 505 | *con_type = NAT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 506 | *imsi = NULL; |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 507 | |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 508 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 509 | LOGP(DNAT, LOGL_ERROR, |
| 510 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | /* the parsed has had some basic l3 length check */ |
| 515 | len = msg->l3h[1]; |
| 516 | if (msgb_l3len(msg) - 3 < len) { |
| 517 | LOGP(DNAT, LOGL_ERROR, |
| 518 | "The CR Data has not enough space...\n"); |
| 519 | return -1; |
| 520 | } |
| 521 | |
| 522 | msg->l4h = &msg->l3h[3]; |
| 523 | len -= 1; |
| 524 | |
| 525 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 526 | |
| 527 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 528 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 529 | return -1; |
| 530 | } |
| 531 | |
| 532 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 533 | |
| 534 | if (hdr48_len < sizeof(*hdr48)) { |
| 535 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 536 | return -1; |
| 537 | } |
| 538 | |
| 539 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 540 | |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 541 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 87ef2f2 | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 542 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 543 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 544 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 545 | return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 87ef2f2 | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 546 | } else if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 547 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 548 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 549 | return _cr_check_cm_serv_req(bsc, &hdr48->data[0], |
| 550 | hdr48_len - sizeof(*hdr48), |
| 551 | con_type, imsi); |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 552 | } else if (hdr48->proto_discr == GSM48_PDISC_RR && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 553 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 554 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 555 | return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 556 | } else { |
Holger Hans Peter Freyther | 1f38747 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 557 | /* We only want to filter the above, let other things pass */ |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 558 | *con_type = NAT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | 1f38747 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 559 | return 0; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 560 | } |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 561 | } |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 562 | |
Holger Hans Peter Freyther | a396757 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 563 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, |
| 564 | struct msgb *msg, uint32_t *len) |
| 565 | { |
| 566 | /* gsm_type is actually the size of the dtap */ |
| 567 | *len = parsed->gsm_type; |
| 568 | if (*len < msgb_l3len(msg) - 3) { |
| 569 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | if (*len < sizeof(struct gsm48_hdr)) { |
| 574 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | msg->l4h = &msg->l3h[3]; |
| 579 | return (struct gsm48_hdr *) msg->l4h; |
| 580 | } |
| 581 | |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 582 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 583 | struct sccp_connections *con, struct bsc_nat_parsed *parsed) |
| 584 | { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 585 | uint32_t len; |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 586 | uint8_t msg_type; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 587 | struct gsm48_hdr *hdr48; |
| 588 | |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 589 | if (con->imsi_checked) |
| 590 | return 0; |
| 591 | |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 592 | /* only care about DTAP messages */ |
| 593 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 594 | return 0; |
| 595 | |
Holger Hans Peter Freyther | a396757 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 596 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 597 | if (!hdr48) |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 598 | return -1; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 599 | |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 600 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 601 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 602 | msg_type == GSM48_MT_MM_ID_RESP) { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 603 | return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con); |
| 604 | } else { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 605 | return 0; |
| 606 | } |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 607 | } |
| 608 | |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 609 | void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
| 610 | { |
| 611 | if (*imsi) { |
| 612 | talloc_free(*imsi); |
| 613 | *imsi = NULL; |
| 614 | } |
| 615 | regfree(reg); |
| 616 | |
| 617 | if (argc > 0) { |
| 618 | *imsi = talloc_strdup(ctx, argv[0]); |
| 619 | regcomp(reg, argv[0], 0); |
| 620 | } |
| 621 | } |
Holger Hans Peter Freyther | 234d312 | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 622 | |
| 623 | static const char *con_types [] = { |
| 624 | [NAT_CON_TYPE_NONE] = "n/a", |
| 625 | [NAT_CON_TYPE_LU] = "Location Update", |
| 626 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 627 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 628 | [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 629 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 234d312 | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 630 | [NAT_CON_TYPE_OTHER] = "Other", |
| 631 | }; |
| 632 | |
| 633 | const char *bsc_con_type_to_string(int type) |
| 634 | { |
| 635 | return con_types[type]; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 636 | } |
| 637 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 638 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 639 | { |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 640 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 641 | |
| 642 | if (!name) |
| 643 | return NULL; |
| 644 | |
| 645 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 646 | if (strcmp(lst->name, name) == 0) |
| 647 | return lst; |
| 648 | |
| 649 | return NULL; |
| 650 | } |
| 651 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 652 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 653 | { |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 654 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 655 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 656 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 657 | if (lst) |
| 658 | return lst; |
| 659 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 660 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 661 | if (!lst) { |
| 662 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 663 | return NULL; |
| 664 | } |
| 665 | |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 666 | /* TODO: get the index right */ |
| 667 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 668 | if (!lst->stats) { |
| 669 | talloc_free(lst); |
| 670 | return NULL; |
| 671 | } |
| 672 | |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 673 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 674 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 26c3a35 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 675 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 676 | return lst; |
Holger Hans Peter Freyther | e4900a0 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 677 | } |
| 678 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 679 | void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) |
Holger Hans Peter Freyther | e4900a0 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 680 | { |
| 681 | llist_del(&lst->list); |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 682 | rate_ctr_group_free(lst->stats); |
Holger Hans Peter Freyther | e4900a0 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 683 | talloc_free(lst); |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 687 | { |
| 688 | struct bsc_nat_acc_lst_entry *entry; |
| 689 | |
| 690 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 691 | if (!entry) |
| 692 | return NULL; |
| 693 | |
Holger Hans Peter Freyther | 26c3a35 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 694 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 695 | return entry; |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 696 | } |
Holger Hans Peter Freyther | 20ee312 | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 697 | |
| 698 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 699 | { |
| 700 | return nat->msc_con->is_connected; |
| 701 | } |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 702 | |
| 703 | static const int con_to_ctr[] = { |
| 704 | [NAT_CON_TYPE_NONE] = -1, |
| 705 | [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, |
| 706 | [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, |
| 707 | [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 708 | [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 709 | [NAT_CON_TYPE_LOCAL_REJECT] = -1, |
| 710 | [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, |
| 711 | }; |
| 712 | |
| 713 | int bsc_conn_type_to_ctr(struct sccp_connections *conn) |
| 714 | { |
| 715 | return con_to_ctr[conn->con_type]; |
| 716 | } |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 717 | |
| 718 | int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 719 | { |
| 720 | int rc; |
| 721 | |
| 722 | rc = write(bfd->fd, msg->data, msg->len); |
| 723 | if (rc != msg->len) |
| 724 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 725 | |
| 726 | return rc; |
| 727 | } |
| 728 | |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 729 | /** |
| 730 | * Rewrite non global numbers... according to rules based on the IMSI |
| 731 | */ |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 732 | struct msgb *bsc_nat_rewrite_setup(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi) |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 733 | { |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 734 | struct tlv_parsed tp; |
| 735 | struct gsm48_hdr *hdr48; |
| 736 | uint32_t len; |
| 737 | uint8_t msg_type; |
| 738 | unsigned int payload_len; |
| 739 | struct gsm_mncc_number called; |
| 740 | struct msg_entry *entry; |
| 741 | char *new_number = NULL; |
| 742 | struct msgb *out, *sccp; |
| 743 | uint8_t *outptr; |
| 744 | const uint8_t *msgptr; |
| 745 | int sec_len; |
| 746 | |
| 747 | if (!imsi || strlen(imsi) < 5) |
| 748 | return msg; |
| 749 | |
| 750 | if (!nat->num_rewr) |
| 751 | return msg; |
| 752 | |
| 753 | /* only care about DTAP messages */ |
| 754 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 755 | return msg; |
| 756 | if (!parsed->dest_local_ref) |
| 757 | return msg; |
| 758 | |
| 759 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 760 | if (!hdr48) |
| 761 | return msg; |
| 762 | |
| 763 | msg_type = hdr48->msg_type & 0xbf; |
| 764 | if (hdr48->proto_discr != GSM48_PDISC_CC || |
| 765 | msg_type != GSM48_MT_CC_SETUP) |
| 766 | return msg; |
| 767 | |
| 768 | /* decode and rewrite the message */ |
| 769 | payload_len = len - sizeof(*hdr48); |
| 770 | tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0); |
| 771 | |
| 772 | /* no number, well let us ignore it */ |
| 773 | if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) |
| 774 | return msg; |
| 775 | |
| 776 | memset(&called, 0, sizeof(called)); |
| 777 | gsm48_decode_called(&called, |
| 778 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1); |
| 779 | |
| 780 | /* check if it looks international and stop */ |
Holger Hans Peter Freyther | 643931d | 2010-10-27 10:24:34 +0200 | [diff] [blame] | 781 | if (called.plan != 1) |
| 782 | return msg; |
| 783 | if (called.type == 1) |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 784 | return msg; |
| 785 | if (strncmp(called.number, "00", 2) == 0) |
| 786 | return msg; |
| 787 | |
| 788 | /* need to find a replacement and then fix it */ |
| 789 | llist_for_each_entry(entry, &nat->num_rewr->entry, list) { |
| 790 | regex_t reg; |
| 791 | regmatch_t matches[2]; |
| 792 | |
Holger Hans Peter Freyther | d414c06 | 2010-10-28 13:51:28 +0200 | [diff] [blame] | 793 | if (entry->mcc[0] != '*' && strncmp(entry->mcc, imsi, 3) != 0) |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 794 | continue; |
Holger Hans Peter Freyther | d414c06 | 2010-10-28 13:51:28 +0200 | [diff] [blame] | 795 | if (entry->mnc[0] != '*' && strncmp(entry->mnc, imsi + 3, 2) != 0) |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 796 | continue; |
| 797 | |
Holger Hans Peter Freyther | 1400310 | 2010-10-27 10:24:12 +0200 | [diff] [blame] | 798 | if (entry->text[0] == '+') { |
| 799 | LOGP(DNAT, LOGL_ERROR, |
| 800 | "Plus is not allowed in the number"); |
| 801 | continue; |
| 802 | } |
| 803 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 804 | /* We have an entry for the IMSI. Need to match now */ |
| 805 | if (regcomp(®, entry->option, REG_EXTENDED) != 0) { |
| 806 | LOGP(DNAT, LOGL_ERROR, |
| 807 | "Regexp '%s' is not valid.\n", entry->option); |
| 808 | continue; |
| 809 | } |
| 810 | |
| 811 | /* this regexp matches... */ |
| 812 | if (regexec(®, called.number, 2, matches, 0) == 0 && |
| 813 | matches[1].rm_eo != -1) |
| 814 | new_number = talloc_asprintf(msg, "%s%s", |
| 815 | entry->text, |
| 816 | &called.number[matches[1].rm_so]); |
| 817 | regfree(®); |
| 818 | |
| 819 | if (new_number) |
| 820 | break; |
| 821 | } |
| 822 | |
| 823 | if (!new_number) { |
| 824 | LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n"); |
| 825 | return msg; |
| 826 | } |
| 827 | |
Holger Hans Peter Freyther | 4446b3b | 2011-02-10 11:41:49 +0100 | [diff] [blame] | 828 | if (strlen(new_number) > sizeof(called.number)) { |
| 829 | LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n"); |
| 830 | talloc_free(new_number); |
| 831 | return msg; |
| 832 | } |
| 833 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 834 | /* |
| 835 | * Need to create a new message now based on the old onew |
| 836 | * with a new number. We can sadly not patch this in place |
| 837 | * so we will need to regenerate it. |
| 838 | */ |
| 839 | |
| 840 | out = msgb_alloc_headroom(4096, 128, "changed-setup"); |
| 841 | if (!out) { |
| 842 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 843 | talloc_free(new_number); |
| 844 | return msg; |
| 845 | } |
| 846 | |
| 847 | /* copy the header */ |
| 848 | outptr = msgb_put(out, sizeof(*hdr48)); |
| 849 | memcpy(outptr, hdr48, sizeof(*hdr48)); |
| 850 | |
| 851 | /* copy everything up to the number */ |
| 852 | sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0]; |
| 853 | outptr = msgb_put(out, sec_len); |
| 854 | memcpy(outptr, &hdr48->data[0], sec_len); |
| 855 | |
| 856 | /* create the new number */ |
Holger Hans Peter Freyther | 91fa850 | 2011-02-10 11:46:51 +0100 | [diff] [blame] | 857 | if (strncmp(new_number, "00", 2) == 0) { |
| 858 | called.type = 1; |
| 859 | strncpy(called.number, new_number + 2, sizeof(called.number)); |
| 860 | } else { |
| 861 | strncpy(called.number, new_number, sizeof(called.number)); |
| 862 | } |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 863 | gsm48_encode_called(out, &called); |
| 864 | |
| 865 | /* copy thre rest */ |
| 866 | msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) + |
| 867 | TLVP_LEN(&tp, GSM48_IE_CALLED_BCD); |
| 868 | sec_len = payload_len - (msgptr - &hdr48->data[0]); |
| 869 | outptr = msgb_put(out, sec_len); |
| 870 | memcpy(outptr, msgptr, sec_len); |
| 871 | |
| 872 | /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */ |
| 873 | gsm0808_prepend_dtap_header(out, 0); |
| 874 | sccp = sccp_create_dt1(parsed->dest_local_ref, out->data, out->len); |
| 875 | if (!sccp) { |
| 876 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 877 | talloc_free(new_number); |
| 878 | talloc_free(out); |
| 879 | return msg; |
| 880 | } |
| 881 | |
| 882 | ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP); |
| 883 | |
| 884 | /* give up memory, we are done */ |
| 885 | talloc_free(new_number); |
| 886 | /* the parsed hangs off from msg but it needs to survive */ |
| 887 | talloc_steal(sccp, parsed); |
| 888 | msgb_free(msg); |
| 889 | msgb_free(out); |
| 890 | out = NULL; |
| 891 | return sccp; |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 892 | } |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 893 | |