Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 1 | |
| 2 | /* BSC Multiplexer/NAT Utilities */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2010 by On-Waves |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 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 |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 26 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 27 | #include <openbsc/bsc_msc.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 28 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 29 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 30 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 31 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 32 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 33 | #include <osmocore/linuxlist.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 34 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | a707061 | 2010-06-15 18:50:57 +0800 | [diff] [blame] | 35 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | e36be81 | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 37 | #include <osmocore/protocol/gsm_08_08.h> |
| 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 | |
| 86 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 87 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 88 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 89 | INIT_LLIST_HEAD(&nat->access_lists); |
| 90 | |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 91 | nat->stats.sccp.conn = counter_alloc("nat.sccp.conn"); |
| 92 | nat->stats.sccp.calls = counter_alloc("nat.sccp.calls"); |
| 93 | nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn"); |
| 94 | nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail"); |
| 95 | nat->stats.msc.reconn = counter_alloc("nat.msc.conn"); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 96 | nat->msc_ip = talloc_strdup(nat, "127.0.0.1"); |
Holger Hans Peter Freyther | bace202 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 97 | nat->msc_port = 5000; |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 98 | nat->auth_timeout = 2; |
| 99 | nat->ping_timeout = 20; |
| 100 | nat->pong_timeout = 5; |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 101 | return nat; |
| 102 | } |
| 103 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 104 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 105 | { |
Holger Hans Peter Freyther | 7c831f6 | 2010-10-12 23:21:54 +0200 | [diff] [blame] | 106 | bsc_replace_string(nat, &nat->msc_ip, ip); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 109 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 110 | { |
| 111 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 112 | if (!con) |
| 113 | return NULL; |
| 114 | |
Holger Hans Peter Freyther | 72deaea | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 115 | con->nat = nat; |
Holger Hans Peter Freyther | 0952b2f | 2010-04-22 12:05:23 +0800 | [diff] [blame] | 116 | write_queue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 117 | return con; |
| 118 | } |
| 119 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 120 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token) |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 121 | { |
| 122 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 123 | if (!conf) |
| 124 | return NULL; |
| 125 | |
| 126 | conf->token = talloc_strdup(conf, token); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 127 | conf->nr = nat->num_bsc; |
| 128 | conf->nat = nat; |
| 129 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 130 | INIT_LLIST_HEAD(&conf->lac_list); |
| 131 | |
Holger Hans Peter Freyther | 085ea30 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 132 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 133 | ++nat->num_bsc; |
| 134 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 135 | 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] | 136 | if (!conf->stats.ctrg) { |
| 137 | talloc_free(conf); |
| 138 | return NULL; |
| 139 | } |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 140 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 141 | return conf; |
| 142 | } |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 143 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 144 | void bsc_config_add_lac(struct bsc_config *cfg, int _lac) |
| 145 | { |
| 146 | struct bsc_lac_entry *lac; |
| 147 | |
| 148 | llist_for_each_entry(lac, &cfg->lac_list, entry) |
| 149 | if (lac->lac == _lac) |
| 150 | return; |
| 151 | |
| 152 | lac = talloc_zero(cfg, struct bsc_lac_entry); |
| 153 | if (!lac) { |
| 154 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate.\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | lac->lac = _lac; |
| 159 | llist_add_tail(&lac->entry, &cfg->lac_list); |
| 160 | } |
| 161 | |
| 162 | void bsc_config_del_lac(struct bsc_config *cfg, int _lac) |
| 163 | { |
| 164 | struct bsc_lac_entry *lac; |
| 165 | |
| 166 | llist_for_each_entry(lac, &cfg->lac_list, entry) |
| 167 | if (lac->lac == _lac) { |
| 168 | llist_del(&lac->entry); |
| 169 | talloc_free(lac); |
| 170 | return; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac_nr) |
| 175 | { |
| 176 | struct bsc_lac_entry *entry; |
| 177 | |
| 178 | llist_for_each_entry(entry, &cfg->lac_list, entry) |
| 179 | if (entry->lac == lac_nr) |
| 180 | return 1; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 185 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 186 | { |
| 187 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 188 | sccp_src_ref_to_int(&conn->real_ref), |
| 189 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 190 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 191 | llist_del(&conn->list_entry); |
| 192 | talloc_free(conn); |
| 193 | } |
| 194 | |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 195 | struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out) |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 196 | { |
| 197 | struct bsc_connection *bsc; |
| 198 | int data_length; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 199 | const uint8_t *data; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 200 | struct tlv_parsed tp; |
| 201 | int i = 0; |
| 202 | |
Holger Hans Peter Freyther | 05c77ab | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 203 | *lac_out = -1; |
| 204 | |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 205 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 206 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
| 207 | return NULL; |
| 208 | } |
| 209 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 210 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 211 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 212 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 217 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 218 | |
| 219 | /* No need to try a different BSS */ |
| 220 | if (data[0] == CELL_IDENT_BSS) { |
| 221 | return NULL; |
| 222 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 60ebf14 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 223 | LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]); |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | /* Currently we only handle one BSC */ |
| 228 | for (i = 1; i < data_length - 1; i += 2) { |
| 229 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 230 | *lac_out = _lac; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 231 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 232 | if (!bsc->cfg) |
| 233 | continue; |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 234 | if (!bsc->authenticated) |
| 235 | continue; |
| 236 | if (!bsc_config_handles_lac(bsc->cfg, _lac)) |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 237 | continue; |
| 238 | |
| 239 | return bsc; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return NULL; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 244 | } |
| 245 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | struct msgb *msg; |
| 249 | |
| 250 | if (length > 4096 - 128) { |
| 251 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 256 | if (!msg) { |
| 257 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /* copy the data */ |
| 262 | msg->l3h = msgb_put(msg, length); |
| 263 | memcpy(msg->l3h, data, length); |
| 264 | |
Holger Hans Peter Freyther | d5cd304 | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 265 | return bsc_write(bsc, msg, IPAC_PROTO_MGCP); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 268 | 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] | 269 | { |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 270 | return bsc_do_write(&bsc->write_queue, msg, proto); |
| 271 | } |
| 272 | |
| 273 | int bsc_do_write(struct write_queue *queue, struct msgb *msg, int proto) |
| 274 | { |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 275 | /* prepend the header */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 276 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 277 | return bsc_write_msg(queue, msg); |
| 278 | } |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 279 | |
Holger Hans Peter Freyther | acc0fdf | 2010-10-16 17:33:46 +0200 | [diff] [blame] | 280 | int bsc_write_msg(struct write_queue *queue, struct msgb *msg) |
| 281 | { |
Holger Hans Peter Freyther | f232b27 | 2010-10-13 17:56:22 +0200 | [diff] [blame] | 282 | if (write_queue_enqueue(queue, msg) != 0) { |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 283 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 284 | msgb_free(msg); |
| 285 | return -1; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 290 | |
Holger Hans Peter Freyther | 01ae681 | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | struct bsc_nat_acc_lst_entry *entry; |
| 294 | |
| 295 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 296 | if (!entry->imsi_allow) |
| 297 | continue; |
| 298 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 306 | { |
| 307 | struct bsc_nat_acc_lst_entry *entry; |
| 308 | |
| 309 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 310 | if (!entry->imsi_deny) |
| 311 | continue; |
| 312 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | return 1; |
| 317 | } |
| 318 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 319 | /* apply white/black list */ |
| 320 | static int auth_imsi(struct bsc_connection *bsc, const char *mi_string) |
| 321 | { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 322 | /* |
| 323 | * Now apply blacklist/whitelist of the BSC and the NAT. |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 324 | * 1.) Allow directly if the IMSI is allowed at the BSC |
| 325 | * 2.) Reject if the IMSI is not allowed at the BSC |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 326 | * 3.) Reject if the IMSI not allowed at the global level. |
| 327 | * 4.) Allow directly if the IMSI is allowed at the global level |
| 328 | */ |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 329 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 330 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 331 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 332 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 333 | nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 334 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 335 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 336 | if (bsc_lst) { |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 337 | /* 1. BSC allow */ |
Holger Hans Peter Freyther | 01ae681 | 2010-10-11 09:50:31 +0200 | [diff] [blame] | 338 | if (bsc_nat_lst_check_allow(bsc_lst, mi_string) == 0) |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 339 | return 1; |
| 340 | |
| 341 | /* 2. BSC deny */ |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 342 | if (lst_check_deny(bsc_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 343 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 344 | "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 345 | rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 346 | return -2; |
| 347 | } |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 348 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* 3. NAT deny */ |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 352 | if (nat_lst) { |
| 353 | if (lst_check_deny(nat_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 354 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 355 | "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 3cc2e2f | 2010-10-06 00:18:20 +0800 | [diff] [blame] | 356 | rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 357 | return -3; |
| 358 | } |
| 359 | } |
| 360 | |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 361 | return 1; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 362 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 363 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 364 | static int _cr_check_loc_upd(struct bsc_connection *bsc, |
| 365 | uint8_t *data, unsigned int length, |
| 366 | char **imsi) |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 367 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 368 | uint8_t mi_type; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 369 | struct gsm48_loc_upd_req *lu; |
| 370 | char mi_string[GSM48_MI_SIZE]; |
| 371 | |
Holger Hans Peter Freyther | 540f4c8 | 2010-05-14 19:24:06 +0800 | [diff] [blame] | 372 | if (length < sizeof(*lu)) { |
| 373 | LOGP(DNAT, LOGL_ERROR, |
| 374 | "LU does not fit. Length is %d \n", length); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | lu = (struct gsm48_loc_upd_req *) data; |
| 379 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 380 | |
| 381 | /* |
| 382 | * We can only deal with the IMSI. This will fail for a phone that |
| 383 | * will send the TMSI of a previous network to us. |
| 384 | */ |
| 385 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 386 | return 0; |
| 387 | |
| 388 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 389 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 390 | return auth_imsi(bsc, mi_string); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 393 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, |
| 394 | uint8_t *data, unsigned int length, |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 395 | int *con_type, char **imsi) |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 396 | { |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 397 | static const uint32_t classmark_offset = |
| 398 | offsetof(struct gsm48_service_request, classmark); |
| 399 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 400 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 401 | uint8_t mi_type; |
| 402 | int rc; |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 403 | struct gsm48_service_request *req; |
| 404 | |
| 405 | /* unfortunately in Phase1 the classmark2 length is variable */ |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 406 | |
| 407 | if (length < sizeof(*req)) { |
| 408 | LOGP(DNAT, LOGL_ERROR, |
| 409 | "CM Serv Req does not fit. Length is %d\n", length); |
| 410 | return -1; |
| 411 | } |
| 412 | |
| 413 | req = (struct gsm48_service_request *) data; |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 414 | if (req->cm_service_type == 0x8) |
| 415 | *con_type = NAT_CON_TYPE_SSA; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 416 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 417 | length - classmark_offset, mi_string, &mi_type); |
| 418 | if (rc < 0) { |
| 419 | LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 420 | return -1; |
| 421 | } |
| 422 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 423 | /* we have to let the TMSI or such pass */ |
| 424 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 425 | return 0; |
| 426 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 427 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 428 | return auth_imsi(bsc, mi_string); |
| 429 | } |
| 430 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 431 | static int _cr_check_pag_resp(struct bsc_connection *bsc, |
| 432 | uint8_t *data, unsigned int length, |
| 433 | char **imsi) |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 434 | { |
| 435 | struct gsm48_pag_resp *resp; |
| 436 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 437 | uint8_t mi_type; |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 438 | |
| 439 | if (length < sizeof(*resp)) { |
| 440 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | resp = (struct gsm48_pag_resp *) data; |
| 445 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 446 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | /* we need to let it pass for now */ |
| 451 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 452 | return 0; |
| 453 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 454 | *imsi = talloc_strdup(bsc, mi_string); |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 455 | return auth_imsi(bsc, mi_string); |
| 456 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 457 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 458 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 459 | uint8_t *data, unsigned int length, |
| 460 | struct sccp_connections *con) |
| 461 | { |
| 462 | char mi_string[GSM48_MI_SIZE]; |
| 463 | uint8_t mi_type; |
| 464 | int ret; |
| 465 | |
| 466 | if (length < 2) { |
| 467 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 468 | return -1; |
| 469 | } |
| 470 | |
| 471 | if (data[0] < length - 1) { |
| 472 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 473 | return -2; |
| 474 | } |
| 475 | |
| 476 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 477 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 478 | |
| 479 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 480 | return 0; |
| 481 | |
| 482 | ret = auth_imsi(bsc, mi_string); |
| 483 | con->imsi_checked = 1; |
Holger Hans Peter Freyther | 4a98ae9 | 2010-09-29 01:00:46 +0800 | [diff] [blame] | 484 | con->imsi = talloc_strdup(con, mi_string); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 485 | return ret; |
| 486 | } |
| 487 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 488 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 489 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 490 | struct bsc_nat_parsed *parsed, int *con_type, |
| 491 | char **imsi) |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 492 | { |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 493 | struct tlv_parsed tp; |
| 494 | struct gsm48_hdr *hdr48; |
| 495 | int hdr48_len; |
| 496 | int len; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 497 | uint8_t msg_type; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 498 | |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 499 | *con_type = NAT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 500 | *imsi = NULL; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 501 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 502 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 503 | LOGP(DNAT, LOGL_ERROR, |
| 504 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | /* the parsed has had some basic l3 length check */ |
| 509 | len = msg->l3h[1]; |
| 510 | if (msgb_l3len(msg) - 3 < len) { |
| 511 | LOGP(DNAT, LOGL_ERROR, |
| 512 | "The CR Data has not enough space...\n"); |
| 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | msg->l4h = &msg->l3h[3]; |
| 517 | len -= 1; |
| 518 | |
| 519 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 520 | |
| 521 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 522 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 527 | |
| 528 | if (hdr48_len < sizeof(*hdr48)) { |
| 529 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 534 | |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 535 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 6c1610f | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 536 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 537 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 538 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 539 | return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 6c1610f | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 540 | } else if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 541 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 542 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 543 | return _cr_check_cm_serv_req(bsc, &hdr48->data[0], |
| 544 | hdr48_len - sizeof(*hdr48), |
| 545 | con_type, imsi); |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 546 | } else if (hdr48->proto_discr == GSM48_PDISC_RR && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 547 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 548 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 549 | return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 550 | } else { |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 551 | /* We only want to filter the above, let other things pass */ |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 552 | *con_type = NAT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 553 | return 0; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 554 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 555 | } |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 556 | |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 557 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, |
| 558 | struct msgb *msg, uint32_t *len) |
| 559 | { |
| 560 | /* gsm_type is actually the size of the dtap */ |
| 561 | *len = parsed->gsm_type; |
| 562 | if (*len < msgb_l3len(msg) - 3) { |
| 563 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | if (*len < sizeof(struct gsm48_hdr)) { |
| 568 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | msg->l4h = &msg->l3h[3]; |
| 573 | return (struct gsm48_hdr *) msg->l4h; |
| 574 | } |
| 575 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 576 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 577 | struct sccp_connections *con, struct bsc_nat_parsed *parsed) |
| 578 | { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 579 | uint32_t len; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 580 | uint8_t msg_type; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 581 | struct gsm48_hdr *hdr48; |
| 582 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 583 | if (con->imsi_checked) |
| 584 | return 0; |
| 585 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 586 | /* only care about DTAP messages */ |
| 587 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 588 | return 0; |
| 589 | |
Holger Hans Peter Freyther | b542332 | 2010-09-29 02:30:50 +0800 | [diff] [blame] | 590 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 591 | if (!hdr48) |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 592 | return -1; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 593 | |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 594 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 595 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 596 | msg_type == GSM48_MT_MM_ID_RESP) { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 597 | return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con); |
| 598 | } else { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 599 | return 0; |
| 600 | } |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 601 | } |
| 602 | |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 603 | void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
| 604 | { |
| 605 | if (*imsi) { |
| 606 | talloc_free(*imsi); |
| 607 | *imsi = NULL; |
| 608 | } |
| 609 | regfree(reg); |
| 610 | |
| 611 | if (argc > 0) { |
| 612 | *imsi = talloc_strdup(ctx, argv[0]); |
| 613 | regcomp(reg, argv[0], 0); |
| 614 | } |
| 615 | } |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 616 | |
| 617 | static const char *con_types [] = { |
| 618 | [NAT_CON_TYPE_NONE] = "n/a", |
| 619 | [NAT_CON_TYPE_LU] = "Location Update", |
| 620 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 621 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 622 | [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 623 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 624 | [NAT_CON_TYPE_OTHER] = "Other", |
| 625 | }; |
| 626 | |
| 627 | const char *bsc_con_type_to_string(int type) |
| 628 | { |
| 629 | return con_types[type]; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 630 | } |
| 631 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 632 | 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] | 633 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 634 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 635 | |
| 636 | if (!name) |
| 637 | return NULL; |
| 638 | |
| 639 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 640 | if (strcmp(lst->name, name) == 0) |
| 641 | return lst; |
| 642 | |
| 643 | return NULL; |
| 644 | } |
| 645 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 646 | 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] | 647 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 648 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 649 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 650 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 651 | if (lst) |
| 652 | return lst; |
| 653 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 654 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 655 | if (!lst) { |
| 656 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 657 | return NULL; |
| 658 | } |
| 659 | |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 660 | /* TODO: get the index right */ |
| 661 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 662 | if (!lst->stats) { |
| 663 | talloc_free(lst); |
| 664 | return NULL; |
| 665 | } |
| 666 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 667 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 668 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 669 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 670 | return lst; |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 673 | 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] | 674 | { |
| 675 | llist_del(&lst->list); |
Holger Hans Peter Freyther | 39f1880 | 2010-09-25 06:14:52 +0800 | [diff] [blame] | 676 | rate_ctr_group_free(lst->stats); |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 677 | talloc_free(lst); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 681 | { |
| 682 | struct bsc_nat_acc_lst_entry *entry; |
| 683 | |
| 684 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 685 | if (!entry) |
| 686 | return NULL; |
| 687 | |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 688 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 689 | return entry; |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 690 | } |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 691 | |
| 692 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 693 | { |
| 694 | return nat->msc_con->is_connected; |
| 695 | } |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 696 | |
| 697 | static const int con_to_ctr[] = { |
| 698 | [NAT_CON_TYPE_NONE] = -1, |
| 699 | [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, |
| 700 | [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, |
| 701 | [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, |
Holger Hans Peter Freyther | 1b1a936 | 2010-09-29 02:47:29 +0800 | [diff] [blame] | 702 | [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, |
Holger Hans Peter Freyther | 2e72807 | 2010-10-03 19:41:42 +0800 | [diff] [blame] | 703 | [NAT_CON_TYPE_LOCAL_REJECT] = -1, |
| 704 | [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, |
| 705 | }; |
| 706 | |
| 707 | int bsc_conn_type_to_ctr(struct sccp_connections *conn) |
| 708 | { |
| 709 | return con_to_ctr[conn->con_type]; |
| 710 | } |
Holger Hans Peter Freyther | 6c1ae96 | 2010-10-13 20:37:13 +0200 | [diff] [blame] | 711 | |
| 712 | int bsc_write_cb(struct bsc_fd *bfd, struct msgb *msg) |
| 713 | { |
| 714 | int rc; |
| 715 | |
| 716 | rc = write(bfd->fd, msg->data, msg->len); |
| 717 | if (rc != msg->len) |
| 718 | LOGP(DNAT, LOGL_ERROR, "Failed to write message to the BSC.\n"); |
| 719 | |
| 720 | return rc; |
| 721 | } |
| 722 | |