Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 1 | /* BSC Multiplexer/NAT */ |
| 2 | |
| 3 | /* |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 24 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 25 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 26 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 27 | |
Pablo Neira Ayuso | 136f453 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 28 | #include <osmocom/core/talloc.h> |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 29 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 30 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 31 | #include <osmocom/sccp/sccp.h> |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * The idea is to have a simple struct describing a IPA packet with |
| 35 | * SCCP SSN and the GSM 08.08 payload and decide. We will both have |
| 36 | * a white and a blacklist of packets we want to handle. |
| 37 | * |
| 38 | * TODO: Implement a "NOT" in the filter language. |
| 39 | */ |
| 40 | |
| 41 | #define ALLOW_ANY -1 |
| 42 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 43 | #define FILTER_TO_BSC 1 |
| 44 | #define FILTER_TO_MSC 2 |
| 45 | #define FILTER_TO_BOTH 3 |
| 46 | |
| 47 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 48 | struct bsc_pkt_filter { |
| 49 | int ipa_proto; |
| 50 | int dest_ssn; |
| 51 | int bssap; |
| 52 | int gsm; |
| 53 | int filter_dir; |
| 54 | }; |
| 55 | |
| 56 | static struct bsc_pkt_filter black_list[] = { |
| 57 | /* filter reset messages to the MSC */ |
| 58 | { IPAC_PROTO_SCCP, SCCP_SSN_BSSAP, 0, BSS_MAP_MSG_RESET, FILTER_TO_MSC }, |
| 59 | |
| 60 | /* filter reset ack messages to the BSC */ |
| 61 | { IPAC_PROTO_SCCP, SCCP_SSN_BSSAP, 0, BSS_MAP_MSG_RESET_ACKNOWLEDGE, FILTER_TO_BSC }, |
| 62 | |
| 63 | /* filter ip access */ |
| 64 | { IPAC_PROTO_IPACCESS, ALLOW_ANY, ALLOW_ANY, ALLOW_ANY, FILTER_TO_MSC }, |
| 65 | }; |
| 66 | |
| 67 | static struct bsc_pkt_filter white_list[] = { |
| 68 | /* allow IPAC_PROTO_SCCP messages to both sides */ |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 69 | { IPAC_PROTO_SCCP, ALLOW_ANY, ALLOW_ANY, ALLOW_ANY, FILTER_TO_BOTH }, |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 70 | |
| 71 | /* allow MGCP messages to both sides */ |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 72 | { IPAC_PROTO_MGCP_OLD, ALLOW_ANY, ALLOW_ANY, ALLOW_ANY, FILTER_TO_BOTH }, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
Holger Hans Peter Freyther | adb6e1c | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 75 | struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg) |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 76 | { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 77 | struct sccp_parse_result result; |
| 78 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 79 | struct ipaccess_head *hh; |
| 80 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 81 | /* quick fail */ |
| 82 | if (msg->len < 4) |
| 83 | return NULL; |
| 84 | |
| 85 | parsed = talloc_zero(msg, struct bsc_nat_parsed); |
| 86 | if (!parsed) |
| 87 | return NULL; |
| 88 | |
| 89 | /* more init */ |
| 90 | parsed->ipa_proto = parsed->called_ssn = parsed->calling_ssn = -1; |
| 91 | parsed->sccp_type = parsed->bssap = parsed->gsm_type = -1; |
| 92 | |
| 93 | /* start parsing */ |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 94 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 95 | parsed->ipa_proto = hh->proto; |
| 96 | |
| 97 | msg->l2h = &hh->data[0]; |
| 98 | |
Holger Hans Peter Freyther | ea8cbd5 | 2010-03-29 14:58:43 +0200 | [diff] [blame] | 99 | /* do a size check on the input */ |
| 100 | if (ntohs(hh->len) != msgb_l2len(msg)) { |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 101 | LOGP(DLINP, LOGL_ERROR, "Wrong input length?\n"); |
Holger Hans Peter Freyther | ea8cbd5 | 2010-03-29 14:58:43 +0200 | [diff] [blame] | 102 | talloc_free(parsed); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 106 | /* analyze sccp down here */ |
| 107 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 108 | memset(&result, 0, sizeof(result)); |
| 109 | if (sccp_parse_header(msg, &result) != 0) { |
| 110 | talloc_free(parsed); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | if (msg->l3h && msgb_l3len(msg) < 3) { |
| 115 | LOGP(DNAT, LOGL_ERROR, "Not enough space or GSM payload\n"); |
| 116 | talloc_free(parsed); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | parsed->sccp_type = sccp_determine_msg_type(msg); |
| 121 | parsed->src_local_ref = result.source_local_reference; |
| 122 | parsed->dest_local_ref = result.destination_local_reference; |
Holger Hans Peter Freyther | 1b9902c | 2013-09-03 14:35:33 +0200 | [diff] [blame] | 123 | if (parsed->dest_local_ref) |
| 124 | parsed->original_dest_ref = *parsed->dest_local_ref; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 125 | parsed->called_ssn = result.called.ssn; |
| 126 | parsed->calling_ssn = result.calling.ssn; |
| 127 | |
| 128 | /* in case of connection confirm we have no payload */ |
| 129 | if (msg->l3h) { |
| 130 | parsed->bssap = msg->l3h[0]; |
| 131 | parsed->gsm_type = msg->l3h[2]; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return parsed; |
| 136 | } |
| 137 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 138 | int bsc_nat_filter_ipa(int dir, struct msgb *msg, struct bsc_nat_parsed *parsed) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 139 | { |
| 140 | int i; |
| 141 | |
| 142 | /* go through the blacklist now */ |
| 143 | for (i = 0; i < ARRAY_SIZE(black_list); ++i) { |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 144 | /* ignore the rule? */ |
| 145 | if (black_list[i].filter_dir != FILTER_TO_BOTH |
| 146 | && black_list[i].filter_dir != dir) |
| 147 | continue; |
| 148 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 149 | /* the proto is not blacklisted */ |
| 150 | if (black_list[i].ipa_proto != ALLOW_ANY |
| 151 | && black_list[i].ipa_proto != parsed->ipa_proto) |
| 152 | continue; |
| 153 | |
| 154 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 155 | /* the SSN is not blacklisted */ |
| 156 | if (black_list[i].dest_ssn != ALLOW_ANY |
| 157 | && black_list[i].dest_ssn != parsed->called_ssn) |
| 158 | continue; |
| 159 | |
| 160 | /* bssap */ |
| 161 | if (black_list[i].bssap != ALLOW_ANY |
| 162 | && black_list[i].bssap != parsed->bssap) |
| 163 | continue; |
| 164 | |
| 165 | /* gsm */ |
| 166 | if (black_list[i].gsm != ALLOW_ANY |
| 167 | && black_list[i].gsm != parsed->gsm_type) |
| 168 | continue; |
| 169 | |
| 170 | /* blacklisted */ |
Holger Hans Peter Freyther | 0dc569a | 2010-03-29 14:59:59 +0200 | [diff] [blame] | 171 | LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i); |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 172 | return 1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 173 | } else { |
| 174 | /* blacklisted, we have no content sniffing yet */ |
Holger Hans Peter Freyther | 0dc569a | 2010-03-29 14:59:59 +0200 | [diff] [blame] | 175 | LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i); |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 176 | return 1; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /* go through the whitelust now */ |
| 181 | for (i = 0; i < ARRAY_SIZE(white_list); ++i) { |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 182 | /* ignore the rule? */ |
| 183 | if (white_list[i].filter_dir != FILTER_TO_BOTH |
| 184 | && white_list[i].filter_dir != dir) |
| 185 | continue; |
| 186 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 187 | /* the proto is not whitelisted */ |
| 188 | if (white_list[i].ipa_proto != ALLOW_ANY |
| 189 | && white_list[i].ipa_proto != parsed->ipa_proto) |
| 190 | continue; |
| 191 | |
| 192 | if (parsed->ipa_proto == IPAC_PROTO_SCCP) { |
| 193 | /* the SSN is not whitelisted */ |
| 194 | if (white_list[i].dest_ssn != ALLOW_ANY |
| 195 | && white_list[i].dest_ssn != parsed->called_ssn) |
| 196 | continue; |
| 197 | |
| 198 | /* bssap */ |
| 199 | if (white_list[i].bssap != ALLOW_ANY |
| 200 | && white_list[i].bssap != parsed->bssap) |
| 201 | continue; |
| 202 | |
| 203 | /* gsm */ |
| 204 | if (white_list[i].gsm != ALLOW_ANY |
| 205 | && white_list[i].gsm != parsed->gsm_type) |
| 206 | continue; |
| 207 | |
| 208 | /* whitelisted */ |
Holger Hans Peter Freyther | 0dc569a | 2010-03-29 14:59:59 +0200 | [diff] [blame] | 209 | LOGP(DNAT, LOGL_INFO, "Whitelisted with rule %d\n", i); |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 210 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 211 | } else { |
| 212 | /* whitelisted */ |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 213 | return 0; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 217 | return 1; |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 218 | } |