blob: f22f5bee15395fb8f9c89d63b438d76ed5c3ff74 [file] [log] [blame]
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08001/* BSC Multiplexer/NAT */
2
3/*
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +08006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * 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 Freyther6a97b8d2010-06-15 18:45:26 +080011 * (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 Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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 Freyther6a97b8d2010-06-15 18:45:26 +080020 *
21 */
22
23#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080024#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080025#include <openbsc/ipaccess.h>
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080026#include <openbsc/debug.h>
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080027
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080028#include <osmocore/talloc.h>
Holger Hans Peter Freyther69d801e2010-06-15 20:13:33 +080029#include <osmocore/protocol/gsm_08_08.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080030
Harald Welted5db12c2010-08-03 15:11:51 +020031#include <osmocom/sccp/sccp.h>
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080032
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 Freyther1d6fb182010-01-30 11:53:30 +010043#define FILTER_TO_BSC 1
44#define FILTER_TO_MSC 2
45#define FILTER_TO_BOTH 3
46
47
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080048struct bsc_pkt_filter {
49 int ipa_proto;
50 int dest_ssn;
51 int bssap;
52 int gsm;
53 int filter_dir;
54};
55
56static 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
67static struct bsc_pkt_filter white_list[] = {
68 /* allow IPAC_PROTO_SCCP messages to both sides */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +010069 { IPAC_PROTO_SCCP, ALLOW_ANY, ALLOW_ANY, ALLOW_ANY, FILTER_TO_BOTH },
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +020070
71 /* allow MGCP messages to both sides */
Holger Hans Peter Freyther19c530c2010-10-13 23:52:01 +020072 { IPAC_PROTO_MGCP, ALLOW_ANY, ALLOW_ANY, ALLOW_ANY, FILTER_TO_BOTH },
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080073};
74
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +080075struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg)
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080076{
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080077 struct sccp_parse_result result;
78 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +080079 struct ipaccess_head *hh;
80
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080081 /* 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 Freyther6a97b8d2010-06-15 18:45:26 +080094 hh = (struct ipaccess_head *) msg->data;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080095 parsed->ipa_proto = hh->proto;
96
97 msg->l2h = &hh->data[0];
98
Holger Hans Peter Freytherea8cbd52010-03-29 14:58:43 +020099 /* do a size check on the input */
100 if (ntohs(hh->len) != msgb_l2len(msg)) {
101 LOGP(DINP, LOGL_ERROR, "Wrong input length?\n");
102 talloc_free(parsed);
103 return NULL;
104 }
105
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800106 /* 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;
123 parsed->called_ssn = result.called.ssn;
124 parsed->calling_ssn = result.calling.ssn;
125
126 /* in case of connection confirm we have no payload */
127 if (msg->l3h) {
128 parsed->bssap = msg->l3h[0];
129 parsed->gsm_type = msg->l3h[2];
130 }
131 }
132
133 return parsed;
134}
135
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100136int bsc_nat_filter_ipa(int dir, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800137{
138 int i;
139
140 /* go through the blacklist now */
141 for (i = 0; i < ARRAY_SIZE(black_list); ++i) {
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100142 /* ignore the rule? */
143 if (black_list[i].filter_dir != FILTER_TO_BOTH
144 && black_list[i].filter_dir != dir)
145 continue;
146
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800147 /* the proto is not blacklisted */
148 if (black_list[i].ipa_proto != ALLOW_ANY
149 && black_list[i].ipa_proto != parsed->ipa_proto)
150 continue;
151
152 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
153 /* the SSN is not blacklisted */
154 if (black_list[i].dest_ssn != ALLOW_ANY
155 && black_list[i].dest_ssn != parsed->called_ssn)
156 continue;
157
158 /* bssap */
159 if (black_list[i].bssap != ALLOW_ANY
160 && black_list[i].bssap != parsed->bssap)
161 continue;
162
163 /* gsm */
164 if (black_list[i].gsm != ALLOW_ANY
165 && black_list[i].gsm != parsed->gsm_type)
166 continue;
167
168 /* blacklisted */
Holger Hans Peter Freyther0dc569a2010-03-29 14:59:59 +0200169 LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100170 return 1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800171 } else {
172 /* blacklisted, we have no content sniffing yet */
Holger Hans Peter Freyther0dc569a2010-03-29 14:59:59 +0200173 LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100174 return 1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800175 }
176 }
177
178 /* go through the whitelust now */
179 for (i = 0; i < ARRAY_SIZE(white_list); ++i) {
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100180 /* ignore the rule? */
181 if (white_list[i].filter_dir != FILTER_TO_BOTH
182 && white_list[i].filter_dir != dir)
183 continue;
184
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800185 /* the proto is not whitelisted */
186 if (white_list[i].ipa_proto != ALLOW_ANY
187 && white_list[i].ipa_proto != parsed->ipa_proto)
188 continue;
189
190 if (parsed->ipa_proto == IPAC_PROTO_SCCP) {
191 /* the SSN is not whitelisted */
192 if (white_list[i].dest_ssn != ALLOW_ANY
193 && white_list[i].dest_ssn != parsed->called_ssn)
194 continue;
195
196 /* bssap */
197 if (white_list[i].bssap != ALLOW_ANY
198 && white_list[i].bssap != parsed->bssap)
199 continue;
200
201 /* gsm */
202 if (white_list[i].gsm != ALLOW_ANY
203 && white_list[i].gsm != parsed->gsm_type)
204 continue;
205
206 /* whitelisted */
Holger Hans Peter Freyther0dc569a2010-03-29 14:59:59 +0200207 LOGP(DNAT, LOGL_INFO, "Whitelisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100208 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800209 } else {
210 /* whitelisted */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100211 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800212 }
213 }
214
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100215 return 1;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800216}