blob: 3498132c379fc8776e6baaed8a7c7d9978dcbdd2 [file] [log] [blame]
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +08001
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 Freytherbae9da42010-03-30 05:57:42 +020027#include <openbsc/bssap.h>
28#include <openbsc/debug.h>
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +020029#include <openbsc/ipaccess.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080030
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020031#include <osmocore/linuxlist.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080032#include <osmocore/talloc.h>
Holger Hans Peter Freyther13959482010-06-15 18:50:57 +080033#include <osmocore/gsm0808.h>
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080034
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +020035#include <sccp/sccp.h>
36
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +020037#include <netinet/in.h>
38#include <arpa/inet.h>
39
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080040struct bsc_nat *bsc_nat_alloc(void)
41{
42 struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
43 if (!nat)
44 return NULL;
45
46 INIT_LLIST_HEAD(&nat->sccp_connections);
47 INIT_LLIST_HEAD(&nat->bsc_connections);
48 INIT_LLIST_HEAD(&nat->bsc_configs);
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020049 nat->stats.sccp.conn = counter_alloc("nat.sccp.conn");
50 nat->stats.sccp.calls = counter_alloc("nat.sccp.calls");
51 nat->stats.bsc.reconn = counter_alloc("nat.bsc.conn");
52 nat->stats.bsc.auth_fail = counter_alloc("nat.bsc.auth_fail");
53 nat->stats.msc.reconn = counter_alloc("nat.msc.conn");
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080054 nat->msc_ip = talloc_strdup(nat, "127.0.0.1");
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +020055 nat->msc_port = 5000;
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +080056 nat->auth_timeout = 2;
57 nat->ping_timeout = 20;
58 nat->pong_timeout = 5;
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080059 return nat;
60}
61
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080062void bsc_nat_set_msc_ip(struct bsc_nat *nat, const char *ip)
63{
64 if (nat->msc_ip)
65 talloc_free(nat->msc_ip);
66 nat->msc_ip = talloc_strdup(nat, ip);
67}
68
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080069struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat)
70{
71 struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
72 if (!con)
73 return NULL;
74
Holger Hans Peter Freytherf8048d92010-03-29 15:14:15 +020075 con->nat = nat;
Holger Hans Peter Freyther81519732010-04-22 12:05:23 +080076 write_queue_init(&con->write_queue, 100);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080077 return con;
78}
79
80struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
81{
82 struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
83 if (!conf)
84 return NULL;
85
86 conf->token = talloc_strdup(conf, token);
87 conf->lac = lac;
88 conf->nr = nat->num_bsc;
89 conf->nat = nat;
90
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020091 llist_add_tail(&conf->entry, &nat->bsc_configs);
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080092 ++nat->num_bsc;
93
Holger Hans Peter Freytherd4702862010-04-12 12:17:09 +020094 conf->stats.sccp.conn = counter_alloc("nat.bsc.sccp.conn");
95 conf->stats.sccp.calls = counter_alloc("nat.bsc.sccp.calls");
96 conf->stats.net.reconn = counter_alloc("nat.bsc.net.reconnects");
97
Holger Hans Peter Freytherdcf8a7d2010-06-15 18:48:01 +080098 return conf;
99}
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200100
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200101void sccp_connection_destroy(struct sccp_connections *conn)
102{
103 LOGP(DNAT, LOGL_DEBUG, "Destroy 0x%x <-> 0x%x mapping for con %p\n",
104 sccp_src_ref_to_int(&conn->real_ref),
105 sccp_src_ref_to_int(&conn->patched_ref), conn->bsc);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800106 bsc_mgcp_dlcx(conn);
Holger Hans Peter Freyther23fe7be2010-03-30 10:45:48 +0200107 llist_del(&conn->list_entry);
108 talloc_free(conn);
109}
110
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200111struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *lac_out)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200112{
113 struct bsc_connection *bsc;
114 int data_length;
115 const u_int8_t *data;
116 struct tlv_parsed tp;
117 int i = 0;
118
Holger Hans Peter Freyther7a773692010-04-18 02:41:20 +0800119 *lac_out = -1;
120
Holger Hans Peter Freythere9be5172010-03-30 06:51:23 +0200121 if (!msg->l3h || msgb_l3len(msg) < 3) {
122 LOGP(DNAT, LOGL_ERROR, "Paging message is too short.\n");
123 return NULL;
124 }
125
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200126 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l3h + 3, msgb_l3len(msg) - 3, 0, 0);
127 if (!TLVP_PRESENT(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST)) {
128 LOGP(DNAT, LOGL_ERROR, "No CellIdentifier List inside paging msg.\n");
129 return NULL;
130 }
131
132 data_length = TLVP_LEN(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
133 data = TLVP_VAL(&tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
Holger Hans Peter Freythera4376ad2010-04-21 18:47:24 +0800134
135 /* No need to try a different BSS */
136 if (data[0] == CELL_IDENT_BSS) {
137 return NULL;
138 } else if (data[0] != CELL_IDENT_LAC) {
Holger Hans Peter Freyther530c4b12010-04-06 10:25:40 +0200139 LOGP(DNAT, LOGL_ERROR, "Unhandled cell ident discrminator: %d\n", data[0]);
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200140 return NULL;
141 }
142
143 /* Currently we only handle one BSC */
144 for (i = 1; i < data_length - 1; i += 2) {
145 unsigned int _lac = ntohs(*(unsigned int *) &data[i]);
Holger Hans Peter Freyther979a3092010-04-17 08:07:19 +0200146 *lac_out = _lac;
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200147 llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200148 if (!bsc->cfg)
149 continue;
150 if (!bsc->authenticated || _lac != bsc->cfg->lac)
Holger Hans Peter Freytherbae9da42010-03-30 05:57:42 +0200151 continue;
152
153 return bsc;
154 }
155 }
156
157 return NULL;
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200158}
159
160int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length)
161{
162 struct msgb *msg;
163
164 if (length > 4096 - 128) {
165 LOGP(DINP, LOGL_ERROR, "Can not send message of that size.\n");
166 return -1;
167 }
168
169 msg = msgb_alloc_headroom(4096, 128, "to-bsc");
170 if (!msg) {
171 LOGP(DINP, LOGL_ERROR, "Failed to allocate memory for BSC msg.\n");
172 return -1;
173 }
174
175 /* copy the data */
176 msg->l3h = msgb_put(msg, length);
177 memcpy(msg->l3h, data, length);
178
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200179 return bsc_write(bsc, msg, NAT_IPAC_PROTO_MGCP);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200180}
181
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200182int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int proto)
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200183{
184 /* prepend the header */
Holger Hans Peter Freyther2896df72010-04-08 10:24:57 +0200185 ipaccess_prepend_header(msg, proto);
Holger Hans Peter Freythera0df82d2010-04-01 08:21:33 +0200186
187 if (write_queue_enqueue(&bsc->write_queue, msg) != 0) {
188 LOGP(DINP, LOGL_ERROR, "Failed to enqueue the write.\n");
189 msgb_free(msg);
190 return -1;
191 }
192
193 return 0;
194}
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800195
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800196/* apply white/black list */
197static int auth_imsi(struct bsc_connection *bsc, const char *mi_string)
198{
199 regmatch_t match[1];
200
201 /*
202 * Now apply blacklist/whitelist of the BSC and the NAT.
203 * 1.) Reject if the IMSI is not allowed at the BSC
204 * 2.) Allow directly if the IMSI is allowed at the BSC
205 * 3.) Reject if the IMSI not allowed at the global level.
206 * 4.) Allow directly if the IMSI is allowed at the global level
207 */
208
209 /* 1. BSC deny */
210 if (bsc->cfg->imsi_deny) {
211 if (regexec(&bsc->cfg->imsi_deny_re, mi_string, 1, match, 0) == 0) {
212 LOGP(DNAT, LOGL_ERROR,
213 "Filtering %s by imsi_deny.\n", mi_string);
214 return -2;
215 }
216 }
217
218 /* 2. BSC allow */
219 if (bsc->cfg->imsi_allow) {
220 if (regexec(&bsc->cfg->imsi_allow_re, mi_string, 1, match, 0) == 0)
221 return 0;
222 }
223
224 /* 3. NAT deny */
225 if (bsc->nat->imsi_deny) {
226 if (regexec(&bsc->nat->imsi_deny_re, mi_string, 1, match, 0) == 0) {
227 LOGP(DNAT, LOGL_ERROR,
228 "Filtering %s by nat imsi_deny.\n", mi_string);
229 return -3;
230 }
231 }
232
233 /* 4. NAT allow */
234 if (bsc->nat->imsi_allow) {
235 if (regexec(&bsc->nat->imsi_allow_re, mi_string, 0, NULL, 0) == 0)
236 return 0;
237 } else {
238 return 0;
239 }
240
241 /* unmatched */
242 return -3;
243}
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800244
245static int _cr_check_loc_upd(struct bsc_connection *bsc, uint8_t *data, unsigned int length)
246{
247 u_int8_t mi_type;
248 struct gsm48_loc_upd_req *lu;
249 char mi_string[GSM48_MI_SIZE];
250
Holger Hans Peter Freytherf8303222010-05-14 19:24:06 +0800251 if (length < sizeof(*lu)) {
252 LOGP(DNAT, LOGL_ERROR,
253 "LU does not fit. Length is %d \n", length);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800254 return -1;
255 }
256
257 lu = (struct gsm48_loc_upd_req *) data;
258 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
259
260 /*
261 * We can only deal with the IMSI. This will fail for a phone that
262 * will send the TMSI of a previous network to us.
263 */
264 if (mi_type != GSM_MI_TYPE_IMSI)
265 return 0;
266
267 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800268 return auth_imsi(bsc, mi_string);
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800269}
270
271
272/* Filter out CR data... */
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800273int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed)
274{
Holger Hans Peter Freyther290ed9a2010-05-14 08:14:09 +0800275 struct tlv_parsed tp;
276 struct gsm48_hdr *hdr48;
277 int hdr48_len;
278 int len;
279
280 if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) {
281 LOGP(DNAT, LOGL_ERROR,
282 "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type);
283 return -1;
284 }
285
286 /* the parsed has had some basic l3 length check */
287 len = msg->l3h[1];
288 if (msgb_l3len(msg) - 3 < len) {
289 LOGP(DNAT, LOGL_ERROR,
290 "The CR Data has not enough space...\n");
291 return -1;
292 }
293
294 msg->l4h = &msg->l3h[3];
295 len -= 1;
296
297 tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0);
298
299 if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) {
300 LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n");
301 return -1;
302 }
303
304 hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION);
305
306 if (hdr48_len < sizeof(*hdr48)) {
307 LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n");
308 return -1;
309 }
310
311 hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION);
312
313 if (hdr48->msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) {
314 return _cr_check_loc_upd(bsc, &hdr48->data[0], hdr48_len - sizeof(*hdr48));
315 } else {
316 return 0;
317 }
Holger Hans Peter Freytherb4af5c92010-05-14 03:39:56 +0800318}
Holger Hans Peter Freyther12dc89a2010-05-14 18:38:29 +0800319
320void bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
321{
322 if (*imsi) {
323 talloc_free(*imsi);
324 *imsi = NULL;
325 }
326 regfree(reg);
327
328 if (argc > 0) {
329 *imsi = talloc_strdup(ctx, argv[0]);
330 regcomp(reg, argv[0], 0);
331 }
332}