Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 5 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010-2011 by On-Waves |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 25 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 26 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 29 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 30 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 31 | |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 32 | #include <osmocom/core/linuxlist.h> |
| 33 | #include <osmocom/core/talloc.h> |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 35 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 37 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
Holger Hans Peter Freyther | e36be81 | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 38 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 39 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 41 | #include <netinet/in.h> |
| 42 | #include <arpa/inet.h> |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 43 | #include <unistd.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 45 | static const struct rate_ctr_desc bsc_cfg_ctr_description[] = { |
Holger Hans Peter Freyther | 275f27a | 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 | a67fc6f | 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 | 2e72807 | 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 | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 57 | [BCFG_CTR_CON_SSA] = { "conn.ssa", "Conn USSD "}, |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 58 | [BCFG_CTR_CON_OTHER] = { "conn.other", "Conn Other "}, |
Holger Hans Peter Freyther | ccefba6 | 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 | 39f1880 | 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 | 090a4d8 | 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 | 47a9f4b | 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 | 090a4d8 | 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 | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 94 | INIT_LLIST_HEAD(&nat->paging_groups); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 95 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 96 | INIT_LLIST_HEAD(&nat->access_lists); |
Holger Hans Peter Freyther | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 97 | INIT_LLIST_HEAD(&nat->dests); |
Holger Hans Peter Freyther | ed42b13 | 2011-05-27 12:38:58 +0200 | [diff] [blame] | 98 | INIT_LLIST_HEAD(&nat->num_rewr); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 99 | INIT_LLIST_HEAD(&nat->smsc_rewr); |
Holger Hans Peter Freyther | 1ed3927 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 100 | INIT_LLIST_HEAD(&nat->tpdest_match); |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 101 | INIT_LLIST_HEAD(&nat->sms_clear_tp_srr); |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 102 | INIT_LLIST_HEAD(&nat->sms_num_rewr); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 103 | |
Pablo Neira Ayuso | 1c45074 | 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 | acd3078 | 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 | 47a9f4b | 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 | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 118 | return nat; |
| 119 | } |
| 120 | |
Holger Hans Peter Freyther | 9226d70 | 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 | 47a9f4b | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 123 | bsc_replace_string(nat, &nat->main_dest->ip, ip); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 124 | } |
| 125 | |
Holger Hans Peter Freyther | 090a4d8 | 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 | 72deaea | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 132 | con->nat = nat; |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 133 | osmo_wqueue_init(&con->write_queue, 100); |
Daniel Willmann | d078d26 | 2011-02-18 14:32:56 +0100 | [diff] [blame] | 134 | INIT_LLIST_HEAD(&con->cmd_pending); |
Holger Hans Peter Freyther | 931d698 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 135 | INIT_LLIST_HEAD(&con->pending_dlcx); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 136 | return con; |
| 137 | } |
| 138 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 139 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token) |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 140 | { |
| 141 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 142 | if (!conf) |
| 143 | return NULL; |
| 144 | |
| 145 | conf->token = talloc_strdup(conf, token); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 146 | conf->nr = nat->num_bsc; |
| 147 | conf->nat = nat; |
Holger Hans Peter Freyther | b28f5a0 | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 148 | conf->max_endpoints = 32; |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 149 | conf->paging_group = PAGIN_GROUP_UNASSIGNED; |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 150 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 151 | INIT_LLIST_HEAD(&conf->lac_list); |
| 152 | |
Holger Hans Peter Freyther | 085ea30 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 153 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 154 | ++nat->num_bsc; |
| 155 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 156 | conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->nr); |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 157 | if (!conf->stats.ctrg) { |
| 158 | talloc_free(conf); |
| 159 | return NULL; |
| 160 | } |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 161 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 162 | return conf; |
| 163 | } |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 164 | |
Holger Hans Peter Freyther | 187221e | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 165 | void bsc_config_free(struct bsc_config *cfg) |
| 166 | { |
| 167 | rate_ctr_group_free(cfg->stats.ctrg); |
| 168 | } |
| 169 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 170 | static void _add_lac(void *ctx, struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 171 | { |
| 172 | struct bsc_lac_entry *lac; |
| 173 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 174 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 175 | if (lac->lac == _lac) |
| 176 | return; |
| 177 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 178 | lac = talloc_zero(ctx, struct bsc_lac_entry); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 179 | if (!lac) { |
| 180 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | lac->lac = _lac; |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 185 | llist_add_tail(&lac->entry, list); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 188 | static void _del_lac(struct llist_head *list, int _lac) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 189 | { |
| 190 | struct bsc_lac_entry *lac; |
| 191 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 192 | llist_for_each_entry(lac, list, entry) |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 193 | if (lac->lac == _lac) { |
| 194 | llist_del(&lac->entry); |
| 195 | talloc_free(lac); |
| 196 | return; |
| 197 | } |
| 198 | } |
| 199 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 200 | void bsc_config_add_lac(struct bsc_config *cfg, int _lac) |
| 201 | { |
| 202 | _add_lac(cfg, &cfg->lac_list, _lac); |
| 203 | } |
| 204 | |
| 205 | void bsc_config_del_lac(struct bsc_config *cfg, int _lac) |
| 206 | { |
| 207 | _del_lac(&cfg->lac_list, _lac); |
| 208 | } |
| 209 | |
| 210 | struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group) |
| 211 | { |
| 212 | struct bsc_nat_paging_group *pgroup; |
| 213 | |
| 214 | pgroup = talloc_zero(nat, struct bsc_nat_paging_group); |
| 215 | if (!pgroup) { |
| 216 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate a paging group.\n"); |
| 217 | return NULL; |
| 218 | } |
| 219 | |
| 220 | pgroup->nr = group; |
| 221 | INIT_LLIST_HEAD(&pgroup->lists); |
| 222 | llist_add_tail(&pgroup->entry, &nat->paging_groups); |
| 223 | return pgroup; |
| 224 | } |
| 225 | |
| 226 | void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *pgroup) |
| 227 | { |
| 228 | llist_del(&pgroup->entry); |
| 229 | talloc_free(pgroup); |
| 230 | } |
| 231 | |
| 232 | struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group) |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 233 | { |
| 234 | struct bsc_nat_paging_group *pgroup; |
| 235 | |
| 236 | llist_for_each_entry(pgroup, &nat->paging_groups, entry) |
| 237 | if (pgroup->nr == group) |
| 238 | return pgroup; |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Holger Hans Peter Freyther | 9561493 | 2011-05-02 19:16:13 +0200 | [diff] [blame] | 243 | void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 244 | { |
| 245 | _add_lac(pgroup, &pgroup->lists, lac); |
| 246 | } |
| 247 | |
| 248 | void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *pgroup, int lac) |
| 249 | { |
| 250 | _del_lac(&pgroup->lists, lac); |
| 251 | } |
| 252 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 253 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr) |
| 254 | { |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 255 | struct bsc_nat_paging_group *pgroup; |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 256 | struct bsc_lac_entry *entry; |
| 257 | |
| 258 | llist_for_each_entry(entry, &cfg->lac_list, entry) |
| 259 | if (entry->lac == lac_nr) |
| 260 | return 1; |
| 261 | |
Holger Hans Peter Freyther | 698e4b9 | 2011-05-02 16:50:36 +0200 | [diff] [blame] | 262 | /* now lookup the paging group */ |
| 263 | pgroup = bsc_nat_paging_group_num(cfg->nat, cfg->paging_group); |
| 264 | if (!pgroup) |
| 265 | return 0; |
| 266 | |
| 267 | llist_for_each_entry(entry, &pgroup->lists, entry) |
| 268 | if (entry->lac == lac_nr) |
| 269 | return 1; |
| 270 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 271 | return 0; |
| 272 | } |
| 273 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 274 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 275 | { |
| 276 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 277 | sccp_src_ref_to_int(&conn->real_ref), |
| 278 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 279 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 280 | llist_del(&conn->list_entry); |
| 281 | talloc_free(conn); |
| 282 | } |
| 283 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 284 | |
| 285 | int bsc_nat_find_paging(struct msgb *msg, |
| 286 | const uint8_t **out_data, int *out_leng) |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 287 | { |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 288 | int data_length; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 289 | const uint8_t *data; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 290 | struct tlv_parsed tp; |
Holger Hans Peter Freyther | 05c77ab | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 291 | |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 292 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 293 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 294 | return -1; |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 295 | } |
| 296 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 297 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 298 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 299 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 300 | return -2; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 304 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 305 | |
| 306 | /* No need to try a different BSS */ |
| 307 | if (data[0] == CELL_IDENT_BSS) { |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 308 | return -3; |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 309 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 60ebf14 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 310 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 311 | return -4; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 312 | } |
| 313 | |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 314 | *out_data = &data[1]; |
| 315 | *out_leng = data_length - 1; |
| 316 | return 0; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 319 | int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 320 | { |
| 321 | struct msgb *msg; |
| 322 | |
| 323 | if (length > 4096 - 128) { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 324 | LOGP(DLINP, LOGL_ERROR, "Can not send message of that size.\n"); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 329 | if (!msg) { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 330 | LOGP(DLINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | /* copy the data */ |
| 335 | msg->l3h = msgb_put(msg, length); |
| 336 | memcpy(msg->l3h, data, length); |
| 337 | |
Holger Hans Peter Freyther | a63d35c | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 338 | return bsc_write(bsc, msg, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 339 | } |
| 340 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 341 | int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 342 | { |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 343 | return bsc_do_write(&bsc->write_queue, msg, proto); |
| 344 | } |
| 345 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 346 | int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 347 | { |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 348 | /* prepend the header */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 349 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 350 | return bsc_write_msg(queue, msg); |
| 351 | } |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 352 | |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 353 | int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg) |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 354 | { |
Pablo Neira Ayuso | da08850 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 355 | if (osmo_wqueue_enqueue(queue, msg) != 0) { |
Pablo Neira Ayuso | 42e41df | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 356 | LOGP(DLINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 357 | msgb_free(msg); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 363 | |
Holger Hans Peter Freyther | 01ae681 | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 364 | int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 365 | { |
| 366 | struct bsc_nat_acc_lst_entry *entry; |
| 367 | |
| 368 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 369 | if (!entry->imsi_allow) |
| 370 | continue; |
| 371 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | return 1; |
| 376 | } |
| 377 | |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 378 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, |
| 379 | struct msgb *msg, uint32_t *len) |
| 380 | { |
| 381 | /* gsm_type is actually the size of the dtap */ |
| 382 | *len = parsed->gsm_type; |
| 383 | if (*len < msgb_l3len(msg) - 3) { |
| 384 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Holger Hans Peter Freyther | 2b1e7dc | 2012-01-10 22:27:43 +0100 | [diff] [blame] | 388 | if (msgb_l3len(msg) - 3 < msg->l3h[2]) { |
| 389 | LOGP(DNAT, LOGL_ERROR, |
| 390 | "GSM48 payload does not fit: %d %d\n", |
| 391 | msg->l3h[2], msgb_l3len(msg) - 3); |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | msg->l4h = &msg->l3h[3]; |
| 396 | return (struct gsm48_hdr *) msg->l4h; |
| 397 | } |
| 398 | |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 399 | static const char *con_types [] = { |
| 400 | [NAT_CON_TYPE_NONE] = "n/a", |
| 401 | [NAT_CON_TYPE_LU] = "Location Update", |
| 402 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 403 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 404 | [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 405 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 406 | [NAT_CON_TYPE_OTHER] = "Other", |
| 407 | }; |
| 408 | |
| 409 | const char *bsc_con_type_to_string(int type) |
| 410 | { |
| 411 | return con_types[type]; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 412 | } |
| 413 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 414 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 415 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 416 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 417 | |
| 418 | if (!name) |
| 419 | return NULL; |
| 420 | |
| 421 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 422 | if (strcmp(lst->name, name) == 0) |
| 423 | return lst; |
| 424 | |
| 425 | return NULL; |
| 426 | } |
| 427 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 428 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 429 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 430 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 431 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 432 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 433 | if (lst) |
| 434 | return lst; |
| 435 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 436 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 437 | if (!lst) { |
| 438 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 442 | /* TODO: get the index right */ |
| 443 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 444 | if (!lst->stats) { |
| 445 | talloc_free(lst); |
| 446 | return NULL; |
| 447 | } |
| 448 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 449 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 450 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 451 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 452 | return lst; |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 453 | } |
| 454 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 455 | void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 456 | { |
| 457 | llist_del(&lst->list); |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 458 | rate_ctr_group_free(lst->stats); |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 459 | talloc_free(lst); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 463 | { |
| 464 | struct bsc_nat_acc_lst_entry *entry; |
| 465 | |
| 466 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 467 | if (!entry) |
| 468 | return NULL; |
| 469 | |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 470 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 471 | return entry; |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 472 | } |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 473 | |
| 474 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 475 | { |
| 476 | return nat->msc_con->is_connected; |
| 477 | } |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 478 | |
| 479 | static const int con_to_ctr[] = { |
| 480 | [NAT_CON_TYPE_NONE] = -1, |
| 481 | [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, |
| 482 | [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, |
| 483 | [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 484 | [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 485 | [NAT_CON_TYPE_LOCAL_REJECT] = -1, |
| 486 | [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, |
| 487 | }; |
| 488 | |
| 489 | int bsc_conn_type_to_ctr(struct sccp_connections *conn) |
| 490 | { |
| 491 | return con_to_ctr[conn->con_type]; |
| 492 | } |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 493 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 494 | int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg) |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 495 | { |
| 496 | int rc; |
| 497 | |
| 498 | rc = write(bfd->fd, msg->data, msg->len); |
| 499 | if (rc != msg->len) |
| 500 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 501 | |
| 502 | return rc; |
| 503 | } |
| 504 | |