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 | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 31 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 32 | #include <osmocore/linuxlist.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 33 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | a707061 | 2010-06-15 18:50:57 +0800 | [diff] [blame] | 34 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | e36be81 | 2010-06-15 20:13:33 +0800 | [diff] [blame] | 36 | #include <osmocore/protocol/gsm_08_08.h> |
| 37 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 38 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 40 | #include <netinet/in.h> |
| 41 | #include <arpa/inet.h> |
| 42 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 43 | |
| 44 | static const struct rate_ctr_desc bsc_cfg_ctr_description[] = { |
Holger Hans Peter Freyther | 275f27a | 2010-06-17 18:31:18 +0800 | [diff] [blame] | 45 | [BCFG_CTR_SCCP_CONN] = { "sccp.conn", "SCCP Connections "}, |
| 46 | [BCFG_CTR_SCCP_CALLS] = { "sccp.calls", "SCCP Assignment Commands "}, |
| 47 | [BCFG_CTR_NET_RECONN] = { "net.reconnects", "Network reconnects "}, |
| 48 | [BCFG_CTR_DROPPED_SCCP] = { "dropped.sccp", "Dropped SCCP connections."}, |
| 49 | [BCFG_CTR_DROPPED_CALLS] = { "dropped.calls", "Dropped active calls. "}, |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = { |
| 53 | .group_name_prefix = "nat.bsc", |
| 54 | .group_description = "NAT BSC Statistics", |
| 55 | .num_ctr = ARRAY_SIZE(bsc_cfg_ctr_description), |
| 56 | .ctr_desc = bsc_cfg_ctr_description, |
| 57 | }; |
| 58 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 59 | struct bsc_nat *bsc_nat_alloc(void) |
| 60 | { |
| 61 | struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); |
| 62 | if (!nat) |
| 63 | return NULL; |
| 64 | |
| 65 | INIT_LLIST_HEAD(&nat->sccp_connections); |
| 66 | INIT_LLIST_HEAD(&nat->bsc_connections); |
| 67 | INIT_LLIST_HEAD(&nat->bsc_configs); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 68 | INIT_LLIST_HEAD(&nat->access_lists); |
| 69 | |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 70 | nat->stats.sccp.conn = counter_alloc("nat.sccp.conn"); |
| 71 | nat->stats.sccp.calls = counter_alloc("nat.sccp.calls"); |
| 72 | nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn"); |
| 73 | nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail"); |
| 74 | nat->stats.msc.reconn = counter_alloc("nat.msc.conn"); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 75 | nat->msc_ip = talloc_strdup(nat, "127.0.0.1"); |
Holger Hans Peter Freyther | bace202 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 76 | nat->msc_port = 5000; |
Holger Hans Peter Freyther | acd3078 | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 77 | nat->auth_timeout = 2; |
| 78 | nat->ping_timeout = 20; |
| 79 | nat->pong_timeout = 5; |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 80 | return nat; |
| 81 | } |
| 82 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 83 | void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip) |
| 84 | { |
| 85 | if (nat->msc_ip) |
| 86 | talloc_free(nat->msc_ip); |
| 87 | nat->msc_ip = talloc_strdup(nat, ip); |
| 88 | } |
| 89 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 90 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat) |
| 91 | { |
| 92 | struct bsc_connection *con = talloc_zero(nat, struct bsc_connection); |
| 93 | if (!con) |
| 94 | return NULL; |
| 95 | |
Holger Hans Peter Freyther | 72deaea | 2010-03-29 15:14:15 +0200 | [diff] [blame] | 96 | con->nat = nat; |
Holger Hans Peter Freyther | 0952b2f | 2010-04-22 12:05:23 +0800 | [diff] [blame] | 97 | write_queue_init(&con->write_queue, 100); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 98 | return con; |
| 99 | } |
| 100 | |
| 101 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac) |
| 102 | { |
| 103 | struct bsc_config *conf = talloc_zero(nat, struct bsc_config); |
| 104 | if (!conf) |
| 105 | return NULL; |
| 106 | |
| 107 | conf->token = talloc_strdup(conf, token); |
| 108 | conf->lac = lac; |
| 109 | conf->nr = nat->num_bsc; |
| 110 | conf->nat = nat; |
| 111 | |
Holger Hans Peter Freyther | 085ea30 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 112 | llist_add_tail(&conf->entry, &nat->bsc_configs); |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 113 | ++nat->num_bsc; |
| 114 | |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 115 | conf->stats.ctrg = rate_ctr_group_alloc(conf, &bsc_cfg_ctrg_desc, conf->lac); |
| 116 | if (!conf->stats.ctrg) { |
| 117 | talloc_free(conf); |
| 118 | return NULL; |
| 119 | } |
Holger Hans Peter Freyther | 5fa42dd | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 120 | |
Holger Hans Peter Freyther | 090a4d8 | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 121 | return conf; |
| 122 | } |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 123 | |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 124 | void sccp_connection_destroy(struct sccp_connections *conn) |
| 125 | { |
| 126 | LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n", |
| 127 | sccp_src_ref_to_int(&conn->real_ref), |
| 128 | sccp_src_ref_to_int(&conn->patched_ref), conn->bsc); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 129 | bsc_mgcp_dlcx(conn); |
Holger Hans Peter Freyther | 8be49b3 | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 130 | llist_del(&conn->list_entry); |
| 131 | talloc_free(conn); |
| 132 | } |
| 133 | |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 134 | 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] | 135 | { |
| 136 | struct bsc_connection *bsc; |
| 137 | int data_length; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 138 | const uint8_t *data; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 139 | struct tlv_parsed tp; |
| 140 | int i = 0; |
| 141 | |
Holger Hans Peter Freyther | 05c77ab | 2010-04-18 02:41:20 +0800 | [diff] [blame] | 142 | *lac_out = -1; |
| 143 | |
Holger Hans Peter Freyther | 4b66bff | 2010-03-30 06:51:23 +0200 | [diff] [blame] | 144 | if (!msg->l3h || msgb_l3len(msg) < 3) { |
| 145 | LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n"); |
| 146 | return NULL; |
| 147 | } |
| 148 | |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 149 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0); |
| 150 | if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) { |
| 151 | LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n"); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 156 | data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST); |
Holger Hans Peter Freyther | 69a365e | 2010-04-21 18:47:24 +0800 | [diff] [blame] | 157 | |
| 158 | /* No need to try a different BSS */ |
| 159 | if (data[0] == CELL_IDENT_BSS) { |
| 160 | return NULL; |
| 161 | } else if (data[0] != CELL_IDENT_LAC) { |
Holger Hans Peter Freyther | 60ebf14 | 2010-04-06 10:25:40 +0200 | [diff] [blame] | 162 | 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] | 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | /* Currently we only handle one BSC */ |
| 167 | for (i = 1; i < data_length - 1; i += 2) { |
| 168 | unsigned int _lac = ntohs(*(unsigned int *) &data[i]); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 169 | *lac_out = _lac; |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 170 | llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 171 | if (!bsc->cfg) |
| 172 | continue; |
| 173 | if (!bsc->authenticated || _lac != bsc->cfg->lac) |
Holger Hans Peter Freyther | 21c4d9e | 2010-03-30 05:57:42 +0200 | [diff] [blame] | 174 | continue; |
| 175 | |
| 176 | return bsc; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return NULL; |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | struct msgb *msg; |
| 186 | |
| 187 | if (length > 4096 - 128) { |
| 188 | LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | msg = msgb_alloc_headroom(4096, 128, "to-bsc"); |
| 193 | if (!msg) { |
| 194 | LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | /* copy the data */ |
| 199 | msg->l3h = msgb_put(msg, length); |
| 200 | memcpy(msg->l3h, data, length); |
| 201 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 202 | return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 205 | 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] | 206 | { |
| 207 | /* prepend the header */ |
Holger Hans Peter Freyther | eb4edde | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 208 | ipaccess_prepend_header(msg, proto); |
Holger Hans Peter Freyther | 8ddcc15 | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 209 | |
| 210 | if (write_queue_enqueue(&bsc->write_queue, msg) != 0) { |
| 211 | LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n"); |
| 212 | msgb_free(msg); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 218 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 219 | static int lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 220 | { |
| 221 | struct bsc_nat_acc_lst_entry *entry; |
| 222 | |
| 223 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 224 | if (!entry->imsi_allow) |
| 225 | continue; |
| 226 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string) |
| 234 | { |
| 235 | struct bsc_nat_acc_lst_entry *entry; |
| 236 | |
| 237 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 238 | if (!entry->imsi_deny) |
| 239 | continue; |
| 240 | if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | return 1; |
| 245 | } |
| 246 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 247 | /* apply white/black list */ |
| 248 | static int auth_imsi(struct bsc_connection *bsc, const char *mi_string) |
| 249 | { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 250 | /* |
| 251 | * Now apply blacklist/whitelist of the BSC and the NAT. |
| 252 | * 1.) Reject if the IMSI is not allowed at the BSC |
| 253 | * 2.) Allow directly if the IMSI is allowed at the BSC |
| 254 | * 3.) Reject if the IMSI not allowed at the global level. |
| 255 | * 4.) Allow directly if the IMSI is allowed at the global level |
| 256 | */ |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 257 | struct bsc_nat_acc_lst *nat_lst = NULL; |
| 258 | struct bsc_nat_acc_lst *bsc_lst = NULL; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 259 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 260 | bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); |
| 261 | 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] | 262 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 263 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 264 | if (bsc_lst) { |
| 265 | /* 1. BSC deny */ |
| 266 | if (lst_check_deny(bsc_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 267 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 268 | "Filtering %s by imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 269 | return -2; |
| 270 | } |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 271 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 272 | /* 2. BSC allow */ |
| 273 | if (lst_check_allow(bsc_lst, mi_string) == 0) |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 274 | return 1; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* 3. NAT deny */ |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 278 | if (nat_lst) { |
| 279 | if (lst_check_deny(nat_lst, mi_string) == 0) { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 280 | LOGP(DNAT, LOGL_ERROR, |
Holger Hans Peter Freyther | 801efa1 | 2010-05-16 00:45:07 +0800 | [diff] [blame] | 281 | "Filtering %s by nat imsi_deny on bsc nr: %d.\n", mi_string, bsc->cfg->nr); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 282 | return -3; |
| 283 | } |
| 284 | } |
| 285 | |
Holger Hans Peter Freyther | 531d3e3 | 2010-09-15 00:41:19 +0800 | [diff] [blame] | 286 | return 1; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 287 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 288 | |
| 289 | static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length) |
| 290 | { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 291 | uint8_t mi_type; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 292 | struct gsm48_loc_upd_req *lu; |
| 293 | char mi_string[GSM48_MI_SIZE]; |
| 294 | |
Holger Hans Peter Freyther | 540f4c8 | 2010-05-14 19:24:06 +0800 | [diff] [blame] | 295 | if (length < sizeof(*lu)) { |
| 296 | LOGP(DNAT, LOGL_ERROR, |
| 297 | "LU does not fit. Length is %d \n", length); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | lu = (struct gsm48_loc_upd_req *) data; |
| 302 | mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; |
| 303 | |
| 304 | /* |
| 305 | * We can only deal with the IMSI. This will fail for a phone that |
| 306 | * will send the TMSI of a previous network to us. |
| 307 | */ |
| 308 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 309 | return 0; |
| 310 | |
| 311 | gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 312 | return auth_imsi(bsc, mi_string); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 313 | } |
| 314 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 315 | static int _cr_check_cm_serv_req(struct bsc_connection *bsc, uint8_t *data, unsigned int length) |
| 316 | { |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 317 | static const uint32_t classmark_offset = |
| 318 | offsetof(struct gsm48_service_request, classmark); |
| 319 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 320 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 321 | uint8_t mi_type; |
| 322 | int rc; |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 323 | struct gsm48_service_request *req; |
| 324 | |
| 325 | /* unfortunately in Phase1 the classmark2 length is variable */ |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 326 | |
| 327 | if (length < sizeof(*req)) { |
| 328 | LOGP(DNAT, LOGL_ERROR, |
| 329 | "CM Serv Req does not fit. Length is %d\n", length); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | req = (struct gsm48_service_request *) data; |
Holger Hans Peter Freyther | 2ed35b3 | 2010-05-16 01:13:28 +0800 | [diff] [blame] | 334 | rc = gsm48_extract_mi((uint8_t *) &req->classmark, |
| 335 | length - classmark_offset, mi_string, &mi_type); |
| 336 | if (rc < 0) { |
| 337 | 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] | 338 | return -1; |
| 339 | } |
| 340 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 341 | /* we have to let the TMSI or such pass */ |
| 342 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 343 | return 0; |
| 344 | |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 345 | return auth_imsi(bsc, mi_string); |
| 346 | } |
| 347 | |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 348 | static int _cr_check_pag_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length) |
| 349 | { |
| 350 | struct gsm48_pag_resp *resp; |
| 351 | char mi_string[GSM48_MI_SIZE]; |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 352 | uint8_t mi_type; |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 353 | |
| 354 | if (length < sizeof(*resp)) { |
| 355 | LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | resp = (struct gsm48_pag_resp *) data; |
| 360 | if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { |
| 361 | LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | /* we need to let it pass for now */ |
| 366 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 367 | return 0; |
| 368 | |
| 369 | return auth_imsi(bsc, mi_string); |
| 370 | } |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 371 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 372 | static int _dt_check_id_resp(struct bsc_connection *bsc, |
| 373 | uint8_t *data, unsigned int length, |
| 374 | struct sccp_connections *con) |
| 375 | { |
| 376 | char mi_string[GSM48_MI_SIZE]; |
| 377 | uint8_t mi_type; |
| 378 | int ret; |
| 379 | |
| 380 | if (length < 2) { |
| 381 | LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | if (data[0] < length - 1) { |
| 386 | LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); |
| 387 | return -2; |
| 388 | } |
| 389 | |
| 390 | mi_type = data[1] & GSM_MI_TYPE_MASK; |
| 391 | gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); |
| 392 | |
| 393 | if (mi_type != GSM_MI_TYPE_IMSI) |
| 394 | return 0; |
| 395 | |
| 396 | ret = auth_imsi(bsc, mi_string); |
| 397 | con->imsi_checked = 1; |
| 398 | return ret; |
| 399 | } |
| 400 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 401 | /* Filter out CR data... */ |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 402 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed, int *con_type) |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 403 | { |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 404 | struct tlv_parsed tp; |
| 405 | struct gsm48_hdr *hdr48; |
| 406 | int hdr48_len; |
| 407 | int len; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 408 | uint8_t msg_type; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 409 | |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 410 | *con_type = NAT_CON_TYPE_NONE; |
| 411 | |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 412 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 413 | LOGP(DNAT, LOGL_ERROR, |
| 414 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | /* the parsed has had some basic l3 length check */ |
| 419 | len = msg->l3h[1]; |
| 420 | if (msgb_l3len(msg) - 3 < len) { |
| 421 | LOGP(DNAT, LOGL_ERROR, |
| 422 | "The CR Data has not enough space...\n"); |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | msg->l4h = &msg->l3h[3]; |
| 427 | len -= 1; |
| 428 | |
| 429 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 430 | |
| 431 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 432 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 437 | |
| 438 | if (hdr48_len < sizeof(*hdr48)) { |
| 439 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 444 | |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 445 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | 6c1610f | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 446 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 447 | msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 448 | *con_type = NAT_CON_TYPE_LU; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 449 | return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48)); |
Holger Hans Peter Freyther | 6c1610f | 2010-05-15 22:09:39 +0800 | [diff] [blame] | 450 | } else if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 451 | msg_type == GSM48_MT_MM_CM_SERV_REQ) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 452 | *con_type = NAT_CON_TYPE_CM_SERV_REQ; |
Holger Hans Peter Freyther | 2ebd52a | 2010-05-15 21:58:33 +0800 | [diff] [blame] | 453 | return _cr_check_cm_serv_req(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48)); |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 454 | } else if (hdr48->proto_discr == GSM48_PDISC_RR && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 455 | msg_type == GSM48_MT_RR_PAG_RESP) { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 456 | *con_type = NAT_CON_TYPE_PAG_RESP; |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 457 | return _cr_check_pag_resp(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48)); |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 458 | } else { |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 459 | /* 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] | 460 | *con_type = NAT_CON_TYPE_OTHER; |
Holger Hans Peter Freyther | 49c0c52 | 2010-05-16 01:05:47 +0800 | [diff] [blame] | 461 | return 0; |
Holger Hans Peter Freyther | 4ebb30e | 2010-05-14 08:14:09 +0800 | [diff] [blame] | 462 | } |
Holger Hans Peter Freyther | 6d35716 | 2010-05-14 03:39:56 +0800 | [diff] [blame] | 463 | } |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 464 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 465 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 466 | struct sccp_connections *con, struct bsc_nat_parsed *parsed) |
| 467 | { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 468 | uint32_t len; |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 469 | uint8_t msg_type; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 470 | struct gsm48_hdr *hdr48; |
| 471 | |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 472 | if (con->imsi_checked) |
| 473 | return 0; |
| 474 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 475 | /* only care about DTAP messages */ |
| 476 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 477 | return 0; |
| 478 | |
| 479 | /* gsm_type is actually the size of the dtap */ |
| 480 | len = parsed->gsm_type; |
| 481 | if (len < msgb_l3len(msg) - 3) { |
| 482 | LOGP(DNAT, LOGL_ERROR, "Not enough space for DTAP.\n"); |
| 483 | return -1; |
| 484 | } |
| 485 | |
| 486 | if (len < sizeof(*hdr48)) { |
| 487 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | msg->l4h = &msg->l3h[3]; |
| 492 | hdr48 = (struct gsm48_hdr *) msg->l4h; |
| 493 | |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 494 | msg_type = hdr48->msg_type & 0xbf; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 495 | if (hdr48->proto_discr == GSM48_PDISC_MM && |
Holger Hans Peter Freyther | efd367d | 2010-09-15 05:24:25 +0800 | [diff] [blame] | 496 | msg_type == GSM48_MT_MM_ID_RESP) { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 497 | return _dt_check_id_resp(bsc, &hdr48->data[0], len - sizeof(*hdr48), con); |
| 498 | } else { |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 499 | return 0; |
| 500 | } |
Holger Hans Peter Freyther | 1b4bde6 | 2010-09-15 01:11:08 +0800 | [diff] [blame] | 501 | } |
| 502 | |
Holger Hans Peter Freyther | dac2dee | 2010-05-14 18:38:29 +0800 | [diff] [blame] | 503 | void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
| 504 | { |
| 505 | if (*imsi) { |
| 506 | talloc_free(*imsi); |
| 507 | *imsi = NULL; |
| 508 | } |
| 509 | regfree(reg); |
| 510 | |
| 511 | if (argc > 0) { |
| 512 | *imsi = talloc_strdup(ctx, argv[0]); |
| 513 | regcomp(reg, argv[0], 0); |
| 514 | } |
| 515 | } |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 516 | |
| 517 | static const char *con_types [] = { |
| 518 | [NAT_CON_TYPE_NONE] = "n/a", |
| 519 | [NAT_CON_TYPE_LU] = "Location Update", |
| 520 | [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", |
| 521 | [NAT_CON_TYPE_PAG_RESP] = "Paging Response", |
Holger Hans Peter Freyther | 15a1f99 | 2010-05-16 20:43:52 +0800 | [diff] [blame] | 522 | [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", |
Holger Hans Peter Freyther | 8e69f0c | 2010-05-16 02:06:11 +0800 | [diff] [blame] | 523 | [NAT_CON_TYPE_OTHER] = "Other", |
| 524 | }; |
| 525 | |
| 526 | const char *bsc_con_type_to_string(int type) |
| 527 | { |
| 528 | return con_types[type]; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 529 | } |
| 530 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 531 | 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] | 532 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 533 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 534 | |
| 535 | if (!name) |
| 536 | return NULL; |
| 537 | |
| 538 | llist_for_each_entry(lst, &nat->access_lists, list) |
| 539 | if (strcmp(lst->name, name) == 0) |
| 540 | return lst; |
| 541 | |
| 542 | return NULL; |
| 543 | } |
| 544 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 545 | 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] | 546 | { |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 547 | struct bsc_nat_acc_lst *lst; |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 548 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 549 | lst = bsc_nat_acc_lst_find(nat, name); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 550 | if (lst) |
| 551 | return lst; |
| 552 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 553 | lst = talloc_zero(nat, struct bsc_nat_acc_lst); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 554 | if (!lst) { |
| 555 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 556 | return NULL; |
| 557 | } |
| 558 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 559 | INIT_LLIST_HEAD(&lst->fltr_list); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 560 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 561 | llist_add_tail(&lst->list, &nat->access_lists); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 562 | return lst; |
Holger Hans Peter Freyther | 704b350 | 2010-06-03 01:44:05 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 565 | 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] | 566 | { |
| 567 | llist_del(&lst->list); |
| 568 | talloc_free(lst); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) |
| 572 | { |
| 573 | struct bsc_nat_acc_lst_entry *entry; |
| 574 | |
| 575 | entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); |
| 576 | if (!entry) |
| 577 | return NULL; |
| 578 | |
Holger Hans Peter Freyther | 6b76250 | 2010-06-08 11:00:09 +0800 | [diff] [blame] | 579 | llist_add_tail(&entry->list, &lst->fltr_list); |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 580 | return entry; |
Holger Hans Peter Freyther | ccefba6 | 2010-06-17 18:16:00 +0800 | [diff] [blame] | 581 | } |
Holger Hans Peter Freyther | 4d0fbda | 2010-07-05 14:39:44 +0800 | [diff] [blame] | 582 | |
| 583 | int bsc_nat_msc_is_connected(struct bsc_nat *nat) |
| 584 | { |
| 585 | return nat->msc_con->is_connected; |
| 586 | } |