blob: 6a9e99fb8ed0c32d1b07b335412ae55f6cc18c2a [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
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010028#include <osmocom/core/talloc.h>
Harald Welted36ff762011-03-23 18:26:56 +010029#include <osmocom/gsm/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 Freyther368a0a72011-01-07 16:54:46 +010072 { IPAC_PROTO_MGCP_OLD, 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)) {
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +0200101 LOGP(DLINP, LOGL_ERROR, "Wrong input length?\n");
Holger Hans Peter Freytherea8cbd52010-03-29 14:58:43 +0200102 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;
Holger Hans Peter Freyther1b9902c2013-09-03 14:35:33 +0200123 if (parsed->dest_local_ref)
124 parsed->original_dest_ref = *parsed->dest_local_ref;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800125 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 Freyther1d6fb182010-01-30 11:53:30 +0100138int bsc_nat_filter_ipa(int dir, struct msgb *msg, struct bsc_nat_parsed *parsed)
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800139{
140 int i;
141
142 /* go through the blacklist now */
143 for (i = 0; i < ARRAY_SIZE(black_list); ++i) {
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100144 /* 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 Freyther0b8f69d2010-06-15 18:45:38 +0800149 /* 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 Freyther0dc569a2010-03-29 14:59:59 +0200171 LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100172 return 1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800173 } else {
174 /* blacklisted, we have no content sniffing yet */
Holger Hans Peter Freyther0dc569a2010-03-29 14:59:59 +0200175 LOGP(DNAT, LOGL_INFO, "Blacklisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100176 return 1;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800177 }
178 }
179
180 /* go through the whitelust now */
181 for (i = 0; i < ARRAY_SIZE(white_list); ++i) {
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100182 /* 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 Freyther0b8f69d2010-06-15 18:45:38 +0800187 /* 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 Freyther0dc569a2010-03-29 14:59:59 +0200209 LOGP(DNAT, LOGL_INFO, "Whitelisted with rule %d\n", i);
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100210 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800211 } else {
212 /* whitelisted */
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100213 return 0;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800214 }
215 }
216
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100217 return 1;
Holger Hans Peter Freyther6a97b8d2010-06-15 18:45:26 +0800218}