Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | e188010 | 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 | dcf8a7d | 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 | 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 | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 37 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
Holger Hans Peter Freyther | 69d801e | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 38 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 39 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 43 | #include <unistd.h> |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 45 | static const struct rate_ctr_desc bsc_cfg_ctr_description[] = { |
Holger Hans Peter Freyther | 71d36b3 | 2010-06-17 18:31:18 +0800 | [diff] [blame] | 46 | [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "}, |
| 47 | [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "}, |
| 48 | [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "}, |
| 49 | [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."}, |
| 50 | [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "}, |
Holger Hans Peter Freyther | ee88496 | 2010-09-25 17:58:22 +0800 | [diff] [blame] | 51 | [BCFG_CTR_REJECTED_CR] = { "rejected.cr", "Rejected CR due filter "}, |
| 52 | [BCFG_CTR_REJECTED_MSG] = { "rejected.msg", "Rejected MSG due filter "}, |
| 53 | [BCFG_CTR_ILL_PACKET] = { "rejected.ill", "Rejected due parse error "}, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 54 | [BCFG_CTR_CON_TYPE_LU] = { "conn.lu", "Conn Location Update "}, |
| 55 | [BCFG_CTR_CON_CMSERV_RQ] = { "conn.rq", "Conn CM Service Req "}, |
| 56 | [BCFG_CTR_CON_PAG_RESP] = { "conn.pag", "Conn Paging Response "}, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 57 | [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "}, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 58 | [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "}, |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = { |
| 62 | .group_name_prefix = "nat.bsc", |
| 63 | .group_description = "NAT BSC Statistics", |
| 64 | .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description), |
| 65 | .ctr_desc = bsc_cfg_ctr_description, |
| 66 | }; |
| 67 | |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 68 | static const struct rate_ctr_desc acc_list_ctr_description[] = { |
| 69 | [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"}, |
| 70 | [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"}, |
| 71 | }; |
| 72 | |
| 73 | static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { |
| 74 | .group_name_prefix = "nat.filter", |
| 75 | .group_description = "NAT Access-List Statistics", |
| 76 | .num_ctr = ARRAY_SIZE(acc_list_ctr_description), |
| 77 | .ctr_desc = acc_list_ctr_description, |
| 78 | }; |
| 79 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 80 | struct bsc_nat *bsc_nat_alloc(void) |
| 81 | { |
| 82 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 83 | if (!nat) |
| 84 | return NULL; |
| 85 | |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 86 | nat->main_dest = talloc_zero(nat, struct bsc_msc_dest); |
| 87 | if (!nat->main_dest) { |
| 88 | talloc_free(nat); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 92 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 93 | INIT_LLIST_HEAD(&nat->bsc_connections); |
Holger Hans Peter Freyther | 474698a | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 94 | INIT_LLIST_HEAD(&nat->paging_groups); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 95 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 96 | INIT_LLIST_HEAD(&nat->access_lists); |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 97 | INIT_LLIST_HEAD(&nat->dests); |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 98 | INIT_LLIST_HEAD(&nat->num_rewr); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 99 | INIT_LLIST_HEAD(&nat->smsc_rewr); |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 100 | INIT_LLIST_HEAD(&nat->tpdest_match); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 101 | |
Pablo Neira Ayuso | dfb342c | 2011-05-06 12:13:10 +0200 | [diff] [blame] | 102 | nat->stats.sccp.conn = osmo_counter_alloc("nat.sccp.conn"); |
| 103 | nat->stats.sccp.calls = osmo_counter_alloc("nat.sccp.calls"); |
| 104 | nat->stats.bsc.reconn = osmo_counter_alloc("nat.bsc.conn"); |
| 105 | nat->stats.bsc.auth_fail = osmo_counter_alloc("nat.bsc.auth_fail"); |
| 106 | nat->stats.msc.reconn = osmo_counter_alloc("nat.msc.conn"); |
| 107 | nat->stats.ussd.reconn = osmo_counter_alloc("nat.ussd.conn"); |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 108 | nat->auth_timeout = 2; |
| 109 | nat->ping_timeout = 20; |
| 110 | nat->pong_timeout = 5; |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 111 | |
| 112 | llist_add(&nat->main_dest->list, &nat->dests); |
| 113 | nat->main_dest->ip = talloc_strdup(nat, "127.0.0.1"); |
| 114 | nat->main_dest->port = 5000; |
| 115 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 116 | return nat; |
| 117 | } |
| 118 | |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 119 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 120 | { |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 121 | bsc_replace_string(nat, &nat->main_dest->ip, ip); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 124 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 125 | { |
| 126 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 127 | if (!con) |
| 128 | return NULL; |
| 129 | |
Holger Hans Peter Freyther | f8048d9 | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 130 | con->nat = nat; |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 131 | osmo_wqueue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 132 | return con; |
| 133 | } |
| 134 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 135 | 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] | 136 | { |
| 137 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 138 | if (!conf) |
| 139 | return NULL; |
| 140 | |
| 141 | conf->token = talloc_strdup(conf, token); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 142 | conf->nr = nat->num_bsc; |
| 143 | conf->nat = nat; |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 144 | conf->max_endpoints = 32; |
Holger Hans Peter Freyther | 474698a | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 145 | conf->paging_group = PAGIN_GROUP_UNASSIGNED; |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 146 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 147 | INIT_LLIST_HEAD(&conf->lac_list); |
| 148 | |
Holger Hans Peter Freyther | d1278c1 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 149 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 150 | ++nat->num_bsc; |
| 151 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 152 | 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] | 153 | if (!conf->stats.ctrg) { |
| 154 | talloc_free(conf); |
| 155 | return NULL; |
| 156 | } |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 157 | |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 158 | return conf; |
| 159 | } |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 160 | |
Holger Hans Peter Freyther | 9212d9d | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 161 | void bsc_config_free(struct bsc_config *cfg) |
| 162 | { |
| 163 | rate_ctr_group_free(cfg->stats.ctrg); |
| 164 | } |
| 165 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 166 | static void _add_lac(void *ctx, struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 167 | { |
| 168 | struct bsc_lac_entry *lac; |
| 169 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 170 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 171 | if (lac->lac == _lac) |
| 172 | return; |
| 173 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 174 | lac = talloc_zero(ctx, struct bsc_lac_entry); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 175 | if (!lac) { |
| 176 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | lac->lac = _lac; |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 181 | llist_add_tail(&lac->entry, list); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 184 | static void _del_lac(struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 185 | { |
| 186 | struct bsc_lac_entry *lac; |
| 187 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 188 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 189 | if (lac->lac == _lac) { |
| 190 | llist_del(&lac->entry); |
| 191 | talloc_free(lac); |
| 192 | return; |
| 193 | } |
| 194 | } |
| 195 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 196 | void bsc_config_add_lac(struct bsc_config *cfg, int _lac) |
| 197 | { |
| 198 | _add_lac(cfg, &cfg->lac_list, _lac); |
| 199 | } |
| 200 | |
| 201 | void bsc_config_del_lac(struct bsc_config *cfg, int _lac) |
| 202 | { |
| 203 | _del_lac(&cfg->lac_list, _lac); |
| 204 | } |
| 205 | |
| 206 | struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group) |
| 207 | { |
| 208 | struct bsc_nat_paging_group *pgroup; |
| 209 | |
| 210 | pgroup = talloc_zero(nat, struct bsc_nat_paging_group); |
| 211 | if (!pgroup) { |
| 212 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n"); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | pgroup->nr = group; |
| 217 | INIT_LLIST_HEAD(&pgroup->lists); |
| 218 | llist_add_tail(&pgroup->entry, &nat->paging_groups); |
| 219 | return pgroup; |
| 220 | } |
| 221 | |
| 222 | void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup) |
| 223 | { |
| 224 | llist_del(&pgroup->entry); |
| 225 | talloc_free(pgroup); |
| 226 | } |
| 227 | |
| 228 | struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group) |
Holger Hans Peter Freyther | 474698a | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 229 | { |
| 230 | struct bsc_nat_paging_group *pgroup; |
| 231 | |
| 232 | llist_for_each_entry(pgroup, &nat->paging_groups, entry) |
| 233 | if (pgroup->nr == group) |
| 234 | return pgroup; |
| 235 | |
| 236 | return NULL; |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 6860c44 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 239 | void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 240 | { |
| 241 | _add_lac(pgroup, &pgroup->lists, lac); |
| 242 | } |
| 243 | |
| 244 | void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 245 | { |
| 246 | _del_lac(&pgroup->lists, lac); |
| 247 | } |
| 248 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 249 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr) |
| 250 | { |
Holger Hans Peter Freyther | 474698a | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 251 | struct bsc_nat_paging_group *pgroup; |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 252 | struct bsc_lac_entry *entry; |
| 253 | |
| 254 | llist_for_each_entry(entry, &cfg->lac_list, entry) |
| 255 | if (entry->lac == lac_nr) |
| 256 | return 1; |
| 257 | |
Holger Hans Peter Freyther | 474698a | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 258 | /* now lookup the paging group */ |
| 259 | pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group); |
| 260 | if (!pgroup) |
| 261 | return 0; |
| 262 | |
| 263 | llist_for_each_entry(entry, &pgroup->lists, entry) |
| 264 | if (entry->lac == lac_nr) |
| 265 | return 1; |
| 266 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 267 | return 0; |
| 268 | } |
| 269 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 270 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 271 | { |
| 272 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 273 | sccp_src_ref_to_int(&conn->real_ref), |
| 274 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 275 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 276 | llist_del(&conn->list_entry); |
| 277 | talloc_free(conn); |
| 278 | } |
| 279 | |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 280 | |
| 281 | int bsc_nat_find_paging(struct msgb *msg, |
| 282 | const uint8_t **out_data, int *out_leng) |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 283 | { |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 284 | int data_length; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 285 | const uint8_t *data; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 286 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | 7a77369 | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 287 | |
Holger Hans Peter Freyther | e9be517 | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 288 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 289 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 290 | return -1; |
Holger Hans Peter Freyther | e9be517 | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 293 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 294 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 295 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 296 | return -2; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 300 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | a4376ad | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 301 | |
| 302 | /* No need to try a different BSS */ |
| 303 | if (data[0] == CELL_IDENT_BSS) { |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 304 | return -3; |
Holger Hans Peter Freyther | a4376ad | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 305 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 530c4b1 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 306 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]); |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 307 | return -4; |
Holger Hans Peter Freyther | bae9da4 | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 308 | } |
| 309 | |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 310 | *out_data = &data[1]; |
| 311 | *out_leng = data_length - 1; |
| 312 | return 0; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | struct msgb *msg; |
| 318 | |
| 319 | if (length > 4096 - 128) { |
| 320 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 325 | if (!msg) { |
| 326 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | /* copy the data */ |
| 331 | msg->l3h = msgb_put(msg, length); |
| 332 | memcpy(msg->l3h, data, length); |
| 333 | |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 334 | return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 337 | 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] | 338 | { |
Holger Hans Peter Freyther | 4d44fc5 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 339 | return bsc_do_write(&bsc->write_queue, msg, proto); |
| 340 | } |
| 341 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 342 | int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | 4d44fc5 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 343 | { |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 344 | /* prepend the header */ |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 345 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | 7e8da13 | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 346 | return bsc_write_msg(queue, msg); |
| 347 | } |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 348 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 349 | int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg) |
Holger Hans Peter Freyther | 7e8da13 | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 350 | { |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 351 | if (osmo_wqueue_enqueue(queue, msg) != 0) { |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 352 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 353 | msgb_free(msg); |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 359 | |
Holger Hans Peter Freyther | c1cac1e | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 360 | 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] | 361 | { |
| 362 | struct bsc_nat_acc_lst_entry *entry; |
| 363 | |
| 364 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 365 | if (!entry->imsi_allow) |
| 366 | continue; |
| 367 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 375 | { |
| 376 | struct bsc_nat_acc_lst_entry *entry; |
| 377 | |
| 378 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 379 | if (!entry->imsi_deny) |
| 380 | continue; |
| 381 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) |
| 382 | return 0; |
| 383 | } |
| 384 | |
| 385 | return 1; |
| 386 | } |
| 387 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 388 | /* apply white/black list */ |
| 389 | static int auth_imsi(struct bsc_connection *bsc, const char *mi_string) |
| 390 | { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 391 | /* |
| 392 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 393 | * 1.) Allow directly if the IMSI is allowed at the BSC |
| 394 | * 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] | 395 | * 3.) Reject if the IMSI not allowed at the global level. |
| 396 | * 4.) Allow directly if the IMSI is allowed at the global level |
| 397 | */ |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 398 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 399 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 400 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 401 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 402 | 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] | 403 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 404 | |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 405 | if (bsc_lst) { |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 406 | /* 1. BSC allow */ |
Holger Hans Peter Freyther | c1cac1e | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 407 | 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] | 408 | return 1; |
| 409 | |
| 410 | /* 2. BSC deny */ |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 411 | if (lst_check_deny(bsc_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 412 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 48945b1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 413 | "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] | 414 | 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] | 415 | return -2; |
| 416 | } |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 417 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* 3. NAT deny */ |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 421 | if (nat_lst) { |
| 422 | if (lst_check_deny(nat_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 423 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 48945b1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 424 | "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] | 425 | 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] | 426 | return -3; |
| 427 | } |
| 428 | } |
| 429 | |
Holger Hans Peter Freyther | 909e61f | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 430 | return 1; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 431 | } |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 432 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 433 | static int _cr_check_loc_upd(struct bsc_connection *bsc, |
| 434 | uint8_t *data, unsigned int length, |
| 435 | char **imsi) |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 436 | { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 437 | uint8_t mi_type; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 438 | struct gsm48_loc_upd_req *lu; |
| 439 | char mi_string[GSM48_MI_SIZE]; |
| 440 | |
Holger Hans Peter Freyther | f830322 | 2010-05-14 19:24:06 +0800 | [diff] [blame] | 441 | if (length < sizeof(*lu)) { |
| 442 | LOGP(DNAT, LOGL_ERROR, |
| 443 | "LU does not fit. Length is %d \n", length); |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | lu = (struct gsm48_loc_upd_req *) data; |
| 448 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 449 | |
| 450 | /* |
| 451 | * We can only deal with the IMSI. This will fail for a phone that |
| 452 | * will send the TMSI of a previous network to us. |
| 453 | */ |
| 454 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 455 | return 0; |
| 456 | |
| 457 | 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] | 458 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 459 | return auth_imsi(bsc, mi_string); |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 460 | } |
| 461 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 462 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, |
| 463 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 464 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 465 | { |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 466 | static const uint32_t classmark_offset = |
| 467 | offsetof(struct gsm48_service_request, classmark); |
| 468 | |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 469 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 470 | uint8_t mi_type; |
| 471 | int rc; |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 472 | struct gsm48_service_request *req; |
| 473 | |
| 474 | /* unfortunately in Phase1 the classmark2 length is variable */ |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 475 | |
| 476 | if (length < sizeof(*req)) { |
| 477 | LOGP(DNAT, LOGL_ERROR, |
| 478 | "CM Serv Req does not fit. Length is %d\n", length); |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | req = (struct gsm48_service_request *) data; |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 483 | if (req->cm_service_type == 0x8) |
| 484 | *con_type = NAT_CON_TYPE_SSA; |
Holger Hans Peter Freyther | 66e1ef7 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 485 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 486 | length - classmark_offset, mi_string, &mi_type); |
| 487 | if (rc < 0) { |
| 488 | 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] | 489 | return -1; |
| 490 | } |
| 491 | |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 492 | /* we have to let the TMSI or such pass */ |
| 493 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 494 | return 0; |
| 495 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 496 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | bcb32a4 | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 497 | return auth_imsi(bsc, mi_string); |
| 498 | } |
| 499 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 500 | static int _cr_check_pag_resp(struct bsc_connection *bsc, |
| 501 | uint8_t *data, unsigned int length, |
| 502 | char **imsi) |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 503 | { |
| 504 | struct gsm48_pag_resp *resp; |
| 505 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 506 | uint8_t mi_type; |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 507 | |
| 508 | if (length < sizeof(*resp)) { |
| 509 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 510 | return -1; |
| 511 | } |
| 512 | |
| 513 | resp = (struct gsm48_pag_resp *) data; |
| 514 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 515 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | /* we need to let it pass for now */ |
| 520 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 521 | return 0; |
| 522 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 523 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 524 | return auth_imsi(bsc, mi_string); |
| 525 | } |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 526 | |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 527 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 528 | uint8_t *data, unsigned int length, |
| 529 | struct sccp_connections *con) |
| 530 | { |
| 531 | char mi_string[GSM48_MI_SIZE]; |
| 532 | uint8_t mi_type; |
| 533 | int ret; |
| 534 | |
| 535 | if (length < 2) { |
| 536 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | if (data[0] < length - 1) { |
| 541 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 542 | return -2; |
| 543 | } |
| 544 | |
| 545 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 546 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 547 | |
| 548 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 549 | return 0; |
| 550 | |
| 551 | ret = auth_imsi(bsc, mi_string); |
| 552 | con->imsi_checked = 1; |
Holger Hans Peter Freyther | 8c78b48 | 2010-09-29 01:00:46 +0800 | [diff] [blame] | 553 | con->imsi = talloc_strdup(con, mi_string); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 554 | return ret; |
| 555 | } |
| 556 | |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 557 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 558 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 559 | struct bsc_nat_parsed *parsed, int *con_type, |
| 560 | char **imsi) |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 561 | { |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 562 | struct tlv_parsed tp; |
| 563 | struct gsm48_hdr *hdr48; |
| 564 | int hdr48_len; |
| 565 | int len; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 566 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 567 | |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 568 | *con_type = NAT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 569 | *imsi = NULL; |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 570 | |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 571 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 572 | LOGP(DNAT, LOGL_ERROR, |
| 573 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | /* the parsed has had some basic l3 length check */ |
| 578 | len = msg->l3h[1]; |
| 579 | if (msgb_l3len(msg) - 3 < len) { |
| 580 | LOGP(DNAT, LOGL_ERROR, |
| 581 | "The CR Data has not enough space...\n"); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | msg->l4h = &msg->l3h[3]; |
| 586 | len -= 1; |
| 587 | |
| 588 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 589 | |
| 590 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 591 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 592 | return -1; |
| 593 | } |
| 594 | |
| 595 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 596 | |
| 597 | if (hdr48_len < sizeof(*hdr48)) { |
| 598 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 599 | return -1; |
| 600 | } |
| 601 | |
| 602 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 603 | |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 604 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 605 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 606 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 607 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 608 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 609 | return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 610 | } else if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 611 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 612 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 613 | return _cr_check_cm_serv_req(bsc, &hdr48->data[0], |
| 614 | hdr48_len - sizeof(*hdr48), |
| 615 | con_type, imsi); |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 616 | } else if (proto == GSM48_PDISC_RR && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 617 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 618 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 619 | 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] | 620 | } else { |
Holger Hans Peter Freyther | 1f38747 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 621 | /* 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] | 622 | *con_type = NAT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | 1f38747 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 623 | return 0; |
Holger Hans Peter Freyther | 290ed9a | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 624 | } |
Holger Hans Peter Freyther | b4af5c9 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 625 | } |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 626 | |
Holger Hans Peter Freyther | a396757 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 627 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, |
| 628 | struct msgb *msg, uint32_t *len) |
| 629 | { |
| 630 | /* gsm_type is actually the size of the dtap */ |
| 631 | *len = parsed->gsm_type; |
| 632 | if (*len < msgb_l3len(msg) - 3) { |
| 633 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | if (*len < sizeof(struct gsm48_hdr)) { |
| 638 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 639 | return NULL; |
| 640 | } |
| 641 | |
| 642 | msg->l4h = &msg->l3h[3]; |
| 643 | return (struct gsm48_hdr *) msg->l4h; |
| 644 | } |
| 645 | |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 646 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 647 | struct sccp_connections *con, struct bsc_nat_parsed *parsed) |
| 648 | { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 649 | uint32_t len; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 650 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 651 | struct gsm48_hdr *hdr48; |
| 652 | |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 653 | if (con->imsi_checked) |
| 654 | return 0; |
| 655 | |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 656 | /* only care about DTAP messages */ |
| 657 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 658 | return 0; |
| 659 | |
Holger Hans Peter Freyther | a396757 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 660 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 661 | if (!hdr48) |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 662 | return -1; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 663 | |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 664 | proto = hdr48->proto_discr & 0x0f; |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 665 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 5cde92c | 2011-04-13 18:56:13 +0200 | [diff] [blame] | 666 | if (proto == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | 11ebe1b | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 667 | msg_type == GSM48_MT_MM_ID_RESP) { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 668 | return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con); |
| 669 | } else { |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 670 | return 0; |
| 671 | } |
Holger Hans Peter Freyther | 74e0a1b | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 672 | } |
| 673 | |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 674 | int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 675 | { |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 676 | int ret; |
| 677 | |
| 678 | ret = 0; |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 679 | if (*imsi) { |
| 680 | talloc_free(*imsi); |
| 681 | *imsi = NULL; |
| 682 | } |
| 683 | regfree(reg); |
| 684 | |
| 685 | if (argc > 0) { |
| 686 | *imsi = talloc_strdup(ctx, argv[0]); |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 687 | ret = regcomp(reg, argv[0], 0); |
| 688 | |
| 689 | /* handle compilation failures */ |
| 690 | if (ret != 0) { |
| 691 | talloc_free(*imsi); |
| 692 | *imsi = NULL; |
| 693 | } |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 694 | } |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 695 | |
| 696 | return ret; |
Holger Hans Peter Freyther | 12dc89a | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 697 | } |
Holger Hans Peter Freyther | 234d312 | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 698 | |
| 699 | static const char *con_types [] = { |
| 700 | [NAT_CON_TYPE_NONE] = "n/a", |
| 701 | [NAT_CON_TYPE_LU] = "Location Update", |
| 702 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 703 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 704 | [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", |
Holger Hans Peter Freyther | b71c23b | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 705 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 234d312 | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 706 | [NAT_CON_TYPE_OTHER] = "Other", |
| 707 | }; |
| 708 | |
| 709 | const char *bsc_con_type_to_string(int type) |
| 710 | { |
| 711 | return con_types[type]; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 712 | } |
| 713 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 714 | 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] | 715 | { |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 716 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 717 | |
| 718 | if (!name) |
| 719 | return NULL; |
| 720 | |
| 721 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 722 | if (strcmp(lst->name, name) == 0) |
| 723 | return lst; |
| 724 | |
| 725 | return NULL; |
| 726 | } |
| 727 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 728 | 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] | 729 | { |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 730 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 731 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 732 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 733 | if (lst) |
| 734 | return lst; |
| 735 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 736 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 737 | if (!lst) { |
| 738 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 739 | return NULL; |
| 740 | } |
| 741 | |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 742 | /* TODO: get the index right */ |
| 743 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 744 | if (!lst->stats) { |
| 745 | talloc_free(lst); |
| 746 | return NULL; |
| 747 | } |
| 748 | |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 749 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 750 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 26c3a35 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 751 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 752 | return lst; |
Holger Hans Peter Freyther | e4900a0 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 753 | } |
| 754 | |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 755 | 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] | 756 | { |
| 757 | llist_del(&lst->list); |
Holger Hans Peter Freyther | 2f1a984 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 758 | rate_ctr_group_free(lst->stats); |
Holger Hans Peter Freyther | e4900a0 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 759 | talloc_free(lst); |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 763 | { |
| 764 | struct bsc_nat_acc_lst_entry *entry; |
| 765 | |
| 766 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 767 | if (!entry) |
| 768 | return NULL; |
| 769 | |
Holger Hans Peter Freyther | 26c3a35 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 770 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 771 | return entry; |
Holger Hans Peter Freyther | b2c38eb | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 772 | } |
Holger Hans Peter Freyther | 20ee312 | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 773 | |
| 774 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 775 | { |
| 776 | return nat->msc_con->is_connected; |
| 777 | } |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 778 | |
| 779 | static const int con_to_ctr[] = { |
| 780 | [NAT_CON_TYPE_NONE] = -1, |
| 781 | [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, |
| 782 | [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, |
| 783 | [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, |
Holger Hans Peter Freyther | 74dc303 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 784 | [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, |
Holger Hans Peter Freyther | 463dc62 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 785 | [NAT_CON_TYPE_LOCAL_REJECT] = -1, |
| 786 | [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, |
| 787 | }; |
| 788 | |
| 789 | int bsc_conn_type_to_ctr(struct sccp_connections *conn) |
| 790 | { |
| 791 | return con_to_ctr[conn->con_type]; |
| 792 | } |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 793 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 794 | int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
Holger Hans Peter Freyther | 69cfa17 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 795 | { |
| 796 | int rc; |
| 797 | |
| 798 | rc = write(bfd->fd, msg->data, msg->len); |
| 799 | if (rc != msg->len) |
| 800 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 801 | |
| 802 | return rc; |
| 803 | } |
| 804 | |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 805 | static char *rewrite_non_international(struct bsc_nat *nat, void *ctx, const char *imsi, |
| 806 | struct gsm_mncc_number *called) |
| 807 | { |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 808 | struct bsc_nat_num_rewr_entry *entry; |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 809 | char *new_number = NULL; |
| 810 | |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 811 | if (llist_empty(&nat->num_rewr)) |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 812 | return NULL; |
| 813 | |
| 814 | if (called->plan != 1) |
| 815 | return NULL; |
| 816 | if (called->type == 1) |
| 817 | return NULL; |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 818 | |
| 819 | /* need to find a replacement and then fix it */ |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 820 | llist_for_each_entry(entry, &nat->num_rewr, list) { |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 821 | regmatch_t matches[2]; |
| 822 | |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 823 | /* check the IMSI match */ |
| 824 | if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0) |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 825 | continue; |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 826 | |
| 827 | /* this regexp matches... */ |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 828 | if (regexec(&entry->num_reg, called->number, 2, matches, 0) == 0 && |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 829 | matches[1].rm_eo != -1) |
| 830 | new_number = talloc_asprintf(ctx, "%s%s", |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 831 | entry->replace, |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 832 | &called->number[matches[1].rm_so]); |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 833 | if (new_number) |
| 834 | break; |
| 835 | } |
| 836 | |
| 837 | return new_number; |
| 838 | } |
| 839 | |
| 840 | |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 841 | /** |
| 842 | * Rewrite non global numbers... according to rules based on the IMSI |
| 843 | */ |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 844 | static struct msgb *rewrite_setup(struct bsc_nat *nat, struct msgb *msg, |
| 845 | struct bsc_nat_parsed *parsed, const char *imsi, |
| 846 | struct gsm48_hdr *hdr48, const uint32_t len) |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 847 | { |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 848 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 849 | unsigned int payload_len; |
| 850 | struct gsm_mncc_number called; |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 851 | struct msgb *out; |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 852 | char *new_number = NULL; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 853 | uint8_t *outptr; |
| 854 | const uint8_t *msgptr; |
| 855 | int sec_len; |
| 856 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 857 | /* decode and rewrite the message */ |
| 858 | payload_len = len - sizeof(*hdr48); |
| 859 | tlv_parse(&tp, &gsm48_att_tlvdef, hdr48->data, payload_len, 0, 0); |
| 860 | |
| 861 | /* no number, well let us ignore it */ |
| 862 | if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 863 | return NULL; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 864 | |
| 865 | memset(&called, 0, sizeof(called)); |
| 866 | gsm48_decode_called(&called, |
| 867 | TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1); |
| 868 | |
| 869 | /* check if it looks international and stop */ |
Holger Hans Peter Freyther | 20102b0 | 2011-05-18 18:41:14 +0200 | [diff] [blame] | 870 | new_number = rewrite_non_international(nat, msg, imsi, &called); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 871 | |
| 872 | if (!new_number) { |
| 873 | LOGP(DNAT, LOGL_DEBUG, "No IMSI match found, returning message.\n"); |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 874 | return NULL; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 875 | } |
| 876 | |
Holger Hans Peter Freyther | 4446b3b | 2011-02-10 11:41:49 +0100 | [diff] [blame] | 877 | if (strlen(new_number) > sizeof(called.number)) { |
| 878 | LOGP(DNAT, LOGL_ERROR, "Number is too long for structure.\n"); |
| 879 | talloc_free(new_number); |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 880 | return NULL; |
Holger Hans Peter Freyther | 4446b3b | 2011-02-10 11:41:49 +0100 | [diff] [blame] | 881 | } |
| 882 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 883 | /* |
| 884 | * Need to create a new message now based on the old onew |
| 885 | * with a new number. We can sadly not patch this in place |
| 886 | * so we will need to regenerate it. |
| 887 | */ |
| 888 | |
| 889 | out = msgb_alloc_headroom(4096, 128, "changed-setup"); |
| 890 | if (!out) { |
| 891 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 892 | talloc_free(new_number); |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 893 | return NULL; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* copy the header */ |
| 897 | outptr = msgb_put(out, sizeof(*hdr48)); |
| 898 | memcpy(outptr, hdr48, sizeof(*hdr48)); |
| 899 | |
| 900 | /* copy everything up to the number */ |
| 901 | sec_len = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 2 - &hdr48->data[0]; |
| 902 | outptr = msgb_put(out, sec_len); |
| 903 | memcpy(outptr, &hdr48->data[0], sec_len); |
| 904 | |
| 905 | /* create the new number */ |
Holger Hans Peter Freyther | 91fa850 | 2011-02-10 11:46:51 +0100 | [diff] [blame] | 906 | if (strncmp(new_number, "00", 2) == 0) { |
| 907 | called.type = 1; |
| 908 | strncpy(called.number, new_number + 2, sizeof(called.number)); |
| 909 | } else { |
| 910 | strncpy(called.number, new_number, sizeof(called.number)); |
| 911 | } |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 912 | gsm48_encode_called(out, &called); |
| 913 | |
| 914 | /* copy thre rest */ |
| 915 | msgptr = TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) + |
| 916 | TLVP_LEN(&tp, GSM48_IE_CALLED_BCD); |
| 917 | sec_len = payload_len - (msgptr - &hdr48->data[0]); |
| 918 | outptr = msgb_put(out, sec_len); |
| 919 | memcpy(outptr, msgptr, sec_len); |
| 920 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 921 | talloc_free(new_number); |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 922 | return out; |
Holger Hans Peter Freyther | a914daf | 2010-10-21 12:12:57 +0200 | [diff] [blame] | 923 | } |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 924 | |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 925 | static struct msgb *rewrite_smsc(struct bsc_nat *nat, struct msgb *msg, |
| 926 | struct bsc_nat_parsed *parsed, const char *imsi, |
| 927 | struct gsm48_hdr *hdr48, const uint32_t len) |
| 928 | { |
| 929 | unsigned int payload_len; |
| 930 | unsigned int cp_len; |
| 931 | |
| 932 | uint8_t ref; |
| 933 | uint8_t orig_addr_len, *orig_addr_ptr; |
| 934 | uint8_t dest_addr_len, *dest_addr_ptr; |
| 935 | uint8_t data_len, *data_ptr; |
| 936 | char smsc_addr[30]; |
| 937 | uint8_t new_addr[12]; |
| 938 | |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 939 | |
| 940 | uint8_t dest_len; |
| 941 | char _dest_nr[30]; |
| 942 | char *dest_nr; |
| 943 | uint8_t dest_match = 0; |
| 944 | |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 945 | struct bsc_nat_num_rewr_entry *entry; |
| 946 | char *new_number = NULL; |
| 947 | uint8_t new_addr_len; |
| 948 | struct gsm48_hdr *new_hdr48; |
| 949 | struct msgb *out; |
| 950 | |
| 951 | payload_len = len - sizeof(*hdr48); |
| 952 | if (payload_len < 1) { |
| 953 | LOGP(DNAT, LOGL_ERROR, "SMS too short for things. %d\n", payload_len); |
| 954 | return NULL; |
| 955 | } |
| 956 | |
| 957 | cp_len = hdr48->data[0]; |
| 958 | if (payload_len + 1 < cp_len) { |
| 959 | LOGP(DNAT, LOGL_ERROR, "SMS RPDU can not fit in: %d %d\n", cp_len, payload_len); |
| 960 | return NULL; |
| 961 | } |
| 962 | |
| 963 | if (hdr48->data[1] != GSM411_MT_RP_DATA_MO) |
| 964 | return NULL; |
| 965 | |
| 966 | if (cp_len < 5) { |
| 967 | LOGP(DNAT, LOGL_ERROR, "RD-DATA can not fit in the CP len: %d\n", cp_len); |
| 968 | return NULL; |
| 969 | } |
| 970 | |
| 971 | ref = hdr48->data[2]; |
| 972 | orig_addr_len = hdr48->data[3]; |
| 973 | orig_addr_ptr = &hdr48->data[4]; |
| 974 | |
| 975 | /* the +1 is for checking if the following element has some space */ |
| 976 | if (cp_len < 3 + orig_addr_len + 1) { |
| 977 | LOGP(DNAT, LOGL_ERROR, "RP-Originator addr does not fit: %d\n", orig_addr_len); |
| 978 | return NULL; |
| 979 | } |
| 980 | |
| 981 | dest_addr_len = hdr48->data[3 + orig_addr_len + 1]; |
| 982 | dest_addr_ptr = &hdr48->data[3 + orig_addr_len + 2]; |
| 983 | |
| 984 | if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1) { |
| 985 | LOGP(DNAT, LOGL_ERROR, "RP-Destination addr does not fit: %d\n", dest_addr_len); |
| 986 | return NULL; |
| 987 | } |
| 988 | gsm48_decode_bcd_number(smsc_addr, ARRAY_SIZE(smsc_addr), dest_addr_ptr - 1, 1); |
| 989 | |
| 990 | data_len = hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 1]; |
| 991 | data_ptr = &hdr48->data[3 + orig_addr_len + 1 + dest_addr_len + 2]; |
| 992 | |
| 993 | if (cp_len < 3 + orig_addr_len + 1 + dest_addr_len + 1 + data_len) { |
| 994 | LOGP(DNAT, LOGL_ERROR, "RP-Data does not fit: %d\n", data_len); |
| 995 | return NULL; |
| 996 | } |
| 997 | |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 998 | /* look into the phone number */ |
| 999 | if ((data_ptr[0] & 0x01) != 1) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | if (data_len < 3) { |
| 1003 | LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT is too short.\n"); |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
| 1007 | dest_len = data_ptr[2]; |
| 1008 | if (data_len < dest_len + 3 || dest_len < 2) { |
| 1009 | LOGP(DNAT, LOGL_ERROR, "SMS-SUBMIT can not have TP-DestAddr.\n"); |
| 1010 | return NULL; |
| 1011 | } |
| 1012 | |
| 1013 | if ((data_ptr[3] & 0x80) == 0) { |
| 1014 | LOGP(DNAT, LOGL_ERROR, "TP-DestAddr has extension. Not handled.\n"); |
| 1015 | return NULL; |
| 1016 | } |
| 1017 | |
| 1018 | if ((data_ptr[3] & 0x0F) == 0) { |
| 1019 | LOGP(DNAT, LOGL_ERROR, "TP-DestAddr is not a ISDN number.\n"); |
| 1020 | return NULL; |
| 1021 | } |
| 1022 | |
| 1023 | gsm48_decode_bcd_number(_dest_nr + 2, ARRAY_SIZE(_dest_nr) - 2, |
| 1024 | &data_ptr[2], 1); |
| 1025 | if ((data_ptr[3] & 0x70) == 0x10) { |
| 1026 | _dest_nr[0] = _dest_nr[1] = '0'; |
| 1027 | dest_nr = &_dest_nr[0]; |
| 1028 | } else { |
| 1029 | dest_nr = &_dest_nr[2]; |
| 1030 | } |
| 1031 | |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1032 | /* We will find a new number now */ |
| 1033 | llist_for_each_entry(entry, &nat->smsc_rewr, list) { |
| 1034 | regmatch_t matches[2]; |
| 1035 | |
| 1036 | /* check the IMSI match */ |
| 1037 | if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0) |
| 1038 | continue; |
| 1039 | |
| 1040 | /* this regexp matches... */ |
| 1041 | if (regexec(&entry->num_reg, smsc_addr, 2, matches, 0) == 0 && |
| 1042 | matches[1].rm_eo != -1) |
| 1043 | new_number = talloc_asprintf(msg, "%s%s", |
| 1044 | entry->replace, |
| 1045 | &smsc_addr[matches[1].rm_so]); |
| 1046 | if (new_number) |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | if (!new_number) |
| 1051 | return NULL; |
| 1052 | |
| 1053 | /* |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1054 | * now match the number against another list |
| 1055 | */ |
| 1056 | llist_for_each_entry(entry, &nat->tpdest_match, list) { |
| 1057 | /* check the IMSI match */ |
| 1058 | if (regexec(&entry->msisdn_reg, imsi, 0, NULL, 0) != 0) |
| 1059 | continue; |
| 1060 | |
| 1061 | if (regexec(&entry->num_reg, dest_nr, 0, NULL, 0) == 0) { |
| 1062 | dest_match =1; |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | if (!dest_match) { |
| 1068 | talloc_free(new_number); |
| 1069 | return NULL; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1073 | * We need to re-create the patched structure. This is why we have |
| 1074 | * saved the above pointers. |
| 1075 | */ |
| 1076 | out = msgb_alloc_headroom(4096, 128, "changed-smsc"); |
| 1077 | if (!out) { |
| 1078 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 1079 | return NULL; |
| 1080 | } |
| 1081 | |
| 1082 | out->l3h = out->data; |
| 1083 | msgb_v_put(out, GSM411_MT_RP_DATA_MO); |
| 1084 | msgb_v_put(out, ref); |
| 1085 | msgb_lv_put(out, orig_addr_len, orig_addr_ptr); |
| 1086 | |
| 1087 | /* |
| 1088 | * Copy the new number. We let libosmocore encode it, then set |
| 1089 | * the extension followed after the length. For our convenience |
| 1090 | * we let the TLV code re-add the length so we start copying |
| 1091 | * from &new_addr[1]. |
| 1092 | */ |
| 1093 | new_addr_len = gsm48_encode_bcd_number(new_addr, ARRAY_SIZE(new_addr), |
| 1094 | 1, new_number); |
| 1095 | new_addr[1] = 0x91; |
| 1096 | msgb_lv_put(out, new_addr_len - 1, new_addr + 1); |
| 1097 | |
| 1098 | msgb_lv_put(out, data_len, data_ptr); |
| 1099 | |
| 1100 | new_hdr48 = (struct gsm48_hdr *) msgb_push(out, sizeof(*hdr48) + 1); |
| 1101 | memcpy(new_hdr48, hdr48, sizeof(*hdr48)); |
| 1102 | new_hdr48->data[0] = msgb_l3len(out); |
| 1103 | |
| 1104 | talloc_free(new_number); |
| 1105 | return out; |
| 1106 | } |
| 1107 | |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1108 | struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *parsed, const char *imsi) |
| 1109 | { |
| 1110 | struct gsm48_hdr *hdr48; |
| 1111 | uint32_t len; |
| 1112 | uint8_t msg_type, proto; |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 1113 | struct msgb *new_msg = NULL, *sccp; |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1114 | |
| 1115 | if (!imsi || strlen(imsi) < 5) |
| 1116 | return msg; |
| 1117 | |
| 1118 | /* only care about DTAP messages */ |
| 1119 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 1120 | return msg; |
| 1121 | if (!parsed->dest_local_ref) |
| 1122 | return msg; |
| 1123 | |
| 1124 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 1125 | if (!hdr48) |
| 1126 | return msg; |
| 1127 | |
| 1128 | proto = hdr48->proto_discr & 0x0f; |
| 1129 | msg_type = hdr48->msg_type & 0xbf; |
| 1130 | |
| 1131 | if (proto == GSM48_PDISC_CC && msg_type == GSM48_MT_CC_SETUP) |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 1132 | new_msg = rewrite_setup(nat, msg, parsed, imsi, hdr48, len); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1133 | else if (proto == GSM48_PDISC_SMS && msg_type == GSM411_MT_CP_DATA) |
| 1134 | new_msg = rewrite_smsc(nat, msg, parsed, imsi, hdr48, len); |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1135 | |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 1136 | if (!new_msg) |
| 1137 | return msg; |
| 1138 | |
| 1139 | /* wrap with DTAP, SCCP, then IPA. TODO: Stop copying */ |
| 1140 | gsm0808_prepend_dtap_header(new_msg, 0); |
| 1141 | sccp = sccp_create_dt1(parsed->dest_local_ref, new_msg->data, new_msg->len); |
| 1142 | talloc_free(new_msg); |
| 1143 | |
| 1144 | if (!sccp) { |
| 1145 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 1146 | return msg; |
| 1147 | } |
| 1148 | |
| 1149 | ipaccess_prepend_header(sccp, IPAC_PROTO_SCCP); |
| 1150 | |
| 1151 | /* the parsed hangs off from msg but it needs to survive */ |
| 1152 | talloc_steal(sccp, parsed); |
| 1153 | msgb_free(msg); |
| 1154 | return sccp; |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1155 | } |
Holger Hans Peter Freyther | 2e2ff34 | 2011-05-27 14:22:58 +0200 | [diff] [blame] | 1156 | |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 1157 | static void num_rewr_free_data(struct bsc_nat_num_rewr_entry *entry) |
| 1158 | { |
| 1159 | regfree(&entry->msisdn_reg); |
| 1160 | regfree(&entry->num_reg); |
| 1161 | talloc_free(entry->replace); |
| 1162 | } |
| 1163 | |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1164 | void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head, |
| 1165 | const struct osmo_config_list *list) |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 1166 | { |
| 1167 | struct bsc_nat_num_rewr_entry *entry, *tmp; |
| 1168 | struct osmo_config_entry *cfg_entry; |
| 1169 | |
| 1170 | /* free the old data */ |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1171 | llist_for_each_entry_safe(entry, tmp, head, list) { |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 1172 | num_rewr_free_data(entry); |
| 1173 | llist_del(&entry->list); |
| 1174 | talloc_free(entry); |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | if (!list) |
| 1179 | return; |
| 1180 | |
| 1181 | llist_for_each_entry(cfg_entry, &list->entry, list) { |
| 1182 | char *regexp; |
| 1183 | if (cfg_entry->text[0] == '+') { |
| 1184 | LOGP(DNAT, LOGL_ERROR, |
| 1185 | "Plus is not allowed in the number\n"); |
| 1186 | continue; |
| 1187 | } |
| 1188 | |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1189 | entry = talloc_zero(ctx, struct bsc_nat_num_rewr_entry); |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 1190 | if (!entry) { |
| 1191 | LOGP(DNAT, LOGL_ERROR, |
| 1192 | "Allication of the num_rewr entry failed.\n"); |
| 1193 | continue; |
| 1194 | } |
| 1195 | |
| 1196 | entry->replace = talloc_strdup(entry, cfg_entry->text); |
| 1197 | if (!entry->replace) { |
| 1198 | LOGP(DNAT, LOGL_ERROR, |
| 1199 | "Failed to copy the replacement text.\n"); |
| 1200 | talloc_free(entry); |
| 1201 | continue; |
| 1202 | } |
| 1203 | |
| 1204 | /* we will now build a regexp string */ |
| 1205 | if (cfg_entry->mcc[0] == '^') { |
| 1206 | regexp = talloc_strdup(entry, cfg_entry->mcc); |
| 1207 | } else { |
| 1208 | regexp = talloc_asprintf(entry, "^%s%s", |
| 1209 | cfg_entry->mcc[0] == '*' ? |
| 1210 | "[0-9][0-9][0-9]" : cfg_entry->mcc, |
| 1211 | cfg_entry->mnc[0] == '*' ? |
| 1212 | "[0-9][0-9]" : cfg_entry->mnc); |
| 1213 | } |
| 1214 | |
| 1215 | if (!regexp) { |
| 1216 | LOGP(DNAT, LOGL_ERROR, "Failed to create a regexp string.\n"); |
| 1217 | talloc_free(entry); |
| 1218 | continue; |
| 1219 | } |
| 1220 | |
| 1221 | if (regcomp(&entry->msisdn_reg, regexp, 0) != 0) { |
| 1222 | LOGP(DNAT, LOGL_ERROR, |
| 1223 | "Failed to compile regexp '%s'\n", regexp); |
| 1224 | talloc_free(regexp); |
| 1225 | talloc_free(entry); |
| 1226 | continue; |
| 1227 | } |
| 1228 | |
| 1229 | talloc_free(regexp); |
| 1230 | if (regcomp(&entry->num_reg, cfg_entry->option, REG_EXTENDED) != 0) { |
| 1231 | LOGP(DNAT, LOGL_ERROR, |
| 1232 | "Failed to compile regexp '%s\n'", cfg_entry->option); |
| 1233 | regfree(&entry->msisdn_reg); |
| 1234 | talloc_free(entry); |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | /* we have copied the number */ |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1239 | llist_add_tail(&entry->list, head); |
Holger Hans Peter Freyther | ad75eab | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 1240 | } |
| 1241 | } |