diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 7e66517de..a974195c8 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -8,6 +8,7 @@ #include struct vty; +struct gsm48_hdr; /* TODO: remove */ struct bsc_nat_parsed; @@ -64,11 +65,13 @@ int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *l /** * Content filtering. */ -int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, - struct bsc_nat_parsed *, int *con_type, char **imsi, +int bsc_msg_filter_initial(struct gsm48_hdr *hdr, size_t size, + struct bsc_connection *bsc, + int *con_type, char **imsi, struct bsc_filter_reject_cause *cause); -int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, - struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, +int bsc_msg_filter_data(struct gsm48_hdr *hdr, size_t size, + struct bsc_connection *bsc, + struct nat_sccp_connection *con, struct bsc_filter_reject_cause *cause); /* IMSI allow/deny handling */ diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h index b9f1e56f8..10f11a62b 100644 --- a/openbsc/include/openbsc/bsc_nat.h +++ b/openbsc/include/openbsc/bsc_nat.h @@ -22,6 +22,7 @@ #define BSC_NAT_H #include "mgcp.h" +#include "bsc_msg_filter.h" #include @@ -435,6 +436,13 @@ int bsc_nat_handle_ctrlif_msg(struct bsc_connection *bsc, struct msgb *msg); int bsc_nat_extract_lac(struct bsc_connection *bsc, struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, struct msgb *msg); +int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, + struct bsc_nat_parsed *, int *con_type, char **imsi, + struct bsc_filter_reject_cause *cause); +int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, + struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, + struct bsc_filter_reject_cause *cause); + /** * CTRL interface helper */ diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 5f8bff004..a1271a095 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -328,14 +328,12 @@ static int _dt_check_id_resp(struct bsc_connection *bsc, /* Filter out CR data... */ -int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, - struct bsc_nat_parsed *parsed, int *con_type, +int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, + struct bsc_connection *bsc, + int *con_type, char **imsi, struct bsc_filter_reject_cause *cause) { - struct tlv_parsed tp; - struct gsm48_hdr *hdr48; - int hdr48_len; - int len, ret = 0; + int ret = 0; uint8_t msg_type, proto; *con_type = NAT_CON_TYPE_NONE; @@ -343,39 +341,6 @@ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; *imsi = NULL; - if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { - LOGP(DNAT, LOGL_ERROR, - "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); - return -1; - } - - /* the parsed has had some basic l3 length check */ - len = msg->l3h[1]; - if (msgb_l3len(msg) - 3 < len) { - LOGP(DNAT, LOGL_ERROR, - "The CR Data has not enough space...\n"); - return -1; - } - - msg->l4h = &msg->l3h[3]; - len -= 1; - - tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); - - if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { - LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); - return -1; - } - - hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); - - if (hdr48_len < sizeof(*hdr48)) { - LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); - return -1; - } - - hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); - proto = hdr48->proto_discr & 0x0f; msg_type = hdr48->msg_type & 0xbf; if (proto == GSM48_PDISC_MM && @@ -412,13 +377,12 @@ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, return auth_imsi(bsc, *imsi, cause); } -int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, - struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, +int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, + struct bsc_connection *bsc, + struct nat_sccp_connection *con, struct bsc_filter_reject_cause *cause) { - uint32_t len; uint8_t msg_type, proto; - struct gsm48_hdr *hdr48; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; @@ -426,14 +390,6 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, if (con->imsi_checked) return 0; - /* only care about DTAP messages */ - if (parsed->bssap != BSSAP_MSG_DTAP) - return 0; - - hdr48 = bsc_unpack_dtap(parsed, msg, &len); - if (!hdr48) - return -1; - proto = hdr48->proto_discr & 0x0f; msg_type = hdr48->msg_type & 0xbf; if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP) diff --git a/openbsc/src/osmo-bsc_nat/Makefile.am b/openbsc/src/osmo-bsc_nat/Makefile.am index 7a38430f9..d96a3911f 100644 --- a/openbsc/src/osmo-bsc_nat/Makefile.am +++ b/openbsc/src/osmo-bsc_nat/Makefile.am @@ -7,7 +7,7 @@ bin_PROGRAMS = osmo-bsc_nat osmo_bsc_nat_SOURCES = bsc_filter.c bsc_mgcp_utils.c bsc_nat.c bsc_nat_utils.c \ bsc_nat_vty.c bsc_sccp.c bsc_ussd.c bsc_nat_ctrl.c \ - bsc_nat_rewrite.c bsc_nat_rewrite_trie.c + bsc_nat_rewrite.c bsc_nat_rewrite_trie.c bsc_nat_filter.c osmo_bsc_nat_LDADD = \ $(top_builddir)/src/libmgcp/libmgcp.a \ $(top_builddir)/src/libbsc/libbsc.a \ diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c new file mode 100644 index 000000000..11d370c4c --- /dev/null +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c @@ -0,0 +1,105 @@ +/* + * (C) 2010-2015 by Holger Hans Peter Freyther + * (C) 2010-2012 by On-Waves + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +#include +#include +#include +#include + +#include + +#include +#include + +#include + +/* Filter out CR data... */ +int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, + struct bsc_nat_parsed *parsed, int *con_type, + char **imsi, struct bsc_filter_reject_cause *cause) +{ + struct tlv_parsed tp; + struct gsm48_hdr *hdr48; + int hdr48_len; + int len; + + *con_type = NAT_CON_TYPE_NONE; + cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + *imsi = NULL; + + if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { + LOGP(DNAT, LOGL_ERROR, + "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); + return -1; + } + + /* the parsed has had some basic l3 length check */ + len = msg->l3h[1]; + if (msgb_l3len(msg) - 3 < len) { + LOGP(DNAT, LOGL_ERROR, + "The CR Data has not enough space...\n"); + return -1; + } + + msg->l4h = &msg->l3h[3]; + len -= 1; + + tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); + + if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { + LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); + return -1; + } + + hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); + + if (hdr48_len < sizeof(*hdr48)) { + LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); + return -1; + } + + hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); + return bsc_msg_filter_initial(hdr48, hdr48_len, bsc, con_type, imsi, cause); +} + +int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, + struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, + struct bsc_filter_reject_cause *cause) +{ + uint32_t len; + struct gsm48_hdr *hdr48; + + cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + + if (con->imsi_checked) + return 0; + + /* only care about DTAP messages */ + if (parsed->bssap != BSSAP_MSG_DTAP) + return 0; + + hdr48 = bsc_unpack_dtap(parsed, msg, &len); + if (!hdr48) + return -1; + + return bsc_msg_filter_data(hdr48, len, bsc, con, cause); +} diff --git a/openbsc/tests/bsc-nat/Makefile.am b/openbsc/tests/bsc-nat/Makefile.am index 64cd9ac85..26e550098 100644 --- a/openbsc/tests/bsc-nat/Makefile.am +++ b/openbsc/tests/bsc-nat/Makefile.am @@ -12,7 +12,8 @@ bsc_nat_test_SOURCES = bsc_nat_test.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_utils.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_rewrite.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_rewrite_trie.c \ - $(top_srcdir)/src/osmo-bsc_nat/bsc_mgcp_utils.c + $(top_srcdir)/src/osmo-bsc_nat/bsc_mgcp_utils.c \ + $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_filter.c bsc_nat_test_LDADD = \ $(top_builddir)/src/libfilter/libfilter.a \ $(top_builddir)/src/libbsc/libbsc.a \