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