Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 5 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 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 | 090a4d8 | 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 | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 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 | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 25 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 26 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 29 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 30 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 31 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/linuxlist.h> |
| 33 | #include <osmocom/core/talloc.h> |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 35 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | e36be81 | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 37 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 38 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 40 | #include <netinet/in.h> |
| 41 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 42 | #include <unistd.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 43 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 44 | static const struct rate_ctr_desc bsc_cfg_ctr_description[] = { |
Holger Hans Peter Freyther | 275f27a | 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 | a67fc6f | 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 | 2e72807 | 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 | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 56 | [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "}, |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 57 | [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "}, |
Holger Hans Peter Freyther | ccefba6 | 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 | 39f1880 | 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 | 090a4d8 | 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 | |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 85 | nat->main_dest = talloc_zero(nat, struct bsc_msc_dest); |
| 86 | if (!nat->main_dest) { |
| 87 | talloc_free(nat); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 91 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 92 | INIT_LLIST_HEAD(&nat->bsc_connections); |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 93 | INIT_LLIST_HEAD(&nat->paging_groups); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 94 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 95 | INIT_LLIST_HEAD(&nat->access_lists); |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 96 | INIT_LLIST_HEAD(&nat->dests); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 97 | |
Pablo Neira Ayuso | 1c45074 | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 98 | nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn"); |
| 99 | nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls"); |
| 100 | nat->stats.bsc.reconn = osmo_counter_alloc("nat.bsc.conn"); |
| 101 | nat->stats.bsc.auth_fail = osmo_counter_alloc("nat.bsc.auth_fail"); |
| 102 | nat->stats.msc.reconn = osmo_counter_alloc("nat.msc.conn"); |
| 103 | nat->stats.ussd.reconn = osmo_counter_alloc("nat.ussd.conn"); |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 104 | nat->auth_timeout = 2; |
| 105 | nat->ping_timeout = 20; |
| 106 | nat->pong_timeout = 5; |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 107 | |
| 108 | llist_add(&nat->main_dest->list, &nat->dests); |
| 109 | nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1"); |
| 110 | nat->main_dest->port = 5000; |
| 111 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 112 | return nat; |
| 113 | } |
| 114 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 115 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 116 | { |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 117 | bsc_replace_string(nat, &nat->main_dest->ip, ip); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 118 | } |
| 119 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 120 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 121 | { |
| 122 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 123 | if (!con) |
| 124 | return NULL; |
| 125 | |
Holger Hans Peter Freyther | 72deaea | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 126 | con->nat = nat; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 127 | osmo_wqueue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 128 | return con; |
| 129 | } |
| 130 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 131 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token) |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 132 | { |
| 133 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 134 | if (!conf) |
| 135 | return NULL; |
| 136 | |
| 137 | conf->token = talloc_strdup(conf, token); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 138 | conf->nr = nat->num_bsc; |
| 139 | conf->nat = nat; |
Holger Hans Peter Freyther | b28f5a0 | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 140 | conf->max_endpoints = 32; |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 141 | conf->paging_group = PAGIN_GROUP_UNASSIGNED; |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 142 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 143 | INIT_LLIST_HEAD(&conf->lac_list); |
| 144 | |
Holger Hans Peter Freyther | 085ea30 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 145 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 146 | ++nat->num_bsc; |
| 147 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 148 | conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr); |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 149 | if (!conf->stats.ctrg) { |
| 150 | talloc_free(conf); |
| 151 | return NULL; |
| 152 | } |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 153 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 154 | return conf; |
| 155 | } |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 156 | |
Holger Hans Peter Freyther | 187221e | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 157 | void bsc_config_free(struct bsc_config *cfg) |
| 158 | { |
| 159 | rate_ctr_group_free(cfg->stats.ctrg); |
| 160 | } |
| 161 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 162 | static void _add_lac(void *ctx, struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 163 | { |
| 164 | struct bsc_lac_entry *lac; |
| 165 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 166 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 167 | if (lac->lac == _lac) |
| 168 | return; |
| 169 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 170 | lac = talloc_zero(ctx, struct bsc_lac_entry); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 171 | if (!lac) { |
| 172 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | lac->lac = _lac; |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 177 | llist_add_tail(&lac->entry, list); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 180 | static void _del_lac(struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 181 | { |
| 182 | struct bsc_lac_entry *lac; |
| 183 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 184 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 185 | if (lac->lac == _lac) { |
| 186 | llist_del(&lac->entry); |
| 187 | talloc_free(lac); |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 192 | void bsc_config_add_lac(struct bsc_config *cfg, int _lac) |
| 193 | { |
| 194 | _add_lac(cfg, &cfg->lac_list, _lac); |
| 195 | } |
| 196 | |
| 197 | void bsc_config_del_lac(struct bsc_config *cfg, int _lac) |
| 198 | { |
| 199 | _del_lac(&cfg->lac_list, _lac); |
| 200 | } |
| 201 | |
| 202 | struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group) |
| 203 | { |
| 204 | struct bsc_nat_paging_group *pgroup; |
| 205 | |
| 206 | pgroup = talloc_zero(nat, struct bsc_nat_paging_group); |
| 207 | if (!pgroup) { |
| 208 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n"); |
| 209 | return NULL; |
| 210 | } |
| 211 | |
| 212 | pgroup->nr = group; |
| 213 | INIT_LLIST_HEAD(&pgroup->lists); |
| 214 | llist_add_tail(&pgroup->entry, &nat->paging_groups); |
| 215 | return pgroup; |
| 216 | } |
| 217 | |
| 218 | void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup) |
| 219 | { |
| 220 | llist_del(&pgroup->entry); |
| 221 | talloc_free(pgroup); |
| 222 | } |
| 223 | |
| 224 | struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group) |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 225 | { |
| 226 | struct bsc_nat_paging_group *pgroup; |
| 227 | |
| 228 | llist_for_each_entry(pgroup, &nat->paging_groups, entry) |
| 229 | if (pgroup->nr == group) |
| 230 | return pgroup; |
| 231 | |
| 232 | return NULL; |
| 233 | } |
| 234 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 235 | void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 236 | { |
| 237 | _add_lac(pgroup, &pgroup->lists, lac); |
| 238 | } |
| 239 | |
| 240 | void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 241 | { |
| 242 | _del_lac(&pgroup->lists, lac); |
| 243 | } |
| 244 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 245 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr) |
| 246 | { |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 247 | struct bsc_nat_paging_group *pgroup; |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 248 | struct bsc_lac_entry *entry; |
| 249 | |
| 250 | llist_for_each_entry(entry, &cfg->lac_list, entry) |
| 251 | if (entry->lac == lac_nr) |
| 252 | return 1; |
| 253 | |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 254 | /* now lookup the paging group */ |
| 255 | pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group); |
| 256 | if (!pgroup) |
| 257 | return 0; |
| 258 | |
| 259 | llist_for_each_entry(entry, &pgroup->lists, entry) |
| 260 | if (entry->lac == lac_nr) |
| 261 | return 1; |
| 262 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 263 | return 0; |
| 264 | } |
| 265 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 266 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 267 | { |
| 268 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 269 | sccp_src_ref_to_int(&conn->real_ref), |
| 270 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 271 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 272 | llist_del(&conn->list_entry); |
| 273 | talloc_free(conn); |
| 274 | } |
| 275 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 276 | |
| 277 | int bsc_nat_find_paging(struct msgb *msg, |
| 278 | const uint8_t **out_data, int *out_leng) |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 279 | { |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 280 | int data_length; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 281 | const uint8_t *data; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 282 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | 05c77ab | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 283 | |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 284 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 285 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 286 | return -1; |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 289 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 290 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 291 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 292 | return -2; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 296 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 297 | |
| 298 | /* No need to try a different BSS */ |
| 299 | if (data[0] == CELL_IDENT_BSS) { |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 300 | return -3; |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 301 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 60ebf14 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 302 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 303 | return -4; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 306 | *out_data = &data[1]; |
| 307 | *out_leng = data_length - 1; |
| 308 | return 0; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 311 | int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 312 | { |
| 313 | struct msgb *msg; |
| 314 | |
| 315 | if (length > 4096 - 128) { |
| 316 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 321 | if (!msg) { |
| 322 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | /* copy the data */ |
| 327 | msg->l3h = msgb_put(msg, length); |
| 328 | memcpy(msg->l3h, data, length); |
| 329 | |
Holger Hans Peter Freyther | a63d35c | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 330 | return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 333 | int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 334 | { |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 335 | return bsc_do_write(&bsc->write_queue, msg, proto); |
| 336 | } |
| 337 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 338 | int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 339 | { |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 340 | /* prepend the header */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 341 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 342 | return bsc_write_msg(queue, msg); |
| 343 | } |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 344 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 345 | int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg) |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 346 | { |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 347 | if (osmo_wqueue_enqueue(queue, msg) != 0) { |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 348 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 349 | msgb_free(msg); |
| 350 | return -1; |
| 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 355 | |
Holger Hans Peter Freyther | 01ae681 | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 356 | int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 357 | { |
| 358 | struct bsc_nat_acc_lst_entry *entry; |
| 359 | |
| 360 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 361 | if (!entry->imsi_allow) |
| 362 | continue; |
| 363 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | return 1; |
| 368 | } |
| 369 | |
| 370 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 371 | { |
| 372 | struct bsc_nat_acc_lst_entry *entry; |
| 373 | |
| 374 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 375 | if (!entry->imsi_deny) |
| 376 | continue; |
| 377 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | return 1; |
| 382 | } |
| 383 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 384 | /* apply white/black list */ |
| 385 | static int auth_imsi(struct bsc_connection *bsc, const char *mi_string) |
| 386 | { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 387 | /* |
| 388 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 389 | * 1.) Allow directly if the IMSI is allowed at the BSC |
| 390 | * 2.) Reject if the IMSI is not allowed at the BSC |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 391 | * 3.) Reject if the IMSI not allowed at the global level. |
| 392 | * 4.) Allow directly if the IMSI is allowed at the global level |
| 393 | */ |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 394 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 395 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 396 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 397 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 398 | nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 399 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 400 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 401 | if (bsc_lst) { |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 402 | /* 1. BSC allow */ |
Holger Hans Peter Freyther | 01ae681 | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 403 | if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0) |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 404 | return 1; |
| 405 | |
| 406 | /* 2. BSC deny */ |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 407 | if (lst_check_deny(bsc_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 408 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 409 | "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 410 | rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 411 | return -2; |
| 412 | } |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 413 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* 3. NAT deny */ |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 417 | if (nat_lst) { |
| 418 | if (lst_check_deny(nat_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 419 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 420 | "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 3cc2e2f | 2010-10-06 00:18:20 +0800 | [diff] [blame] | 421 | rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 422 | return -3; |
| 423 | } |
| 424 | } |
| 425 | |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 426 | return 1; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 427 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 428 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 429 | static int _cr_check_loc_upd(struct bsc_connection *bsc, |
| 430 | uint8_t *data, unsigned int length, |
| 431 | char **imsi) |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 432 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 433 | uint8_t mi_type; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 434 | struct gsm48_loc_upd_req *lu; |
| 435 | char mi_string[GSM48_MI_SIZE]; |
| 436 | |
Holger Hans Peter Freyther | 540f4c8 | 2010-05-14 19:24:06 +0800 | [diff] [blame] | 437 | if (length < sizeof(*lu)) { |
| 438 | LOGP(DNAT, LOGL_ERROR, |
| 439 | "LU does not fit. Length is %d \n", length); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | lu = (struct gsm48_loc_upd_req *) data; |
| 444 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 445 | |
| 446 | /* |
| 447 | * We can only deal with the IMSI. This will fail for a phone that |
| 448 | * will send the TMSI of a previous network to us. |
| 449 | */ |
| 450 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 451 | return 0; |
| 452 | |
| 453 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 454 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 455 | return auth_imsi(bsc, mi_string); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 456 | } |
| 457 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 458 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, |
| 459 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 460 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 461 | { |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 462 | static const uint32_t classmark_offset = |
| 463 | offsetof(struct gsm48_service_request, classmark); |
| 464 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 465 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 466 | uint8_t mi_type; |
| 467 | int rc; |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 468 | struct gsm48_service_request *req; |
| 469 | |
| 470 | /* unfortunately in Phase1 the classmark2 length is variable */ |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 471 | |
| 472 | if (length < sizeof(*req)) { |
| 473 | LOGP(DNAT, LOGL_ERROR, |
| 474 | "CM Serv Req does not fit. Length is %d\n", length); |
| 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | req = (struct gsm48_service_request *) data; |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 479 | if (req->cm_service_type == 0x8) |
| 480 | *con_type = NAT_CON_TYPE_SSA; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 481 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 482 | length - classmark_offset, mi_string, &mi_type); |
| 483 | if (rc < 0) { |
| 484 | LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 485 | return -1; |
| 486 | } |
| 487 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 488 | /* we have to let the TMSI or such pass */ |
| 489 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 490 | return 0; |
| 491 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 492 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 493 | return auth_imsi(bsc, mi_string); |
| 494 | } |
| 495 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 496 | static int _cr_check_pag_resp(struct bsc_connection *bsc, |
| 497 | uint8_t *data, unsigned int length, |
| 498 | char **imsi) |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 499 | { |
| 500 | struct gsm48_pag_resp *resp; |
| 501 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 502 | uint8_t mi_type; |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 503 | |
| 504 | if (length < sizeof(*resp)) { |
| 505 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | resp = (struct gsm48_pag_resp *) data; |
| 510 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 511 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 512 | return -1; |
| 513 | } |
| 514 | |
| 515 | /* we need to let it pass for now */ |
| 516 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 517 | return 0; |
| 518 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 519 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 520 | return auth_imsi(bsc, mi_string); |
| 521 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 522 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 523 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 524 | uint8_t *data, unsigned int length, |
| 525 | struct sccp_connections *con) |
| 526 | { |
| 527 | char mi_string[GSM48_MI_SIZE]; |
| 528 | uint8_t mi_type; |
| 529 | int ret; |
| 530 | |
| 531 | if (length < 2) { |
| 532 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | if (data[0] < length - 1) { |
| 537 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 538 | return -2; |
| 539 | } |
| 540 | |
| 541 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 542 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 543 | |
| 544 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 545 | return 0; |
| 546 | |
| 547 | ret = auth_imsi(bsc, mi_string); |
| 548 | con->imsi_checked = 1; |
Holger Hans Peter Freyther | 4a98ae9 | 2010-09-29 01:00:46 +0800 | [diff] [blame] | 549 | con->imsi = talloc_strdup(con, mi_string); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 550 | return ret; |
| 551 | } |
| 552 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 553 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 554 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 555 | struct bsc_nat_parsed *parsed, int *con_type, |
| 556 | char **imsi) |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 557 | { |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 558 | struct tlv_parsed tp; |
| 559 | struct gsm48_hdr *hdr48; |
| 560 | int hdr48_len; |
| 561 | int len; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 562 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 563 | |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 564 | *con_type = NAT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 565 | *imsi = NULL; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 566 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 567 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 568 | LOGP(DNAT, LOGL_ERROR, |
| 569 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | /* the parsed has had some basic l3 length check */ |
| 574 | len = msg->l3h[1]; |
| 575 | if (msgb_l3len(msg) - 3 < len) { |
| 576 | LOGP(DNAT, LOGL_ERROR, |
| 577 | "The CR Data has not enough space...\n"); |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | msg->l4h = &msg->l3h[3]; |
| 582 | len -= 1; |
| 583 | |
| 584 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 585 | |
| 586 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 587 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 588 | return -1; |
| 589 | } |
| 590 | |
| 591 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 592 | |
| 593 | if (hdr48_len < sizeof(*hdr48)) { |
| 594 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 599 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 600 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 601 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 602 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 603 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 604 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 605 | return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 606 | } else if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 607 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 608 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 609 | return _cr_check_cm_serv_req(bsc, &hdr48->data[0], |
| 610 | hdr48_len - sizeof(*hdr48), |
| 611 | con_type, imsi); |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 612 | } else if (proto == GSM48_PDISC_RR && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 613 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 614 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 615 | return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 616 | } else { |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 617 | /* We only want to filter the above, let other things pass */ |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 618 | *con_type = NAT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 619 | return 0; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 620 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 621 | } |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 622 | |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 623 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, |
| 624 | struct msgb *msg, uint32_t *len) |
| 625 | { |
| 626 | /* gsm_type is actually the size of the dtap */ |
| 627 | *len = parsed->gsm_type; |
| 628 | if (*len < msgb_l3len(msg) - 3) { |
| 629 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | if (*len < sizeof(struct gsm48_hdr)) { |
| 634 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | msg->l4h = &msg->l3h[3]; |
| 639 | return (struct gsm48_hdr *) msg->l4h; |
| 640 | } |
| 641 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 642 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 643 | struct sccp_connections *con, struct bsc_nat_parsed *parsed) |
| 644 | { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 645 | uint32_t len; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 646 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 647 | struct gsm48_hdr *hdr48; |
| 648 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 649 | if (con->imsi_checked) |
| 650 | return 0; |
| 651 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 652 | /* only care about DTAP messages */ |
| 653 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 654 | return 0; |
| 655 | |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 656 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 657 | if (!hdr48) |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 658 | return -1; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 659 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 660 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 661 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 662 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 663 | msg_type == GSM48_MT_MM_ID_RESP) { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 664 | return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con); |
| 665 | } else { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 666 | return 0; |
| 667 | } |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 668 | } |
| 669 | |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 670 | int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 671 | { |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 672 | int ret; |
| 673 | |
| 674 | ret = 0; |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 675 | if (*imsi) { |
| 676 | talloc_free(*imsi); |
| 677 | *imsi = NULL; |
| 678 | } |
| 679 | regfree(reg); |
| 680 | |
| 681 | if (argc > 0) { |
| 682 | *imsi = talloc_strdup(ctx, argv[0]); |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 683 | ret = regcomp(reg, argv[0], 0); |
| 684 | |
| 685 | /* handle compilation failures */ |
| 686 | if (ret != 0) { |
| 687 | talloc_free(*imsi); |
| 688 | *imsi = NULL; |
| 689 | } |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 690 | } |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 691 | |
| 692 | return ret; |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 693 | } |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 694 | |
| 695 | static const char *con_types [] = { |
| 696 | [NAT_CON_TYPE_NONE] = "n/a", |
| 697 | [NAT_CON_TYPE_LU] = "Location Update", |
| 698 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 699 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 700 | [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 701 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 702 | [NAT_CON_TYPE_OTHER] = "Other", |
| 703 | }; |
| 704 | |
| 705 | const char *bsc_con_type_to_string(int type) |
| 706 | { |
| 707 | return con_types[type]; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 708 | } |
| 709 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 710 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 711 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 712 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 713 | |
| 714 | if (!name) |
| 715 | return NULL; |
| 716 | |
| 717 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 718 | if (strcmp(lst->name, name) == 0) |
| 719 | return lst; |
| 720 | |
| 721 | return NULL; |
| 722 | } |
| 723 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 724 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 725 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 726 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 727 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 728 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 729 | if (lst) |
| 730 | return lst; |
| 731 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 732 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 733 | if (!lst) { |
| 734 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 735 | return NULL; |
| 736 | } |
| 737 | |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 738 | /* TODO: get the index right */ |
| 739 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 740 | if (!lst->stats) { |
| 741 | talloc_free(lst); |
| 742 | return NULL; |
| 743 | } |
| 744 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 745 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 746 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 747 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 748 | return lst; |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 749 | } |
| 750 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 751 | void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 752 | { |
| 753 | llist_del(&lst->list); |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 754 | rate_ctr_group_free(lst->stats); |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 755 | talloc_free(lst); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 759 | { |
| 760 | struct bsc_nat_acc_lst_entry *entry; |
| 761 | |
| 762 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 763 | if (!entry) |
| 764 | return NULL; |
| 765 | |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 766 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 767 | return entry; |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 768 | } |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 769 | |
| 770 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 771 | { |
| 772 | return nat->msc_con->is_connected; |
| 773 | } |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 774 | |
| 775 | static const int con_to_ctr[] = { |
| 776 | [NAT_CON_TYPE_NONE] = -1, |
| 777 | [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, |
| 778 | [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, |
| 779 | [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 780 | [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 781 | [NAT_CON_TYPE_LOCAL_REJECT] = -1, |
| 782 | [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, |
| 783 | }; |
| 784 | |
| 785 | int bsc_conn_type_to_ctr(struct sccp_connections *conn) |
| 786 | { |
| 787 | return con_to_ctr[conn->con_type]; |
| 788 | } |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 789 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 790 | int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 791 | { |
| 792 | int rc; |
| 793 | |
| 794 | rc = write(bfd->fd, msg->data, msg->len); |
| 795 | if (rc != msg->len) |
| 796 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 797 | |
| 798 | return rc; |
| 799 | } |
| 800 | |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 801 | /** |
| 802 | * Rewrite non global numbers... according to rules based on the IMSI |
| 803 | */ |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 804 | 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 | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 805 | { |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 806 | struct tlv_parsed tp; |
| 807 | struct gsm48_hdr *hdr48; |
| 808 | uint32_t len; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 809 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 810 | unsigned int payload_len; |
| 811 | struct gsm_mncc_number called; |
Pablo Neira Ayuso | 1a91e67 | 2011-05-07 13:11:20 +0200 | [diff] [blame^] | 812 | struct osmo_config_entry *entry; |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 813 | char *new_number = NULL; |
| 814 | struct msgb *out, *sccp; |
| 815 | uint8_t *outptr; |
| 816 | const uint8_t *msgptr; |
| 817 | int sec_len; |
| 818 | |
| 819 | if (!imsi || strlen(imsi) < 5) |
| 820 | return msg; |
| 821 | |
| 822 | if (!nat->num_rewr) |
| 823 | return msg; |
| 824 | |
| 825 | /* only care about DTAP messages */ |
| 826 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 827 | return msg; |
| 828 | if (!parsed->dest_local_ref) |
| 829 | return msg; |
| 830 | |
| 831 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 832 | if (!hdr48) |
| 833 | return msg; |
| 834 | |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 835 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 836 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | b14ece1 | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 837 | if (proto != GSM48_PDISC_CC || |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 838 | msg_type != GSM48_MT_CC_SETUP) |
| 839 | return msg; |
| 840 | |
| 841 | /* decode and rewrite the message */ |
| 842 | payload_len = len - sizeof(*hdr48); |
| 843 | tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0); |
| 844 | |
| 845 | /* no number, well let us ignore it */ |
| 846 | if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) |
| 847 | return msg; |
| 848 | |
| 849 | memset(&called, 0, sizeof(called)); |
| 850 | gsm48_decode_called(&called, |
| 851 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1); |
| 852 | |
| 853 | /* check if it looks international and stop */ |
Holger Hans Peter Freyther | 22f7f54 | 2010-10-27 10:24:34 +0200 | [diff] [blame] | 854 | if (called.plan != 1) |
| 855 | return msg; |
| 856 | if (called.type == 1) |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 857 | return msg; |
| 858 | if (strncmp(called.number, "00", 2) == 0) |
| 859 | return msg; |
| 860 | |
| 861 | /* need to find a replacement and then fix it */ |
| 862 | llist_for_each_entry(entry, &nat->num_rewr->entry, list) { |
| 863 | regex_t reg; |
| 864 | regmatch_t matches[2]; |
| 865 | |
Holger Hans Peter Freyther | 999c5eb | 2010-10-28 13:51:28 +0200 | [diff] [blame] | 866 | if (entry->mcc[0] != '*' && strncmp(entry->mcc, imsi, 3) != 0) |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 867 | continue; |
Holger Hans Peter Freyther | 999c5eb | 2010-10-28 13:51:28 +0200 | [diff] [blame] | 868 | if (entry->mnc[0] != '*' && strncmp(entry->mnc, imsi + 3, 2) != 0) |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 869 | continue; |
| 870 | |
Holger Hans Peter Freyther | 2091261 | 2010-10-27 10:24:12 +0200 | [diff] [blame] | 871 | if (entry->text[0] == '+') { |
| 872 | LOGP(DNAT, LOGL_ERROR, |
| 873 | "Plus is not allowed in the number"); |
| 874 | continue; |
| 875 | } |
| 876 | |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 877 | /* We have an entry for the IMSI. Need to match now */ |
| 878 | if (regcomp(®, entry->option, REG_EXTENDED) != 0) { |
| 879 | LOGP(DNAT, LOGL_ERROR, |
| 880 | "Regexp '%s' is not valid.\n", entry->option); |
| 881 | continue; |
| 882 | } |
| 883 | |
| 884 | /* this regexp matches... */ |
| 885 | if (regexec(®, called.number, 2, matches, 0) == 0 && |
| 886 | matches[1].rm_eo != -1) |
| 887 | new_number = talloc_asprintf(msg, "%s%s", |
| 888 | entry->text, |
| 889 | &called.number[matches[1].rm_so]); |
| 890 | regfree(®); |
| 891 | |
| 892 | if (new_number) |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | if (!new_number) { |
| 897 | LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n"); |
| 898 | return msg; |
| 899 | } |
| 900 | |
Holger Hans Peter Freyther | 0518e72 | 2011-02-10 11:41:49 +0100 | [diff] [blame] | 901 | if (strlen(new_number) > sizeof(called.number)) { |
| 902 | LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n"); |
| 903 | talloc_free(new_number); |
| 904 | return msg; |
| 905 | } |
| 906 | |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 907 | /* |
| 908 | * Need to create a new message now based on the old onew |
| 909 | * with a new number. We can sadly not patch this in place |
| 910 | * so we will need to regenerate it. |
| 911 | */ |
| 912 | |
| 913 | out = msgb_alloc_headroom(4096, 128, "changed-setup"); |
| 914 | if (!out) { |
| 915 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 916 | talloc_free(new_number); |
| 917 | return msg; |
| 918 | } |
| 919 | |
| 920 | /* copy the header */ |
| 921 | outptr = msgb_put(out, sizeof(*hdr48)); |
| 922 | memcpy(outptr, hdr48, sizeof(*hdr48)); |
| 923 | |
| 924 | /* copy everything up to the number */ |
| 925 | sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0]; |
| 926 | outptr = msgb_put(out, sec_len); |
| 927 | memcpy(outptr, &hdr48->data[0], sec_len); |
| 928 | |
| 929 | /* create the new number */ |
Holger Hans Peter Freyther | da580ec | 2011-02-10 11:46:51 +0100 | [diff] [blame] | 930 | if (strncmp(new_number, "00", 2) == 0) { |
| 931 | called.type = 1; |
| 932 | strncpy(called.number, new_number + 2, sizeof(called.number)); |
| 933 | } else { |
| 934 | strncpy(called.number, new_number, sizeof(called.number)); |
| 935 | } |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 936 | gsm48_encode_called(out, &called); |
| 937 | |
| 938 | /* copy thre rest */ |
| 939 | msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) + |
| 940 | TLVP_LEN(&tp, GSM48_IE_CALLED_BCD); |
| 941 | sec_len = payload_len - (msgptr - &hdr48->data[0]); |
| 942 | outptr = msgb_put(out, sec_len); |
| 943 | memcpy(outptr, msgptr, sec_len); |
| 944 | |
| 945 | /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */ |
| 946 | gsm0808_prepend_dtap_header(out, 0); |
| 947 | sccp = sccp_create_dt1(parsed->dest_local_ref, out->data, out->len); |
| 948 | if (!sccp) { |
| 949 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 950 | talloc_free(new_number); |
| 951 | talloc_free(out); |
| 952 | return msg; |
| 953 | } |
| 954 | |
| 955 | ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP); |
| 956 | |
| 957 | /* give up memory, we are done */ |
| 958 | talloc_free(new_number); |
| 959 | /* the parsed hangs off from msg but it needs to survive */ |
| 960 | talloc_steal(sccp, parsed); |
| 961 | msgb_free(msg); |
| 962 | msgb_free(out); |
| 963 | out = NULL; |
| 964 | return sccp; |
Holger Hans Peter Freyther | c3ac978 | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 965 | } |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 966 | |