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