From 4247cead2b77b2ad8ae2a6c1b48a450309185bb3 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 18:42:46 +0200 Subject: filter: Move the gsm 04.08 filter to a common place For customer requirements we want to be able to do filtering on the BSC as well. The same messages need to be scanned and the same access-lists will be looked at. In the future we might even split traffic based on the IMSI. Begin with moving the code to a new top level directory and then renaming and removing the nat dependency. --- openbsc/configure.ac | 1 + openbsc/src/Makefile.am | 2 +- openbsc/src/libfilter/Makefile.am | 9 + openbsc/src/libfilter/bsc_msg_filter.c | 443 ++++++++++++++++++++++++++++++ openbsc/src/osmo-bsc_nat/Makefile.am | 3 +- openbsc/src/osmo-bsc_nat/bsc_nat_filter.c | 443 ------------------------------ openbsc/tests/bsc-nat/Makefile.am | 5 +- 7 files changed, 459 insertions(+), 447 deletions(-) create mode 100644 openbsc/src/libfilter/Makefile.am create mode 100644 openbsc/src/libfilter/bsc_msg_filter.c delete mode 100644 openbsc/src/osmo-bsc_nat/bsc_nat_filter.c diff --git a/openbsc/configure.ac b/openbsc/configure.ac index 00d1b70c9..394972f46 100644 --- a/openbsc/configure.ac +++ b/openbsc/configure.ac @@ -179,6 +179,7 @@ AC_OUTPUT( src/libmsc/Makefile src/libmgcp/Makefile src/libcommon/Makefile + src/libfilter/Makefile src/osmo-nitb/Makefile src/osmo-bsc/Makefile src/osmo-bsc_nat/Makefile diff --git a/openbsc/src/Makefile.am b/openbsc/src/Makefile.am index db7603b89..6f6174eb1 100644 --- a/openbsc/src/Makefile.am +++ b/openbsc/src/Makefile.am @@ -2,7 +2,7 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOGSM_CFLAGS) $(LIBOSMOVTY_CFLAGS) $(COVERAGE_CFLAGS) AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(LIBOSMOGSM_LIBS) $(COVERAGE_LDFLAGS) -SUBDIRS = libcommon libmgcp libbsc libmsc libtrau osmo-nitb osmo-bsc_mgcp utils ipaccess gprs +SUBDIRS = libcommon libmgcp libbsc libmsc libtrau libfilter osmo-nitb osmo-bsc_mgcp utils ipaccess gprs # Conditional modules if BUILD_NAT diff --git a/openbsc/src/libfilter/Makefile.am b/openbsc/src/libfilter/Makefile.am new file mode 100644 index 000000000..e24ec5f73 --- /dev/null +++ b/openbsc/src/libfilter/Makefile.am @@ -0,0 +1,9 @@ +AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir) +AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) \ + $(LIBOSMOVTY_CFLAGS) $(LIBOSMOABIS_CFLAGS) $(COVERAGE_CFLAGS) + +noinst_LIBRARIES = libfilter.a + +libfilter_a_SOURCES = \ + bsc_msg_filter.c + diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c new file mode 100644 index 000000000..64f6d7347 --- /dev/null +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -0,0 +1,443 @@ +/* + * Access filtering + */ +/* + * (C) 2010-2012 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 +#include + +#include +#include + +#include + +int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) +{ + struct bsc_nat_barr_entry *n; + n = rb_entry(root->rb_node, struct bsc_nat_barr_entry, node); + + while (n) { + int rc = strcmp(imsi, n->imsi); + if (rc == 0) { + *cm = n->cm_reject_cause; + *lu = n->lu_reject_cause; + return 1; + } + + n = rb_entry( + (rc < 0) ? n->node.rb_left : n->node.rb_right, + struct bsc_nat_barr_entry, node); + }; + + return 0; +} + +static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *root) +{ + struct rb_node **new = &root->rb_node, *parent = NULL; + + while (*new) { + int rc; + struct bsc_nat_barr_entry *this; + this = rb_entry(*new, struct bsc_nat_barr_entry, node); + parent = *new; + + rc = strcmp(entry->imsi, this->imsi); + if (rc < 0) + new = &((*new)->rb_left); + else if (rc > 0) + new = &((*new)->rb_right); + else { + LOGP(DNAT, LOGL_ERROR, + "Duplicate entry for IMSI(%s)\n", entry->imsi); + talloc_free(entry); + return -1; + } + } + + rb_link_node(&entry->node, parent, new); + rb_insert_color(&entry->node, root); + return 0; +} + +int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, + const struct osmo_config_list *list) +{ + struct osmo_config_entry *cfg_entry; + int err = 0; + + /* free the old data */ + while (!RB_EMPTY_ROOT(root)) { + struct rb_node *node = rb_first(root); + rb_erase(node, root); + talloc_free(node); + } + + if (!list) + return 0; + + /* now adapt the new list */ + llist_for_each_entry(cfg_entry, &list->entry, list) { + struct bsc_nat_barr_entry *entry; + entry = talloc_zero(ctx, struct bsc_nat_barr_entry); + if (!entry) { + LOGP(DNAT, LOGL_ERROR, + "Allocation of the barr entry failed.\n"); + continue; + } + + entry->imsi = talloc_strdup(entry, cfg_entry->mcc); + entry->cm_reject_cause = atoi(cfg_entry->mnc); + entry->lu_reject_cause = atoi(cfg_entry->option); + err |= insert_barr_node(entry, root); + } + + return err; +} + + +static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string, + int *cm_cause, int *lu_cause) +{ + struct bsc_nat_acc_lst_entry *entry; + + llist_for_each_entry(entry, &lst->fltr_list, list) { + if (!entry->imsi_deny) + continue; + if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) { + *cm_cause = entry->cm_reject_cause; + *lu_cause = entry->lu_reject_cause; + return 0; + } + } + + return 1; +} + +/* apply white/black list */ +static int auth_imsi(struct bsc_connection *bsc, const char *imsi, + struct bsc_nat_reject_cause *cause) +{ + /* + * Now apply blacklist/whitelist of the BSC and the NAT. + * 1.) Check the global IMSI barr list + * 2.) Allow directly if the IMSI is allowed at the BSC + * 3.) Reject if the IMSI is not allowed at the BSC + * 4.) Reject if the IMSI not allowed at the global level. + * 5.) Allow directly if the IMSI is allowed at the global level + */ + int cm, lu; + struct bsc_nat_acc_lst *nat_lst = NULL; + struct bsc_nat_acc_lst *bsc_lst = NULL; + + /* 1. global check for barred imsis */ + if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { + cause->cm_reject_cause = cm; + cause->lu_reject_cause = lu; + LOGP(DNAT, LOGL_DEBUG, + "Blocking subscriber IMSI %s with CM: %d LU: %d\n", + imsi, cm, lu); + return -4; + } + + + bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); + nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); + + + if (bsc_lst) { + /* 2. BSC allow */ + if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0) + return 1; + + /* 3. BSC deny */ + if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { + LOGP(DNAT, LOGL_ERROR, + "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); + rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); + cause->cm_reject_cause = cm; + cause->lu_reject_cause = lu; + return -2; + } + + } + + /* 4. NAT deny */ + if (nat_lst) { + if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { + LOGP(DNAT, LOGL_ERROR, + "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); + rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); + cause->cm_reject_cause = cm; + cause->lu_reject_cause = lu; + return -3; + } + } + + return 1; +} + +static int _cr_check_loc_upd(struct bsc_connection *bsc, + uint8_t *data, unsigned int length, + char **imsi) +{ + uint8_t mi_type; + struct gsm48_loc_upd_req *lu; + char mi_string[GSM48_MI_SIZE]; + + if (length < sizeof(*lu)) { + LOGP(DNAT, LOGL_ERROR, + "LU does not fit. Length is %d \n", length); + return -1; + } + + lu = (struct gsm48_loc_upd_req *) data; + mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; + + /* + * We can only deal with the IMSI. This will fail for a phone that + * will send the TMSI of a previous network to us. + */ + if (mi_type != GSM_MI_TYPE_IMSI) + return 0; + + gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); + *imsi = talloc_strdup(bsc, mi_string); + return 1; +} + +static int _cr_check_cm_serv_req(struct bsc_connection *bsc, + uint8_t *data, unsigned int length, + int *con_type, char **imsi) +{ + static const uint32_t classmark_offset = + offsetof(struct gsm48_service_request, classmark); + + char mi_string[GSM48_MI_SIZE]; + uint8_t mi_type; + int rc; + struct gsm48_service_request *req; + + /* unfortunately in Phase1 the classmark2 length is variable */ + + if (length < sizeof(*req)) { + LOGP(DNAT, LOGL_ERROR, + "CM Serv Req does not fit. Length is %d\n", length); + return -1; + } + + req = (struct gsm48_service_request *) data; + if (req->cm_service_type == 0x8) + *con_type = NAT_CON_TYPE_SSA; + rc = gsm48_extract_mi((uint8_t *) &req->classmark, + length - classmark_offset, mi_string, &mi_type); + if (rc < 0) { + LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); + return -1; + } + + /* we have to let the TMSI or such pass */ + if (mi_type != GSM_MI_TYPE_IMSI) + return 0; + + *imsi = talloc_strdup(bsc, mi_string); + return 1; +} + +static int _cr_check_pag_resp(struct bsc_connection *bsc, + uint8_t *data, unsigned int length, char **imsi) +{ + struct gsm48_pag_resp *resp; + char mi_string[GSM48_MI_SIZE]; + uint8_t mi_type; + + if (length < sizeof(*resp)) { + LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); + return -1; + } + + resp = (struct gsm48_pag_resp *) data; + if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { + LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); + return -1; + } + + /* we need to let it pass for now */ + if (mi_type != GSM_MI_TYPE_IMSI) + return 0; + + *imsi = talloc_strdup(bsc, mi_string); + return 1; +} + +static int _dt_check_id_resp(struct bsc_connection *bsc, + uint8_t *data, unsigned int length, + struct nat_sccp_connection *con, + struct bsc_nat_reject_cause *cause) +{ + char mi_string[GSM48_MI_SIZE]; + uint8_t mi_type; + + if (length < 2) { + LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); + return -1; + } + + if (data[0] < length - 1) { + LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); + return -2; + } + + mi_type = data[1] & GSM_MI_TYPE_MASK; + gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); + + if (mi_type != GSM_MI_TYPE_IMSI) + return 0; + + con->imsi_checked = 1; + con->imsi = talloc_strdup(con, mi_string); + return auth_imsi(bsc, mi_string, cause); +} + + +/* 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_nat_reject_cause *cause) +{ + struct tlv_parsed tp; + struct gsm48_hdr *hdr48; + int hdr48_len; + int len, ret = 0; + uint8_t msg_type, proto; + + *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); + + proto = hdr48->proto_discr & 0x0f; + msg_type = hdr48->msg_type & 0xbf; + if (proto == GSM48_PDISC_MM && + msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { + *con_type = NAT_CON_TYPE_LU; + ret = _cr_check_loc_upd(bsc, &hdr48->data[0], + hdr48_len - sizeof(*hdr48), imsi); + } else if (proto == GSM48_PDISC_MM && + msg_type == GSM48_MT_MM_CM_SERV_REQ) { + *con_type = NAT_CON_TYPE_CM_SERV_REQ; + ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0], + hdr48_len - sizeof(*hdr48), + con_type, imsi); + } else if (proto == GSM48_PDISC_RR && + msg_type == GSM48_MT_RR_PAG_RESP) { + *con_type = NAT_CON_TYPE_PAG_RESP; + ret = _cr_check_pag_resp(bsc, &hdr48->data[0], + hdr48_len - sizeof(*hdr48), imsi); + } else { + /* We only want to filter the above, let other things pass */ + *con_type = NAT_CON_TYPE_OTHER; + return 0; + } + + /* check if we are done */ + if (ret != 1) + return ret; + + /* the memory allocation failed */ + if (!*imsi) + return -1; + + /* now check the imsi */ + 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, + struct bsc_nat_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; + + 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) + return 0; + + return _dt_check_id_resp(bsc, &hdr48->data[0], + len - sizeof(*hdr48), con, cause); +} diff --git a/openbsc/src/osmo-bsc_nat/Makefile.am b/openbsc/src/osmo-bsc_nat/Makefile.am index ca103a4ff..7a38430f9 100644 --- a/openbsc/src/osmo-bsc_nat/Makefile.am +++ b/openbsc/src/osmo-bsc_nat/Makefile.am @@ -7,12 +7,13 @@ 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_filter.c bsc_nat_rewrite_trie.c + bsc_nat_rewrite.c bsc_nat_rewrite_trie.c osmo_bsc_nat_LDADD = \ $(top_builddir)/src/libmgcp/libmgcp.a \ $(top_builddir)/src/libbsc/libbsc.a \ $(top_builddir)/src/libtrau/libtrau.a \ $(top_builddir)/src/libcommon/libcommon.a \ + $(top_builddir)/src/libfilter/libfilter.a \ -lrt $(LIBOSMOSCCP_LIBS) $(LIBOSMOCORE_LIBS) \ $(LIBOSMOGSM_LIBS) $(LIBOSMOVTY_LIBS) $(LIBOSMOCTRL_LIBS) \ $(LIBOSMOABIS_LIBS) $(LIBOSMONETIF_LIBS) diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c deleted file mode 100644 index 64f6d7347..000000000 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c +++ /dev/null @@ -1,443 +0,0 @@ -/* - * Access filtering - */ -/* - * (C) 2010-2012 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 -#include - -#include -#include - -#include - -int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) -{ - struct bsc_nat_barr_entry *n; - n = rb_entry(root->rb_node, struct bsc_nat_barr_entry, node); - - while (n) { - int rc = strcmp(imsi, n->imsi); - if (rc == 0) { - *cm = n->cm_reject_cause; - *lu = n->lu_reject_cause; - return 1; - } - - n = rb_entry( - (rc < 0) ? n->node.rb_left : n->node.rb_right, - struct bsc_nat_barr_entry, node); - }; - - return 0; -} - -static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *root) -{ - struct rb_node **new = &root->rb_node, *parent = NULL; - - while (*new) { - int rc; - struct bsc_nat_barr_entry *this; - this = rb_entry(*new, struct bsc_nat_barr_entry, node); - parent = *new; - - rc = strcmp(entry->imsi, this->imsi); - if (rc < 0) - new = &((*new)->rb_left); - else if (rc > 0) - new = &((*new)->rb_right); - else { - LOGP(DNAT, LOGL_ERROR, - "Duplicate entry for IMSI(%s)\n", entry->imsi); - talloc_free(entry); - return -1; - } - } - - rb_link_node(&entry->node, parent, new); - rb_insert_color(&entry->node, root); - return 0; -} - -int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, - const struct osmo_config_list *list) -{ - struct osmo_config_entry *cfg_entry; - int err = 0; - - /* free the old data */ - while (!RB_EMPTY_ROOT(root)) { - struct rb_node *node = rb_first(root); - rb_erase(node, root); - talloc_free(node); - } - - if (!list) - return 0; - - /* now adapt the new list */ - llist_for_each_entry(cfg_entry, &list->entry, list) { - struct bsc_nat_barr_entry *entry; - entry = talloc_zero(ctx, struct bsc_nat_barr_entry); - if (!entry) { - LOGP(DNAT, LOGL_ERROR, - "Allocation of the barr entry failed.\n"); - continue; - } - - entry->imsi = talloc_strdup(entry, cfg_entry->mcc); - entry->cm_reject_cause = atoi(cfg_entry->mnc); - entry->lu_reject_cause = atoi(cfg_entry->option); - err |= insert_barr_node(entry, root); - } - - return err; -} - - -static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string, - int *cm_cause, int *lu_cause) -{ - struct bsc_nat_acc_lst_entry *entry; - - llist_for_each_entry(entry, &lst->fltr_list, list) { - if (!entry->imsi_deny) - continue; - if (regexec(&entry->imsi_deny_re, mi_string, 0, NULL, 0) == 0) { - *cm_cause = entry->cm_reject_cause; - *lu_cause = entry->lu_reject_cause; - return 0; - } - } - - return 1; -} - -/* apply white/black list */ -static int auth_imsi(struct bsc_connection *bsc, const char *imsi, - struct bsc_nat_reject_cause *cause) -{ - /* - * Now apply blacklist/whitelist of the BSC and the NAT. - * 1.) Check the global IMSI barr list - * 2.) Allow directly if the IMSI is allowed at the BSC - * 3.) Reject if the IMSI is not allowed at the BSC - * 4.) Reject if the IMSI not allowed at the global level. - * 5.) Allow directly if the IMSI is allowed at the global level - */ - int cm, lu; - struct bsc_nat_acc_lst *nat_lst = NULL; - struct bsc_nat_acc_lst *bsc_lst = NULL; - - /* 1. global check for barred imsis */ - if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { - cause->cm_reject_cause = cm; - cause->lu_reject_cause = lu; - LOGP(DNAT, LOGL_DEBUG, - "Blocking subscriber IMSI %s with CM: %d LU: %d\n", - imsi, cm, lu); - return -4; - } - - - bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); - nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); - - - if (bsc_lst) { - /* 2. BSC allow */ - if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0) - return 1; - - /* 3. BSC deny */ - if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { - LOGP(DNAT, LOGL_ERROR, - "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); - rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); - cause->cm_reject_cause = cm; - cause->lu_reject_cause = lu; - return -2; - } - - } - - /* 4. NAT deny */ - if (nat_lst) { - if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { - LOGP(DNAT, LOGL_ERROR, - "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); - rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); - cause->cm_reject_cause = cm; - cause->lu_reject_cause = lu; - return -3; - } - } - - return 1; -} - -static int _cr_check_loc_upd(struct bsc_connection *bsc, - uint8_t *data, unsigned int length, - char **imsi) -{ - uint8_t mi_type; - struct gsm48_loc_upd_req *lu; - char mi_string[GSM48_MI_SIZE]; - - if (length < sizeof(*lu)) { - LOGP(DNAT, LOGL_ERROR, - "LU does not fit. Length is %d \n", length); - return -1; - } - - lu = (struct gsm48_loc_upd_req *) data; - mi_type = lu->mi[0] & GSM_MI_TYPE_MASK; - - /* - * We can only deal with the IMSI. This will fail for a phone that - * will send the TMSI of a previous network to us. - */ - if (mi_type != GSM_MI_TYPE_IMSI) - return 0; - - gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); - *imsi = talloc_strdup(bsc, mi_string); - return 1; -} - -static int _cr_check_cm_serv_req(struct bsc_connection *bsc, - uint8_t *data, unsigned int length, - int *con_type, char **imsi) -{ - static const uint32_t classmark_offset = - offsetof(struct gsm48_service_request, classmark); - - char mi_string[GSM48_MI_SIZE]; - uint8_t mi_type; - int rc; - struct gsm48_service_request *req; - - /* unfortunately in Phase1 the classmark2 length is variable */ - - if (length < sizeof(*req)) { - LOGP(DNAT, LOGL_ERROR, - "CM Serv Req does not fit. Length is %d\n", length); - return -1; - } - - req = (struct gsm48_service_request *) data; - if (req->cm_service_type == 0x8) - *con_type = NAT_CON_TYPE_SSA; - rc = gsm48_extract_mi((uint8_t *) &req->classmark, - length - classmark_offset, mi_string, &mi_type); - if (rc < 0) { - LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); - return -1; - } - - /* we have to let the TMSI or such pass */ - if (mi_type != GSM_MI_TYPE_IMSI) - return 0; - - *imsi = talloc_strdup(bsc, mi_string); - return 1; -} - -static int _cr_check_pag_resp(struct bsc_connection *bsc, - uint8_t *data, unsigned int length, char **imsi) -{ - struct gsm48_pag_resp *resp; - char mi_string[GSM48_MI_SIZE]; - uint8_t mi_type; - - if (length < sizeof(*resp)) { - LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); - return -1; - } - - resp = (struct gsm48_pag_resp *) data; - if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { - LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); - return -1; - } - - /* we need to let it pass for now */ - if (mi_type != GSM_MI_TYPE_IMSI) - return 0; - - *imsi = talloc_strdup(bsc, mi_string); - return 1; -} - -static int _dt_check_id_resp(struct bsc_connection *bsc, - uint8_t *data, unsigned int length, - struct nat_sccp_connection *con, - struct bsc_nat_reject_cause *cause) -{ - char mi_string[GSM48_MI_SIZE]; - uint8_t mi_type; - - if (length < 2) { - LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); - return -1; - } - - if (data[0] < length - 1) { - LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); - return -2; - } - - mi_type = data[1] & GSM_MI_TYPE_MASK; - gsm48_mi_to_string(mi_string, sizeof(mi_string), &data[1], data[0]); - - if (mi_type != GSM_MI_TYPE_IMSI) - return 0; - - con->imsi_checked = 1; - con->imsi = talloc_strdup(con, mi_string); - return auth_imsi(bsc, mi_string, cause); -} - - -/* 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_nat_reject_cause *cause) -{ - struct tlv_parsed tp; - struct gsm48_hdr *hdr48; - int hdr48_len; - int len, ret = 0; - uint8_t msg_type, proto; - - *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); - - proto = hdr48->proto_discr & 0x0f; - msg_type = hdr48->msg_type & 0xbf; - if (proto == GSM48_PDISC_MM && - msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { - *con_type = NAT_CON_TYPE_LU; - ret = _cr_check_loc_upd(bsc, &hdr48->data[0], - hdr48_len - sizeof(*hdr48), imsi); - } else if (proto == GSM48_PDISC_MM && - msg_type == GSM48_MT_MM_CM_SERV_REQ) { - *con_type = NAT_CON_TYPE_CM_SERV_REQ; - ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0], - hdr48_len - sizeof(*hdr48), - con_type, imsi); - } else if (proto == GSM48_PDISC_RR && - msg_type == GSM48_MT_RR_PAG_RESP) { - *con_type = NAT_CON_TYPE_PAG_RESP; - ret = _cr_check_pag_resp(bsc, &hdr48->data[0], - hdr48_len - sizeof(*hdr48), imsi); - } else { - /* We only want to filter the above, let other things pass */ - *con_type = NAT_CON_TYPE_OTHER; - return 0; - } - - /* check if we are done */ - if (ret != 1) - return ret; - - /* the memory allocation failed */ - if (!*imsi) - return -1; - - /* now check the imsi */ - 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, - struct bsc_nat_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; - - 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) - return 0; - - return _dt_check_id_resp(bsc, &hdr48->data[0], - len - sizeof(*hdr48), con, cause); -} diff --git a/openbsc/tests/bsc-nat/Makefile.am b/openbsc/tests/bsc-nat/Makefile.am index b620d9649..64cd9ac85 100644 --- a/openbsc/tests/bsc-nat/Makefile.am +++ b/openbsc/tests/bsc-nat/Makefile.am @@ -10,11 +10,12 @@ bsc_nat_test_SOURCES = bsc_nat_test.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_filter.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_sccp.c \ $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_utils.c \ - $(top_srcdir)/src/osmo-bsc_nat/bsc_nat_filter.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 -bsc_nat_test_LDADD = $(top_builddir)/src/libbsc/libbsc.a \ +bsc_nat_test_LDADD = \ + $(top_builddir)/src/libfilter/libfilter.a \ + $(top_builddir)/src/libbsc/libbsc.a \ $(top_builddir)/src/libmgcp/libmgcp.a \ $(top_builddir)/src/libtrau/libtrau.a \ $(top_builddir)/src/libcommon/libcommon.a \ -- cgit v1.2.3 From 973dbaeebdbdbd8fed417cdfd169644093389d05 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 20:47:03 +0200 Subject: filter: Move the method definition to the filter module Move the filter methods to the filter module. This is still only usable for the NAT and the _dt/_cr filter routines need to move back to the bsc_nat in the long run. --- openbsc/include/openbsc/Makefile.am | 2 +- openbsc/include/openbsc/bsc_msg_filter.h | 71 ++++++++++++++++++++++++++++++++ openbsc/include/openbsc/bsc_nat.h | 56 ------------------------- openbsc/src/libfilter/bsc_msg_filter.c | 5 ++- openbsc/src/osmo-bsc_nat/bsc_nat.c | 1 + openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c | 1 + openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 1 + openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 1 + openbsc/tests/bsc-nat/bsc_nat_test.c | 1 + 9 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 openbsc/include/openbsc/bsc_msg_filter.h diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am index d2b30de79..254f43dbe 100644 --- a/openbsc/include/openbsc/Makefile.am +++ b/openbsc/include/openbsc/Makefile.am @@ -16,7 +16,7 @@ noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \ arfcn_range_encode.h nat_rewrite_trie.h bsc_nat_callstats.h \ osmux.h mgcp_transcode.h gprs_utils.h \ gprs_gb_parse.h smpp.h meas_feed.h gprs_gsup_messages.h \ - gprs_gsup_client.h + gprs_gsup_client.h bsc_msg_filter.h openbsc_HEADERS = gsm_04_08.h meas_rep.h bsc_api.h openbscdir = $(includedir)/openbsc diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h new file mode 100644 index 000000000..d9b3f6b5c --- /dev/null +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include + +#include + +/* TODO: remove */ +struct bsc_nat_parsed; +struct bsc_connection; +struct nat_sccp_connection; + +struct bsc_nat_reject_cause { + int lu_reject_cause; + int cm_reject_cause; +}; + +struct bsc_nat_barr_entry { + struct rb_node node; + + char *imsi; + int cm_reject_cause; + int lu_reject_cause; +}; + +enum bsc_nat_acc_ctr { + ACC_LIST_BSC_FILTER, + ACC_LIST_NAT_FILTER, +}; + +struct bsc_nat_acc_lst { + struct llist_head list; + + /* counter */ + struct rate_ctr_group *stats; + + /* the name of the list */ + const char *name; + struct llist_head fltr_list; +}; + +struct bsc_nat_acc_lst_entry { + struct llist_head list; + + /* the filter */ + char *imsi_allow; + regex_t imsi_allow_re; + char *imsi_deny; + regex_t imsi_deny_re; + + /* reject reasons for the access lists */ + int cm_reject_cause; + int lu_reject_cause; +}; + + +int bsc_nat_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); +int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); + +/** + * Content filtering. + */ +int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, + struct bsc_nat_parsed *, int *con_type, char **imsi, + struct bsc_nat_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_nat_reject_cause *cause); + diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h index 19144e3dd..de709c323 100644 --- a/openbsc/include/openbsc/bsc_nat.h +++ b/openbsc/include/openbsc/bsc_nat.h @@ -229,36 +229,6 @@ struct bsc_nat_statistics { } ussd; }; -enum bsc_nat_acc_ctr { - ACC_LIST_BSC_FILTER, - ACC_LIST_NAT_FILTER, -}; - -struct bsc_nat_acc_lst { - struct llist_head list; - - /* counter */ - struct rate_ctr_group *stats; - - /* the name of the list */ - const char *name; - struct llist_head fltr_list; -}; - -struct bsc_nat_acc_lst_entry { - struct llist_head list; - - /* the filter */ - char *imsi_allow; - regex_t imsi_allow_re; - char *imsi_deny; - regex_t imsi_deny_re; - - /* reject reasons for the access lists */ - int cm_reject_cause; - int lu_reject_cause; -}; - /** * the structure of the "nat" network */ @@ -355,11 +325,6 @@ struct bsc_nat_ussd_con { struct osmo_timer_list auth_timeout; }; -struct bsc_nat_reject_cause { - int lu_reject_cause; - int cm_reject_cause; -}; - /* create and init the structures */ struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token); struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num); @@ -389,16 +354,6 @@ int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *p int bsc_nat_vty_init(struct bsc_nat *nat); int bsc_nat_find_paging(struct msgb *msg, const uint8_t **,int *len); -/** - * Content filtering. - */ -int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, - struct bsc_nat_parsed *, int *con_type, char **imsi, - struct bsc_nat_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_nat_reject_cause *cause); - /** * SCCP patching and handling */ @@ -478,17 +433,6 @@ struct bsc_nat_num_rewr_entry { void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head, const struct osmo_config_list *); -struct bsc_nat_barr_entry { - struct rb_node node; - - char *imsi; - int cm_reject_cause; - int lu_reject_cause; -}; - -int bsc_nat_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); -int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); - void bsc_nat_send_mgcp_to_msc(struct bsc_nat *bsc_nat, struct msgb *msg); void bsc_nat_handle_mgcp(struct bsc_nat *bsc, struct msgb *msg); diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 64f6d7347..8f2e1cabf 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -2,7 +2,7 @@ * Access filtering */ /* - * (C) 2010-2012 by Holger Hans Peter Freyther + * (C) 2010-2015 by Holger Hans Peter Freyther * (C) 2010-2012 by On-Waves * All Rights Reserved * @@ -21,6 +21,8 @@ * */ +#include + #include #include #include @@ -28,7 +30,6 @@ #include #include -#include #include #include diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c index 5c3b696e2..ebd291b7f 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c index 4b59b404f..439bf3344 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c @@ -29,6 +29,7 @@ #include #include +#include #include #include diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index 70ad577b9..e658569a7 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 9e66cdc28..bf8ba5ce3 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 5b01cc3cd..105fec92b 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3 From 4579bb1ed7464d66343d84846314ec66e6f8cccd Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 21:55:08 +0200 Subject: filter: Move the access list management around --- openbsc/include/openbsc/bsc_msg_filter.h | 9 +++ openbsc/include/openbsc/bsc_nat.h | 8 --- openbsc/src/libfilter/Makefile.am | 3 +- openbsc/src/libfilter/bsc_msg_acc.c | 116 +++++++++++++++++++++++++++++++ openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 88 ----------------------- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 1 + 6 files changed, 128 insertions(+), 97 deletions(-) create mode 100644 openbsc/src/libfilter/bsc_msg_acc.c diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index d9b3f6b5c..248a0828b 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -8,6 +8,7 @@ #include /* TODO: remove */ +struct bsc_nat; struct bsc_nat_parsed; struct bsc_connection; struct nat_sccp_connection; @@ -69,3 +70,11 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, struct bsc_nat_reject_cause *cause); +/* IMSI allow/deny handling */ +struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name); +struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name); +void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); + +struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); +int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); + diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h index de709c323..b9f1e56f8 100644 --- a/openbsc/include/openbsc/bsc_nat.h +++ b/openbsc/include/openbsc/bsc_nat.h @@ -390,14 +390,6 @@ int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int id); int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg); int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg); -/* IMSI allow/deny handling */ -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name); -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name); -void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); - -struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); -int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); - int bsc_nat_msc_is_connected(struct bsc_nat *nat); int bsc_conn_type_to_ctr(struct nat_sccp_connection *conn); diff --git a/openbsc/src/libfilter/Makefile.am b/openbsc/src/libfilter/Makefile.am index e24ec5f73..d79afb2cb 100644 --- a/openbsc/src/libfilter/Makefile.am +++ b/openbsc/src/libfilter/Makefile.am @@ -5,5 +5,6 @@ AM_CFLAGS=-Wall $(LIBOSMOCORE_CFLAGS) $(LIBOSMOGSM_CFLAGS) \ noinst_LIBRARIES = libfilter.a libfilter_a_SOURCES = \ - bsc_msg_filter.c + bsc_msg_filter.c \ + bsc_msg_acc.c diff --git a/openbsc/src/libfilter/bsc_msg_acc.c b/openbsc/src/libfilter/bsc_msg_acc.c new file mode 100644 index 000000000..947a7b2ed --- /dev/null +++ b/openbsc/src/libfilter/bsc_msg_acc.c @@ -0,0 +1,116 @@ +/* + * (C) 2010-2015 by Holger Hans Peter Freyther + * (C) 2010-2011 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 + +static const struct rate_ctr_desc acc_list_ctr_description[] = { + [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"}, + [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"}, +}; + +static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { + .group_name_prefix = "nat.filter", + .group_description = "NAT Access-List Statistics", + .num_ctr = ARRAY_SIZE(acc_list_ctr_description), + .ctr_desc = acc_list_ctr_description, +}; + + +int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) +{ + struct bsc_nat_acc_lst_entry *entry; + + llist_for_each_entry(entry, &lst->fltr_list, list) { + if (!entry->imsi_allow) + continue; + if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) + return 0; + } + + return 1; +} + +struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) +{ + struct bsc_nat_acc_lst *lst; + + if (!name) + return NULL; + + llist_for_each_entry(lst, &nat->access_lists, list) + if (strcmp(lst->name, name) == 0) + return lst; + + return NULL; +} + +struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) +{ + struct bsc_nat_acc_lst *lst; + + lst = bsc_nat_acc_lst_find(nat, name); + if (lst) + return lst; + + lst = talloc_zero(nat, struct bsc_nat_acc_lst); + if (!lst) { + LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); + return NULL; + } + + /* TODO: get the index right */ + lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); + if (!lst->stats) { + talloc_free(lst); + return NULL; + } + + INIT_LLIST_HEAD(&lst->fltr_list); + lst->name = talloc_strdup(lst, name); + llist_add_tail(&lst->list, &nat->access_lists); + return lst; +} + +void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) +{ + llist_del(&lst->list); + rate_ctr_group_free(lst->stats); + talloc_free(lst); +} + +struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) +{ + struct bsc_nat_acc_lst_entry *entry; + + entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); + if (!entry) + return NULL; + + entry->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + entry->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; + llist_add_tail(&entry->list, &lst->fltr_list); + return entry; +} + diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index e658569a7..33582ffe1 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -67,18 +67,6 @@ static const struct rate_ctr_group_desc bsc_cfg_ctrg_desc = { .ctr_desc = bsc_cfg_ctr_description, }; -static const struct rate_ctr_desc acc_list_ctr_description[] = { - [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"}, - [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"}, -}; - -static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { - .group_name_prefix = "nat.filter", - .group_description = "NAT Access-List Statistics", - .num_ctr = ARRAY_SIZE(acc_list_ctr_description), - .ctr_desc = acc_list_ctr_description, -}; - struct bsc_nat *bsc_nat_alloc(void) { struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat); @@ -393,20 +381,6 @@ int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg) return 0; } -int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) -{ - struct bsc_nat_acc_lst_entry *entry; - - llist_for_each_entry(entry, &lst->fltr_list, list) { - if (!entry->imsi_allow) - continue; - if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) - return 0; - } - - return 1; -} - struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len) { @@ -443,68 +417,6 @@ const char *bsc_con_type_to_string(int type) return con_types[type]; } -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) -{ - struct bsc_nat_acc_lst *lst; - - if (!name) - return NULL; - - llist_for_each_entry(lst, &nat->access_lists, list) - if (strcmp(lst->name, name) == 0) - return lst; - - return NULL; -} - -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) -{ - struct bsc_nat_acc_lst *lst; - - lst = bsc_nat_acc_lst_find(nat, name); - if (lst) - return lst; - - lst = talloc_zero(nat, struct bsc_nat_acc_lst); - if (!lst) { - LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); - return NULL; - } - - /* TODO: get the index right */ - lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); - if (!lst->stats) { - talloc_free(lst); - return NULL; - } - - INIT_LLIST_HEAD(&lst->fltr_list); - lst->name = talloc_strdup(lst, name); - llist_add_tail(&lst->list, &nat->access_lists); - return lst; -} - -void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) -{ - llist_del(&lst->list); - rate_ctr_group_free(lst->stats); - talloc_free(lst); -} - -struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) -{ - struct bsc_nat_acc_lst_entry *entry; - - entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); - if (!entry) - return NULL; - - entry->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; - entry->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; - llist_add_tail(&entry->list, &lst->fltr_list); - return entry; -} - int bsc_nat_msc_is_connected(struct bsc_nat *nat) { return nat->msc_con->is_connected; diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index 67844b812..48626b1ad 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -22,6 +22,7 @@ #include #include +#include #include #include -- cgit v1.2.3 From d04d009f473d89a426c16dd24e5a4c692caf0017 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 22:14:34 +0200 Subject: filter: Move VTY code into the filter module --- openbsc/include/openbsc/bsc_msg_filter.h | 4 + openbsc/src/libfilter/Makefile.am | 3 +- openbsc/src/libfilter/bsc_msg_vty.c | 141 +++++++++++++++++++++++++++++++ openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 112 +----------------------- 4 files changed, 149 insertions(+), 111 deletions(-) create mode 100644 openbsc/src/libfilter/bsc_msg_vty.c diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 248a0828b..bff326fc7 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -7,6 +7,8 @@ #include +struct vty; + /* TODO: remove */ struct bsc_nat; struct bsc_nat_parsed; @@ -78,3 +80,5 @@ void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); +void bsc_nat_lst_vty_init(struct bsc_nat *nat, int node); +void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst); diff --git a/openbsc/src/libfilter/Makefile.am b/openbsc/src/libfilter/Makefile.am index d79afb2cb..4dbc59041 100644 --- a/openbsc/src/libfilter/Makefile.am +++ b/openbsc/src/libfilter/Makefile.am @@ -6,5 +6,6 @@ noinst_LIBRARIES = libfilter.a libfilter_a_SOURCES = \ bsc_msg_filter.c \ - bsc_msg_acc.c + bsc_msg_acc.c \ + bsc_msg_vty.c diff --git a/openbsc/src/libfilter/bsc_msg_vty.c b/openbsc/src/libfilter/bsc_msg_vty.c new file mode 100644 index 000000000..070a03da6 --- /dev/null +++ b/openbsc/src/libfilter/bsc_msg_vty.c @@ -0,0 +1,141 @@ +/* (C) 2010-2015 by Holger Hans Peter Freyther + * (C) 2010-2013 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 + +static struct bsc_nat *_nat; + +DEFUN(cfg_lst_no, + cfg_lst_no_cmd, + "no access-list NAME", + NO_STR "Remove an access-list by name\n" + "The access-list to remove\n") +{ + struct bsc_nat_acc_lst *acc; + acc = bsc_nat_acc_lst_find(_nat, argv[0]); + if (!acc) + return CMD_WARNING; + + bsc_nat_acc_lst_delete(acc); + return CMD_SUCCESS; +} + +DEFUN(show_acc_lst, + show_acc_lst_cmd, + "show access-list NAME", + SHOW_STR "IMSI access list\n" "Name of the access list\n") +{ + struct bsc_nat_acc_lst *acc; + acc = bsc_nat_acc_lst_find(_nat, argv[0]); + if (!acc) + return CMD_WARNING; + + vty_out(vty, "access-list %s%s", acc->name, VTY_NEWLINE); + vty_out_rate_ctr_group(vty, " ", acc->stats); + + return CMD_SUCCESS; +} + +DEFUN(cfg_lst_imsi_allow, + cfg_lst_imsi_allow_cmd, + "access-list NAME imsi-allow [REGEXP]", + "Access list commands\n" + "Name of the access list\n" + "Add allowed IMSI to the list\n" + "Regexp for IMSIs\n") +{ + struct bsc_nat_acc_lst *acc; + struct bsc_nat_acc_lst_entry *entry; + + acc = bsc_nat_acc_lst_get(_nat, argv[0]); + if (!acc) + return CMD_WARNING; + + entry = bsc_nat_acc_lst_entry_create(acc); + if (!entry) + return CMD_WARNING; + + if (gsm_parse_reg(acc, &entry->imsi_allow_re, &entry->imsi_allow, argc - 1, &argv[1]) != 0) + return CMD_WARNING; + return CMD_SUCCESS; +} + +DEFUN(cfg_lst_imsi_deny, + cfg_lst_imsi_deny_cmd, + "access-list NAME imsi-deny [REGEXP] (<0-256>) (<0-256>)", + "Access list commands\n" + "Name of the access list\n" + "Add denied IMSI to the list\n" + "Regexp for IMSIs\n" + "CM Service Reject reason\n" + "LU Reject reason\n") +{ + struct bsc_nat_acc_lst *acc; + struct bsc_nat_acc_lst_entry *entry; + + acc = bsc_nat_acc_lst_get(_nat, argv[0]); + if (!acc) + return CMD_WARNING; + + entry = bsc_nat_acc_lst_entry_create(acc); + if (!entry) + return CMD_WARNING; + + if (gsm_parse_reg(acc, &entry->imsi_deny_re, &entry->imsi_deny, argc - 1, &argv[1]) != 0) + return CMD_WARNING; + if (argc >= 3) + entry->cm_reject_cause = atoi(argv[2]); + if (argc >= 4) + entry->lu_reject_cause = atoi(argv[3]); + return CMD_SUCCESS; +} + +void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst) +{ + struct bsc_nat_acc_lst_entry *entry; + + llist_for_each_entry(entry, &lst->fltr_list, list) { + if (entry->imsi_allow) + vty_out(vty, " access-list %s imsi-allow %s%s", + lst->name, entry->imsi_allow, VTY_NEWLINE); + if (entry->imsi_deny) + vty_out(vty, " access-list %s imsi-deny %s %d %d%s", + lst->name, entry->imsi_deny, + entry->cm_reject_cause, entry->lu_reject_cause, + VTY_NEWLINE); + } +} + + +void bsc_nat_lst_vty_init(struct bsc_nat *nat, int node) +{ + _nat = nat; + + install_element_ve(&show_acc_lst_cmd); + + /* access-list */ + install_element(node, &cfg_lst_imsi_allow_cmd); + install_element(node, &cfg_lst_imsi_deny_cmd); + install_element(node, &cfg_lst_no_cmd); +} diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index bf8ba5ce3..89b9742a9 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -71,22 +71,6 @@ static int config_write_pgroup(struct vty *vty) return CMD_SUCCESS; } -static void write_acc_lst(struct vty *vty, struct bsc_nat_acc_lst *lst) -{ - struct bsc_nat_acc_lst_entry *entry; - - llist_for_each_entry(entry, &lst->fltr_list, list) { - if (entry->imsi_allow) - vty_out(vty, " access-list %s imsi-allow %s%s", - lst->name, entry->imsi_allow, VTY_NEWLINE); - if (entry->imsi_deny) - vty_out(vty, " access-list %s imsi-deny %s %d %d%s", - lst->name, entry->imsi_deny, - entry->cm_reject_cause, entry->lu_reject_cause, - VTY_NEWLINE); - } -} - static void dump_lac(struct vty *vty, struct llist_head *head) { struct bsc_lac_entry *lac; @@ -152,7 +136,7 @@ static int config_write_nat(struct vty *vty) _nat->num_rewr_trie_name, VTY_NEWLINE); llist_for_each_entry(lst, &_nat->access_lists, list) - write_acc_lst(vty, lst); + bsc_nat_acc_lst_write(vty, lst); llist_for_each_entry(pgroup, &_nat->paging_groups, entry) write_pgroup_lst(vty, pgroup); if (_nat->mgcp_ipa) @@ -868,94 +852,6 @@ DEFUN(cfg_bsc_no_lac, cfg_bsc_no_lac_cmd, return CMD_SUCCESS; } - - -DEFUN(cfg_lst_imsi_allow, - cfg_lst_imsi_allow_cmd, - "access-list NAME imsi-allow [REGEXP]", - "Access list commands\n" - "Name of the access list\n" - "Add allowed IMSI to the list\n" - "Regexp for IMSIs\n") -{ - struct bsc_nat_acc_lst *acc; - struct bsc_nat_acc_lst_entry *entry; - - acc = bsc_nat_acc_lst_get(_nat, argv[0]); - if (!acc) - return CMD_WARNING; - - entry = bsc_nat_acc_lst_entry_create(acc); - if (!entry) - return CMD_WARNING; - - if (gsm_parse_reg(acc, &entry->imsi_allow_re, &entry->imsi_allow, argc - 1, &argv[1]) != 0) - return CMD_WARNING; - return CMD_SUCCESS; -} - -DEFUN(cfg_lst_imsi_deny, - cfg_lst_imsi_deny_cmd, - "access-list NAME imsi-deny [REGEXP] (<0-256>) (<0-256>)", - "Access list commands\n" - "Name of the access list\n" - "Add denied IMSI to the list\n" - "Regexp for IMSIs\n" - "CM Service Reject reason\n" - "LU Reject reason\n") -{ - struct bsc_nat_acc_lst *acc; - struct bsc_nat_acc_lst_entry *entry; - - acc = bsc_nat_acc_lst_get(_nat, argv[0]); - if (!acc) - return CMD_WARNING; - - entry = bsc_nat_acc_lst_entry_create(acc); - if (!entry) - return CMD_WARNING; - - if (gsm_parse_reg(acc, &entry->imsi_deny_re, &entry->imsi_deny, argc - 1, &argv[1]) != 0) - return CMD_WARNING; - if (argc >= 3) - entry->cm_reject_cause = atoi(argv[2]); - if (argc >= 4) - entry->lu_reject_cause = atoi(argv[3]); - return CMD_SUCCESS; -} - -/* naming to follow Zebra... */ -DEFUN(cfg_lst_no, - cfg_lst_no_cmd, - "no access-list NAME", - NO_STR "Remove an access-list by name\n" - "The access-list to remove\n") -{ - struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_nat, argv[0]); - if (!acc) - return CMD_WARNING; - - bsc_nat_acc_lst_delete(acc); - return CMD_SUCCESS; -} - -DEFUN(show_acc_lst, - show_acc_lst_cmd, - "show access-list NAME", - SHOW_STR "IMSI access list\n" "Name of the access list\n") -{ - struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_nat, argv[0]); - if (!acc) - return CMD_WARNING; - - vty_out(vty, "access-list %s%s", acc->name, VTY_NEWLINE); - vty_out_rate_ctr_group(vty, " ", acc->stats); - - return CMD_SUCCESS; -} - DEFUN(show_bar_lst, show_bar_lst_cmd, "show imsi-black-list", @@ -1246,7 +1142,6 @@ int bsc_nat_vty_init(struct bsc_nat *nat) install_element_ve(&show_msc_cmd); install_element_ve(&test_regex_cmd); install_element_ve(&show_bsc_mgcp_cmd); - install_element_ve(&show_acc_lst_cmd); install_element_ve(&show_bar_lst_cmd); install_element_ve(&show_prefix_tree_cmd); install_element_ve(&show_ussd_connection_cmd); @@ -1276,10 +1171,7 @@ int bsc_nat_vty_init(struct bsc_nat *nat) install_element(NAT_NODE, &cfg_nat_ussd_local_cmd); install_element(NAT_NODE, &cfg_nat_use_ipa_for_mgcp_cmd); - /* access-list */ - install_element(NAT_NODE, &cfg_lst_imsi_allow_cmd); - install_element(NAT_NODE, &cfg_lst_imsi_deny_cmd); - install_element(NAT_NODE, &cfg_lst_no_cmd); + bsc_nat_lst_vty_init(nat, NAT_NODE); /* number rewriting */ install_element(NAT_NODE, &cfg_nat_number_rewrite_cmd); -- cgit v1.2.3 From d7e04b9956bb7d579697604fff6ba67fc6b9e52d Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 22:28:32 +0200 Subject: filter: Cease out "struct bsc_nat" from the API This means we need to require a talloc context and simply operate on the list. I had considered creating a structure to hold the list head but I didn't find any other members so omitted it for now. --- openbsc/include/openbsc/bsc_msg_filter.h | 7 +++---- openbsc/src/libfilter/bsc_msg_acc.c | 12 ++++++------ openbsc/src/libfilter/bsc_msg_filter.c | 4 ++-- openbsc/src/libfilter/bsc_msg_vty.c | 19 +++++++++---------- openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 2 +- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 2 +- openbsc/tests/bsc-nat/bsc_nat_test.c | 4 ++-- 7 files changed, 24 insertions(+), 26 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index bff326fc7..80dc37abf 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -10,7 +10,6 @@ struct vty; /* TODO: remove */ -struct bsc_nat; struct bsc_nat_parsed; struct bsc_connection; struct nat_sccp_connection; @@ -73,12 +72,12 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_reject_cause *cause); /* IMSI allow/deny handling */ -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name); -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name); +struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct llist_head *lst, const char *name); +struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(void *ctx, struct llist_head *lst, const char *name); void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); -void bsc_nat_lst_vty_init(struct bsc_nat *nat, int node); +void bsc_nat_lst_vty_init(void *ctx, struct llist_head *lst, int node); void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst); diff --git a/openbsc/src/libfilter/bsc_msg_acc.c b/openbsc/src/libfilter/bsc_msg_acc.c index 947a7b2ed..cc6c44405 100644 --- a/openbsc/src/libfilter/bsc_msg_acc.c +++ b/openbsc/src/libfilter/bsc_msg_acc.c @@ -52,29 +52,29 @@ int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) return 1; } -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name) +struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct llist_head *head, const char *name) { struct bsc_nat_acc_lst *lst; if (!name) return NULL; - llist_for_each_entry(lst, &nat->access_lists, list) + llist_for_each_entry(lst, head, list) if (strcmp(lst->name, name) == 0) return lst; return NULL; } -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name) +struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(void *ctx, struct llist_head *head, const char *name) { struct bsc_nat_acc_lst *lst; - lst = bsc_nat_acc_lst_find(nat, name); + lst = bsc_nat_acc_lst_find(head, name); if (lst) return lst; - lst = talloc_zero(nat, struct bsc_nat_acc_lst); + lst = talloc_zero(ctx, struct bsc_nat_acc_lst); if (!lst) { LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); return NULL; @@ -89,7 +89,7 @@ struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *nam INIT_LLIST_HEAD(&lst->fltr_list); lst->name = talloc_strdup(lst, name); - llist_add_tail(&lst->list, &nat->access_lists); + llist_add_tail(&lst->list, head); return lst; } diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 8f2e1cabf..1f7a14e63 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -168,8 +168,8 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, } - bsc_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->cfg->acc_lst_name); - nat_lst = bsc_nat_acc_lst_find(bsc->nat, bsc->nat->acc_lst_name); + bsc_lst = bsc_nat_acc_lst_find(&bsc->nat->access_lists, bsc->cfg->acc_lst_name); + nat_lst = bsc_nat_acc_lst_find(&bsc->nat->access_lists, bsc->nat->acc_lst_name); if (bsc_lst) { diff --git a/openbsc/src/libfilter/bsc_msg_vty.c b/openbsc/src/libfilter/bsc_msg_vty.c index 070a03da6..79cf03db1 100644 --- a/openbsc/src/libfilter/bsc_msg_vty.c +++ b/openbsc/src/libfilter/bsc_msg_vty.c @@ -18,13 +18,13 @@ */ #include -#include #include #include #include -static struct bsc_nat *_nat; +static struct llist_head *_acc_lst; +static void *_ctx; DEFUN(cfg_lst_no, cfg_lst_no_cmd, @@ -33,7 +33,7 @@ DEFUN(cfg_lst_no, "The access-list to remove\n") { struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_nat, argv[0]); + acc = bsc_nat_acc_lst_find(_acc_lst, argv[0]); if (!acc) return CMD_WARNING; @@ -47,7 +47,7 @@ DEFUN(show_acc_lst, SHOW_STR "IMSI access list\n" "Name of the access list\n") { struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_nat, argv[0]); + acc = bsc_nat_acc_lst_find(_acc_lst, argv[0]); if (!acc) return CMD_WARNING; @@ -68,7 +68,7 @@ DEFUN(cfg_lst_imsi_allow, struct bsc_nat_acc_lst *acc; struct bsc_nat_acc_lst_entry *entry; - acc = bsc_nat_acc_lst_get(_nat, argv[0]); + acc = bsc_nat_acc_lst_get(_ctx, _acc_lst, argv[0]); if (!acc) return CMD_WARNING; @@ -94,7 +94,7 @@ DEFUN(cfg_lst_imsi_deny, struct bsc_nat_acc_lst *acc; struct bsc_nat_acc_lst_entry *entry; - acc = bsc_nat_acc_lst_get(_nat, argv[0]); + acc = bsc_nat_acc_lst_get(_ctx, _acc_lst, argv[0]); if (!acc) return CMD_WARNING; @@ -127,11 +127,10 @@ void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst) } } - -void bsc_nat_lst_vty_init(struct bsc_nat *nat, int node) +void bsc_nat_lst_vty_init(void *ctx, struct llist_head *lst, int node) { - _nat = nat; - + _ctx = ctx; + _acc_lst = lst; install_element_ve(&show_acc_lst_cmd); /* access-list */ diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 89b9742a9..3a53dd402 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -1171,7 +1171,7 @@ int bsc_nat_vty_init(struct bsc_nat *nat) install_element(NAT_NODE, &cfg_nat_ussd_local_cmd); install_element(NAT_NODE, &cfg_nat_use_ipa_for_mgcp_cmd); - bsc_nat_lst_vty_init(nat, NAT_NODE); + bsc_nat_lst_vty_init(nat, &nat->access_lists, NAT_NODE); /* number rewriting */ install_element(NAT_NODE, &cfg_nat_number_rewrite_cmd); diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index 48626b1ad..f27453bc3 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -416,7 +416,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse if (msg_type == GSM0480_MTYPE_REGISTER) { /* now check if it is a IMSI we care about */ - lst = bsc_nat_acc_lst_find(con->bsc->nat, + lst = bsc_nat_acc_lst_find(&con->bsc->nat->access_lists, con->bsc->nat->ussd_lst_name); if (!lst) return 0; diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 105fec92b..8ee8e3945 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -879,8 +879,8 @@ static void test_cr_filter() bsc->cfg->acc_lst_name = "bsc"; nat->acc_lst_name = "nat"; - nat_lst = bsc_nat_acc_lst_get(nat, "nat"); - bsc_lst = bsc_nat_acc_lst_get(nat, "bsc"); + nat_lst = bsc_nat_acc_lst_get(nat, &nat->access_lists, "nat"); + bsc_lst = bsc_nat_acc_lst_get(nat, &nat->access_lists, "bsc"); bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst); nat_entry = bsc_nat_acc_lst_entry_create(nat_lst); -- cgit v1.2.3 From a1e6bd6768e61828823da3ba774e55b6f89559fc Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sat, 4 Apr 2015 22:40:12 +0200 Subject: filter: Remove nat from bsc_nat_acc_lst and replace with msg --- openbsc/include/openbsc/bsc_msg_filter.h | 18 +++++++++--------- openbsc/src/libfilter/bsc_msg_acc.c | 24 ++++++++++++------------ openbsc/src/libfilter/bsc_msg_filter.c | 14 +++++++------- openbsc/src/libfilter/bsc_msg_vty.c | 32 ++++++++++++++++---------------- openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c | 10 +++++----- openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 4 ++-- openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 6 +++--- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 6 +++--- openbsc/tests/bsc-nat/bsc_nat_test.c | 12 ++++++------ 9 files changed, 63 insertions(+), 63 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 80dc37abf..8420e48af 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -32,7 +32,7 @@ enum bsc_nat_acc_ctr { ACC_LIST_NAT_FILTER, }; -struct bsc_nat_acc_lst { +struct bsc_msg_acc_lst { struct llist_head list; /* counter */ @@ -43,7 +43,7 @@ struct bsc_nat_acc_lst { struct llist_head fltr_list; }; -struct bsc_nat_acc_lst_entry { +struct bsc_msg_acc_lst_entry { struct llist_head list; /* the filter */ @@ -72,12 +72,12 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_reject_cause *cause); /* IMSI allow/deny handling */ -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct llist_head *lst, const char *name); -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(void *ctx, struct llist_head *lst, const char *name); -void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); +struct bsc_msg_acc_lst *bsc_msg_acc_lst_find(struct llist_head *lst, const char *name); +struct bsc_msg_acc_lst *bsc_msg_acc_lst_get(void *ctx, struct llist_head *lst, const char *name); +void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst); -struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); -int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); +struct bsc_msg_acc_lst_entry *bsc_msg_acc_lst_entry_create(struct bsc_msg_acc_lst *); +int bsc_msg_acc_lst_check_allow(struct bsc_msg_acc_lst *lst, const char *imsi); -void bsc_nat_lst_vty_init(void *ctx, struct llist_head *lst, int node); -void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst); +void bsc_msg_lst_vty_init(void *ctx, struct llist_head *lst, int node); +void bsc_msg_acc_lst_write(struct vty *vty, struct bsc_msg_acc_lst *lst); diff --git a/openbsc/src/libfilter/bsc_msg_acc.c b/openbsc/src/libfilter/bsc_msg_acc.c index cc6c44405..d2f45b36f 100644 --- a/openbsc/src/libfilter/bsc_msg_acc.c +++ b/openbsc/src/libfilter/bsc_msg_acc.c @@ -38,9 +38,9 @@ static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { }; -int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) +int bsc_msg_acc_lst_check_allow(struct bsc_msg_acc_lst *lst, const char *mi_string) { - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst_entry *entry; llist_for_each_entry(entry, &lst->fltr_list, list) { if (!entry->imsi_allow) @@ -52,9 +52,9 @@ int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *mi_string) return 1; } -struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct llist_head *head, const char *name) +struct bsc_msg_acc_lst *bsc_msg_acc_lst_find(struct llist_head *head, const char *name) { - struct bsc_nat_acc_lst *lst; + struct bsc_msg_acc_lst *lst; if (!name) return NULL; @@ -66,15 +66,15 @@ struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct llist_head *head, const char return NULL; } -struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(void *ctx, struct llist_head *head, const char *name) +struct bsc_msg_acc_lst *bsc_msg_acc_lst_get(void *ctx, struct llist_head *head, const char *name) { - struct bsc_nat_acc_lst *lst; + struct bsc_msg_acc_lst *lst; - lst = bsc_nat_acc_lst_find(head, name); + lst = bsc_msg_acc_lst_find(head, name); if (lst) return lst; - lst = talloc_zero(ctx, struct bsc_nat_acc_lst); + lst = talloc_zero(ctx, struct bsc_msg_acc_lst); if (!lst) { LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); return NULL; @@ -93,18 +93,18 @@ struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(void *ctx, struct llist_head *head, return lst; } -void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst) +void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst) { llist_del(&lst->list); rate_ctr_group_free(lst->stats); talloc_free(lst); } -struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *lst) +struct bsc_msg_acc_lst_entry *bsc_msg_acc_lst_entry_create(struct bsc_msg_acc_lst *lst) { - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst_entry *entry; - entry = talloc_zero(lst, struct bsc_nat_acc_lst_entry); + entry = talloc_zero(lst, struct bsc_msg_acc_lst_entry); if (!entry) return NULL; diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 1f7a14e63..19367a013 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -123,10 +123,10 @@ int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, } -static int lst_check_deny(struct bsc_nat_acc_lst *lst, const char *mi_string, +static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, int *cm_cause, int *lu_cause) { - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst_entry *entry; llist_for_each_entry(entry, &lst->fltr_list, list) { if (!entry->imsi_deny) @@ -154,8 +154,8 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, * 5.) Allow directly if the IMSI is allowed at the global level */ int cm, lu; - struct bsc_nat_acc_lst *nat_lst = NULL; - struct bsc_nat_acc_lst *bsc_lst = NULL; + struct bsc_msg_acc_lst *nat_lst = NULL; + struct bsc_msg_acc_lst *bsc_lst = NULL; /* 1. global check for barred imsis */ if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { @@ -168,13 +168,13 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, } - bsc_lst = bsc_nat_acc_lst_find(&bsc->nat->access_lists, bsc->cfg->acc_lst_name); - nat_lst = bsc_nat_acc_lst_find(&bsc->nat->access_lists, bsc->nat->acc_lst_name); + bsc_lst = bsc_msg_acc_lst_find(&bsc->nat->access_lists, bsc->cfg->acc_lst_name); + nat_lst = bsc_msg_acc_lst_find(&bsc->nat->access_lists, bsc->nat->acc_lst_name); if (bsc_lst) { /* 2. BSC allow */ - if (bsc_nat_lst_check_allow(bsc_lst, imsi) == 0) + if (bsc_msg_acc_lst_check_allow(bsc_lst, imsi) == 0) return 1; /* 3. BSC deny */ diff --git a/openbsc/src/libfilter/bsc_msg_vty.c b/openbsc/src/libfilter/bsc_msg_vty.c index 79cf03db1..c342fdca0 100644 --- a/openbsc/src/libfilter/bsc_msg_vty.c +++ b/openbsc/src/libfilter/bsc_msg_vty.c @@ -32,12 +32,12 @@ DEFUN(cfg_lst_no, NO_STR "Remove an access-list by name\n" "The access-list to remove\n") { - struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_acc_lst, argv[0]); + struct bsc_msg_acc_lst *acc; + acc = bsc_msg_acc_lst_find(_acc_lst, argv[0]); if (!acc) return CMD_WARNING; - bsc_nat_acc_lst_delete(acc); + bsc_msg_acc_lst_delete(acc); return CMD_SUCCESS; } @@ -46,8 +46,8 @@ DEFUN(show_acc_lst, "show access-list NAME", SHOW_STR "IMSI access list\n" "Name of the access list\n") { - struct bsc_nat_acc_lst *acc; - acc = bsc_nat_acc_lst_find(_acc_lst, argv[0]); + struct bsc_msg_acc_lst *acc; + acc = bsc_msg_acc_lst_find(_acc_lst, argv[0]); if (!acc) return CMD_WARNING; @@ -65,14 +65,14 @@ DEFUN(cfg_lst_imsi_allow, "Add allowed IMSI to the list\n" "Regexp for IMSIs\n") { - struct bsc_nat_acc_lst *acc; - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst *acc; + struct bsc_msg_acc_lst_entry *entry; - acc = bsc_nat_acc_lst_get(_ctx, _acc_lst, argv[0]); + acc = bsc_msg_acc_lst_get(_ctx, _acc_lst, argv[0]); if (!acc) return CMD_WARNING; - entry = bsc_nat_acc_lst_entry_create(acc); + entry = bsc_msg_acc_lst_entry_create(acc); if (!entry) return CMD_WARNING; @@ -91,14 +91,14 @@ DEFUN(cfg_lst_imsi_deny, "CM Service Reject reason\n" "LU Reject reason\n") { - struct bsc_nat_acc_lst *acc; - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst *acc; + struct bsc_msg_acc_lst_entry *entry; - acc = bsc_nat_acc_lst_get(_ctx, _acc_lst, argv[0]); + acc = bsc_msg_acc_lst_get(_ctx, _acc_lst, argv[0]); if (!acc) return CMD_WARNING; - entry = bsc_nat_acc_lst_entry_create(acc); + entry = bsc_msg_acc_lst_entry_create(acc); if (!entry) return CMD_WARNING; @@ -111,9 +111,9 @@ DEFUN(cfg_lst_imsi_deny, return CMD_SUCCESS; } -void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst) +void bsc_msg_acc_lst_write(struct vty *vty, struct bsc_msg_acc_lst *lst) { - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst_entry *entry; llist_for_each_entry(entry, &lst->fltr_list, list) { if (entry->imsi_allow) @@ -127,7 +127,7 @@ void bsc_nat_acc_lst_write(struct vty *vty, struct bsc_nat_acc_lst *lst) } } -void bsc_nat_lst_vty_init(void *ctx, struct llist_head *lst, int node) +void bsc_msg_lst_vty_init(void *ctx, struct llist_head *lst, int node) { _ctx = ctx; _acc_lst = lst; diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c index 439bf3344..f3ca92400 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_ctrl.c @@ -405,19 +405,19 @@ static int get_net_cfg_acc_cmd(struct ctrl_cmd *cmd, void *data) static int set_net_cfg_acc_cmd(struct ctrl_cmd *cmd, void *data) { const char *access_name = extract_acc_name(cmd->variable); - struct bsc_nat_acc_lst *acc; - struct bsc_nat_acc_lst_entry *entry; + struct bsc_msg_acc_lst *acc; + struct bsc_msg_acc_lst_entry *entry; const char *value = cmd->value; int rc; /* Should have been caught by verify_net_cfg_acc_cmd */ - acc = bsc_nat_acc_lst_find(g_nat, access_name); + acc = bsc_msg_acc_lst_find(&g_nat->access_lists, access_name); if (!acc) { cmd->reply = "Access list not found"; return CTRL_CMD_ERROR; } - entry = bsc_nat_acc_lst_entry_create(acc); + entry = bsc_msg_acc_lst_entry_create(acc); if (!entry) { cmd->reply = "OOM"; return CTRL_CMD_ERROR; @@ -436,7 +436,7 @@ static int set_net_cfg_acc_cmd(struct ctrl_cmd *cmd, void *data) static int verify_net_cfg_acc_cmd(struct ctrl_cmd *cmd, const char *value, void *data) { const char *access_name = extract_acc_name(cmd->variable); - struct bsc_nat_acc_lst *acc = bsc_nat_acc_lst_find(g_nat, access_name); + struct bsc_msg_acc_lst *acc = bsc_msg_acc_lst_find(&g_nat->access_lists, access_name); if (!acc) { cmd->reply = "Access list not known"; diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index 33582ffe1..89212087c 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -112,12 +112,12 @@ struct bsc_nat *bsc_nat_alloc(void) void bsc_nat_free(struct bsc_nat *nat) { struct bsc_config *cfg, *tmp; - struct bsc_nat_acc_lst *lst, *tmp_lst; + struct bsc_msg_acc_lst *lst, *tmp_lst; llist_for_each_entry_safe(cfg, tmp, &nat->bsc_configs, entry) bsc_config_free(cfg); llist_for_each_entry_safe(lst, tmp_lst, &nat->access_lists, list) - bsc_nat_acc_lst_delete(lst); + bsc_msg_acc_lst_delete(lst); bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, NULL); diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 3a53dd402..502bdddd4 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -87,7 +87,7 @@ static void write_pgroup_lst(struct vty *vty, struct bsc_nat_paging_group *pgrou static int config_write_nat(struct vty *vty) { - struct bsc_nat_acc_lst *lst; + struct bsc_msg_acc_lst *lst; struct bsc_nat_paging_group *pgroup; vty_out(vty, "nat%s", VTY_NEWLINE); @@ -136,7 +136,7 @@ static int config_write_nat(struct vty *vty) _nat->num_rewr_trie_name, VTY_NEWLINE); llist_for_each_entry(lst, &_nat->access_lists, list) - bsc_nat_acc_lst_write(vty, lst); + bsc_msg_acc_lst_write(vty, lst); llist_for_each_entry(pgroup, &_nat->paging_groups, entry) write_pgroup_lst(vty, pgroup); if (_nat->mgcp_ipa) @@ -1171,7 +1171,7 @@ int bsc_nat_vty_init(struct bsc_nat *nat) install_element(NAT_NODE, &cfg_nat_ussd_local_cmd); install_element(NAT_NODE, &cfg_nat_use_ipa_for_mgcp_cmd); - bsc_nat_lst_vty_init(nat, &nat->access_lists, NAT_NODE); + bsc_msg_lst_vty_init(nat, &nat->access_lists, NAT_NODE); /* number rewriting */ install_element(NAT_NODE, &cfg_nat_number_rewrite_cmd); diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index f27453bc3..968456b95 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -374,7 +374,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse uint8_t proto; uint8_t ti; struct gsm48_hdr *hdr48; - struct bsc_nat_acc_lst *lst; + struct bsc_msg_acc_lst *lst; struct ussd_request req; /* @@ -416,12 +416,12 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse if (msg_type == GSM0480_MTYPE_REGISTER) { /* now check if it is a IMSI we care about */ - lst = bsc_nat_acc_lst_find(&con->bsc->nat->access_lists, + lst = bsc_msg_acc_lst_find(&con->bsc->nat->access_lists, con->bsc->nat->ussd_lst_name); if (!lst) return 0; - if (bsc_nat_lst_check_allow(lst, con->imsi) != 0) + if (bsc_msg_acc_lst_check_allow(lst, con->imsi) != 0) return 0; /* now decode the message and see if we really want to handle it */ diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 8ee8e3945..61b3bd161 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -868,8 +868,8 @@ static void test_cr_filter() int i, res, contype; struct msgb *msg = msgb_alloc(4096, "test_cr_filter"); struct bsc_nat_parsed *parsed; - struct bsc_nat_acc_lst *nat_lst, *bsc_lst; - struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry; + struct bsc_msg_acc_lst *nat_lst, *bsc_lst; + struct bsc_msg_acc_lst_entry *nat_entry, *bsc_entry; struct bsc_nat_reject_cause cause; struct bsc_nat *nat = bsc_nat_alloc(); @@ -879,11 +879,11 @@ static void test_cr_filter() bsc->cfg->acc_lst_name = "bsc"; nat->acc_lst_name = "nat"; - nat_lst = bsc_nat_acc_lst_get(nat, &nat->access_lists, "nat"); - bsc_lst = bsc_nat_acc_lst_get(nat, &nat->access_lists, "bsc"); + nat_lst = bsc_msg_acc_lst_get(nat, &nat->access_lists, "nat"); + bsc_lst = bsc_msg_acc_lst_get(nat, &nat->access_lists, "bsc"); - bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst); - nat_entry = bsc_nat_acc_lst_entry_create(nat_lst); + bsc_entry = bsc_msg_acc_lst_entry_create(bsc_lst); + nat_entry = bsc_msg_acc_lst_entry_create(nat_lst); /* test the default value as we are going to overwrite it */ OSMO_ASSERT(bsc_entry->cm_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED); -- cgit v1.2.3 From 14b2cd9f321974e211f3291a6ccc2d1aaaf948d1 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 16:50:34 +0200 Subject: filter: Rename BSC to LOCAL and NAT to GLOBAL --- openbsc/include/openbsc/bsc_msg_filter.h | 4 ++-- openbsc/src/libfilter/bsc_msg_acc.c | 4 ++-- openbsc/src/libfilter/bsc_msg_filter.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 8420e48af..4ecd28605 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -28,8 +28,8 @@ struct bsc_nat_barr_entry { }; enum bsc_nat_acc_ctr { - ACC_LIST_BSC_FILTER, - ACC_LIST_NAT_FILTER, + ACC_LIST_LOCAL_FILTER, + ACC_LIST_GLOBAL_FILTER, }; struct bsc_msg_acc_lst { diff --git a/openbsc/src/libfilter/bsc_msg_acc.c b/openbsc/src/libfilter/bsc_msg_acc.c index d2f45b36f..6258b3577 100644 --- a/openbsc/src/libfilter/bsc_msg_acc.c +++ b/openbsc/src/libfilter/bsc_msg_acc.c @@ -26,8 +26,8 @@ #include static const struct rate_ctr_desc acc_list_ctr_description[] = { - [ACC_LIST_BSC_FILTER] = { "access-list.bsc-filter", "Rejected by rule for BSC"}, - [ACC_LIST_NAT_FILTER] = { "access-list.nat-filter", "Rejected by rule for NAT"}, + [ACC_LIST_LOCAL_FILTER] = { "access-list.local-filter", "Rejected by rule for local"}, + [ACC_LIST_GLOBAL_FILTER]= { "access-list.global-filter", "Rejected by rule for global"}, }; static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 19367a013..e72ec0a37 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -181,7 +181,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); - rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_BSC_FILTER]); + rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_LOCAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; return -2; @@ -194,7 +194,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); - rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_NAT_FILTER]); + rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_GLOBAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; return -3; -- cgit v1.2.3 From c36a6d5705830a6fd6e41e684dc031db35165e04 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 16:55:28 +0200 Subject: filter: More renaming and remove of "NAT" from it --- openbsc/include/openbsc/bsc_msg_filter.h | 14 +++++++------- openbsc/src/libfilter/bsc_msg_filter.c | 30 +++++++++++++++--------------- openbsc/src/osmo-bsc_nat/bsc_nat.c | 6 +++--- openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 10 +++++----- openbsc/tests/bsc-nat/bsc_nat_test.c | 22 +++++++++++----------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 4ecd28605..7e66517de 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -14,12 +14,12 @@ struct bsc_nat_parsed; struct bsc_connection; struct nat_sccp_connection; -struct bsc_nat_reject_cause { +struct bsc_filter_reject_cause { int lu_reject_cause; int cm_reject_cause; }; -struct bsc_nat_barr_entry { +struct bsc_filter_barr_entry { struct rb_node node; char *imsi; @@ -27,7 +27,7 @@ struct bsc_nat_barr_entry { int lu_reject_cause; }; -enum bsc_nat_acc_ctr { +enum bsc_filter_acc_ctr { ACC_LIST_LOCAL_FILTER, ACC_LIST_GLOBAL_FILTER, }; @@ -58,18 +58,18 @@ struct bsc_msg_acc_lst_entry { }; -int bsc_nat_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); -int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); +int bsc_filter_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); +int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); /** * Content filtering. */ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *, int *con_type, char **imsi, - struct bsc_nat_reject_cause *cause); + 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_nat_reject_cause *cause); + struct bsc_filter_reject_cause *cause); /* IMSI allow/deny handling */ struct bsc_msg_acc_lst *bsc_msg_acc_lst_find(struct llist_head *lst, const char *name); diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index e72ec0a37..5f8bff004 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -38,10 +38,10 @@ #include -int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) +int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) { - struct bsc_nat_barr_entry *n; - n = rb_entry(root->rb_node, struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry *n; + n = rb_entry(root->rb_node, struct bsc_filter_barr_entry, node); while (n) { int rc = strcmp(imsi, n->imsi); @@ -53,20 +53,20 @@ int bsc_nat_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) n = rb_entry( (rc < 0) ? n->node.rb_left : n->node.rb_right, - struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry, node); }; return 0; } -static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *root) +static int insert_barr_node(struct bsc_filter_barr_entry *entry, struct rb_root *root) { struct rb_node **new = &root->rb_node, *parent = NULL; while (*new) { int rc; - struct bsc_nat_barr_entry *this; - this = rb_entry(*new, struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry *this; + this = rb_entry(*new, struct bsc_filter_barr_entry, node); parent = *new; rc = strcmp(entry->imsi, this->imsi); @@ -87,7 +87,7 @@ static int insert_barr_node(struct bsc_nat_barr_entry *entry, struct rb_root *ro return 0; } -int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, +int bsc_filter_barr_adapt(void *ctx, struct rb_root *root, const struct osmo_config_list *list) { struct osmo_config_entry *cfg_entry; @@ -105,8 +105,8 @@ int bsc_nat_barr_adapt(void *ctx, struct rb_root *root, /* now adapt the new list */ llist_for_each_entry(cfg_entry, &list->entry, list) { - struct bsc_nat_barr_entry *entry; - entry = talloc_zero(ctx, struct bsc_nat_barr_entry); + struct bsc_filter_barr_entry *entry; + entry = talloc_zero(ctx, struct bsc_filter_barr_entry); if (!entry) { LOGP(DNAT, LOGL_ERROR, "Allocation of the barr entry failed.\n"); @@ -143,7 +143,7 @@ static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, /* apply white/black list */ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, - struct bsc_nat_reject_cause *cause) + struct bsc_filter_reject_cause *cause) { /* * Now apply blacklist/whitelist of the BSC and the NAT. @@ -158,7 +158,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, struct bsc_msg_acc_lst *bsc_lst = NULL; /* 1. global check for barred imsis */ - if (bsc_nat_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { + if (bsc_filter_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; LOGP(DNAT, LOGL_DEBUG, @@ -300,7 +300,7 @@ static int _cr_check_pag_resp(struct bsc_connection *bsc, static int _dt_check_id_resp(struct bsc_connection *bsc, uint8_t *data, unsigned int length, struct nat_sccp_connection *con, - struct bsc_nat_reject_cause *cause) + struct bsc_filter_reject_cause *cause) { char mi_string[GSM48_MI_SIZE]; uint8_t mi_type; @@ -330,7 +330,7 @@ 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, - char **imsi, struct bsc_nat_reject_cause *cause) + char **imsi, struct bsc_filter_reject_cause *cause) { struct tlv_parsed tp; struct gsm48_hdr *hdr48; @@ -414,7 +414,7 @@ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, - struct bsc_nat_reject_cause *cause) + struct bsc_filter_reject_cause *cause) { uint32_t len; uint8_t msg_type, proto; diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c index ebd291b7f..116c612c1 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c @@ -426,7 +426,7 @@ static void bsc_stat_reject(int filter, struct bsc_connection *bsc, int normal) */ static void bsc_send_con_release(struct bsc_connection *bsc, struct nat_sccp_connection *con, - struct bsc_nat_reject_cause *cause) + struct bsc_filter_reject_cause *cause) { struct msgb *rlsd; /* 1. release the network */ @@ -476,7 +476,7 @@ static void bsc_send_con_release(struct bsc_connection *bsc, static void bsc_send_con_refuse(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed, int con_type, - struct bsc_nat_reject_cause *cause) + struct bsc_filter_reject_cause *cause) { struct msgb *payload; struct msgb *refuse; @@ -1026,7 +1026,7 @@ static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) struct bsc_connection *con_bsc = NULL; int con_type; struct bsc_nat_parsed *parsed; - struct bsc_nat_reject_cause cause; + struct bsc_filter_reject_cause cause; /* Parse and filter messages */ parsed = bsc_nat_parse(msg); diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 502bdddd4..596ecaee4 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -503,7 +503,7 @@ DEFUN(cfg_nat_imsi_black_list_fn, int rc; struct osmo_config_list *rewr = NULL; rewr = osmo_config_list_parse(_nat, _nat->imsi_black_list_fn); - rc = bsc_nat_barr_adapt(_nat, &_nat->imsi_black_list, rewr); + rc = bsc_filter_barr_adapt(_nat, &_nat->imsi_black_list, rewr); if (rc != 0) { vty_out(vty, "%%There was an error parsing the list." " Please see the error log.%s", VTY_NEWLINE); @@ -513,7 +513,7 @@ DEFUN(cfg_nat_imsi_black_list_fn, return CMD_SUCCESS; } - bsc_nat_barr_adapt(_nat, &_nat->imsi_black_list, NULL); + bsc_filter_barr_adapt(_nat, &_nat->imsi_black_list, NULL); return CMD_SUCCESS; } @@ -524,7 +524,7 @@ DEFUN(cfg_nat_no_imsi_black_list_fn, { talloc_free(_nat->imsi_black_list_fn); _nat->imsi_black_list_fn = NULL; - bsc_nat_barr_adapt(_nat, &_nat->imsi_black_list, NULL); + bsc_filter_barr_adapt(_nat, &_nat->imsi_black_list, NULL); return CMD_SUCCESS; } @@ -862,8 +862,8 @@ DEFUN(show_bar_lst, vty_out(vty, "IMSIs barred from the network:%s", VTY_NEWLINE); for (node = rb_first(&_nat->imsi_black_list); node; node = rb_next(node)) { - struct bsc_nat_barr_entry *entry; - entry = rb_entry(node, struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry *entry; + entry = rb_entry(node, struct bsc_filter_barr_entry, node); vty_out(vty, " IMSI(%s) CM-Reject-Cause(%d) LU-Reject-Cause(%d)%s", entry->imsi, entry->cm_reject_cause, entry->lu_reject_cause, diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 61b3bd161..2020c7ad6 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -870,7 +870,7 @@ static void test_cr_filter() struct bsc_nat_parsed *parsed; struct bsc_msg_acc_lst *nat_lst, *bsc_lst; struct bsc_msg_acc_lst_entry *nat_entry, *bsc_entry; - struct bsc_nat_reject_cause cause; + struct bsc_filter_reject_cause cause; struct bsc_nat *nat = bsc_nat_alloc(); struct bsc_connection *bsc = bsc_connection_alloc(nat); @@ -947,7 +947,7 @@ static void test_dt_filter() int i; struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); struct bsc_nat_parsed *parsed; - struct bsc_nat_reject_cause cause; + struct bsc_filter_reject_cause cause; struct bsc_nat *nat = bsc_nat_alloc(); struct bsc_connection *bsc = bsc_connection_alloc(nat); @@ -1454,21 +1454,21 @@ static void test_barr_list_parsing(void) if (lst == NULL) abort(); - rc = bsc_nat_barr_adapt(NULL, &root, lst); + rc = bsc_filter_barr_adapt(NULL, &root, lst); if (rc != 0) abort(); talloc_free(lst); for (node = rb_first(&root); node; node = rb_next(node)) { - struct bsc_nat_barr_entry *entry; - entry = rb_entry(node, struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry *entry; + entry = rb_entry(node, struct bsc_filter_barr_entry, node); printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, entry->cm_reject_cause, entry->lu_reject_cause); } /* do the look up now.. */ - rc = bsc_nat_barr_find(&root, "12123119", &cm, &lu); + rc = bsc_filter_barr_find(&root, "12123119", &cm, &lu); if (!rc) { printf("Failed to find the IMSI.\n"); abort(); @@ -1480,7 +1480,7 @@ static void test_barr_list_parsing(void) } /* empty and check that it is empty */ - bsc_nat_barr_adapt(NULL, &root, NULL); + bsc_filter_barr_adapt(NULL, &root, NULL); if (!RB_EMPTY_ROOT(&root)) { printf("Failed to empty the list.\n"); abort(); @@ -1493,7 +1493,7 @@ static void test_barr_list_parsing(void) abort(); } - rc = bsc_nat_barr_adapt(NULL, &root, lst); + rc = bsc_filter_barr_adapt(NULL, &root, lst); if (rc != -1) { printf("It should have failed due dup\n"); abort(); @@ -1502,13 +1502,13 @@ static void test_barr_list_parsing(void) /* dump for reference */ for (node = rb_first(&root); node; node = rb_next(node)) { - struct bsc_nat_barr_entry *entry; - entry = rb_entry(node, struct bsc_nat_barr_entry, node); + struct bsc_filter_barr_entry *entry; + entry = rb_entry(node, struct bsc_filter_barr_entry, node); printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, entry->cm_reject_cause, entry->lu_reject_cause); } - rc = bsc_nat_barr_adapt(NULL, &root, NULL); + rc = bsc_filter_barr_adapt(NULL, &root, NULL); } static void test_nat_extract_lac() -- cgit v1.2.3 From 4ba947bf4b80b553a3411ec1fd29c45430bd6c40 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 18:07:45 +0200 Subject: filter: Separate SCCP/BSSAP extraction and gsm48 code For the BSC we will have the gsm48_hdr and don't need to find data within SCCP. For legacy reasons we need to initialize con_type, imsi, reject causes early on and need to do the same in the filter method. --- openbsc/include/openbsc/bsc_msg_filter.h | 11 ++-- openbsc/include/openbsc/bsc_nat.h | 8 +++ openbsc/src/libfilter/bsc_msg_filter.c | 58 ++--------------- openbsc/src/osmo-bsc_nat/Makefile.am | 2 +- openbsc/src/osmo-bsc_nat/bsc_nat_filter.c | 105 ++++++++++++++++++++++++++++++ openbsc/tests/bsc-nat/Makefile.am | 3 +- 6 files changed, 130 insertions(+), 57 deletions(-) create mode 100644 openbsc/src/osmo-bsc_nat/bsc_nat_filter.c 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 \ -- cgit v1.2.3 From a0478814bc1c56a9e5648ffecd7c11671804e6ff Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 18:42:45 +0200 Subject: filter: Remove NAT knowledge from auth_imsi Push back the parameters we need to pass. auth_imsi doesn't know anything about the nat now. --- openbsc/src/libfilter/bsc_msg_filter.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index a1271a095..486ed9368 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -142,7 +142,10 @@ static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, } /* apply white/black list */ -static int auth_imsi(struct bsc_connection *bsc, const char *imsi, +static int auth_imsi(struct rb_root *black_list, + struct llist_head *access_lists, + const char *local_lst_name, const char *global_lst_name, + int bsc_nr, const char *imsi, struct bsc_filter_reject_cause *cause) { /* @@ -158,7 +161,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, struct bsc_msg_acc_lst *bsc_lst = NULL; /* 1. global check for barred imsis */ - if (bsc_filter_barr_find(&bsc->nat->imsi_black_list, imsi, &cm, &lu)) { + if (bsc_filter_barr_find(black_list, imsi, &cm, &lu)) { cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; LOGP(DNAT, LOGL_DEBUG, @@ -168,8 +171,8 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, } - bsc_lst = bsc_msg_acc_lst_find(&bsc->nat->access_lists, bsc->cfg->acc_lst_name); - nat_lst = bsc_msg_acc_lst_find(&bsc->nat->access_lists, bsc->nat->acc_lst_name); + bsc_lst = bsc_msg_acc_lst_find(access_lists, local_lst_name); + nat_lst = bsc_msg_acc_lst_find(access_lists, global_lst_name); if (bsc_lst) { @@ -180,7 +183,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, /* 3. BSC deny */ if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, - "Filtering %s by imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); + "Filtering %s by imsi_deny on config nr: %d.\n", imsi, bsc_nr); rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_LOCAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; @@ -193,7 +196,7 @@ static int auth_imsi(struct bsc_connection *bsc, const char *imsi, if (nat_lst) { if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, - "Filtering %s by nat imsi_deny on bsc nr: %d.\n", imsi, bsc->cfg->nr); + "Filtering %s global imsi_deny on bsc nr: %d.\n", imsi, bsc_nr); rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_GLOBAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; @@ -323,7 +326,10 @@ static int _dt_check_id_resp(struct bsc_connection *bsc, con->imsi_checked = 1; con->imsi = talloc_strdup(con, mi_string); - return auth_imsi(bsc, mi_string, cause); + return auth_imsi(&bsc->nat->imsi_black_list, + &bsc->nat->access_lists, + bsc->cfg->acc_lst_name, bsc->nat->acc_lst_name, + bsc->cfg->nr, mi_string, cause); } @@ -374,7 +380,10 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, return -1; /* now check the imsi */ - return auth_imsi(bsc, *imsi, cause); + return auth_imsi(&bsc->nat->imsi_black_list, + &bsc->nat->access_lists, + bsc->cfg->acc_lst_name, bsc->nat->acc_lst_name, + bsc->cfg->nr, *imsi, cause); } int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, -- cgit v1.2.3 From 71857d72426c75e4bb71c3843a9eeeff53eca37d Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 18:48:27 +0200 Subject: filter: Put all the parameters in a struct to avoid order issues With the "local" and "global" list name we might pick the wrong argument. Avoid it by passing them as a struct. --- openbsc/src/libfilter/bsc_msg_filter.c | 50 +++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 486ed9368..a24e9de8b 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -38,6 +38,14 @@ #include +struct filter_request { + struct rb_root *black_list; + struct llist_head *access_lists; + const char *local_lst_name; + const char *global_lst_name; + int bsc_nr; +}; + int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) { struct bsc_filter_barr_entry *n; @@ -142,10 +150,8 @@ static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, } /* apply white/black list */ -static int auth_imsi(struct rb_root *black_list, - struct llist_head *access_lists, - const char *local_lst_name, const char *global_lst_name, - int bsc_nr, const char *imsi, +static int auth_imsi(struct filter_request *req, + const char *imsi, struct bsc_filter_reject_cause *cause) { /* @@ -161,7 +167,7 @@ static int auth_imsi(struct rb_root *black_list, struct bsc_msg_acc_lst *bsc_lst = NULL; /* 1. global check for barred imsis */ - if (bsc_filter_barr_find(black_list, imsi, &cm, &lu)) { + if (bsc_filter_barr_find(req->black_list, imsi, &cm, &lu)) { cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; LOGP(DNAT, LOGL_DEBUG, @@ -171,8 +177,8 @@ static int auth_imsi(struct rb_root *black_list, } - bsc_lst = bsc_msg_acc_lst_find(access_lists, local_lst_name); - nat_lst = bsc_msg_acc_lst_find(access_lists, global_lst_name); + bsc_lst = bsc_msg_acc_lst_find(req->access_lists, req->local_lst_name); + nat_lst = bsc_msg_acc_lst_find(req->access_lists, req->global_lst_name); if (bsc_lst) { @@ -183,7 +189,7 @@ static int auth_imsi(struct rb_root *black_list, /* 3. BSC deny */ if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, - "Filtering %s by imsi_deny on config nr: %d.\n", imsi, bsc_nr); + "Filtering %s by imsi_deny on config nr: %d.\n", imsi, req->bsc_nr); rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_LOCAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; @@ -196,7 +202,7 @@ static int auth_imsi(struct rb_root *black_list, if (nat_lst) { if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { LOGP(DNAT, LOGL_ERROR, - "Filtering %s global imsi_deny on bsc nr: %d.\n", imsi, bsc_nr); + "Filtering %s global imsi_deny on bsc nr: %d.\n", imsi, req->bsc_nr); rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_GLOBAL_FILTER]); cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; @@ -300,7 +306,7 @@ static int _cr_check_pag_resp(struct bsc_connection *bsc, return 1; } -static int _dt_check_id_resp(struct bsc_connection *bsc, +static int _dt_check_id_resp(struct filter_request *req, uint8_t *data, unsigned int length, struct nat_sccp_connection *con, struct bsc_filter_reject_cause *cause) @@ -326,10 +332,7 @@ static int _dt_check_id_resp(struct bsc_connection *bsc, con->imsi_checked = 1; con->imsi = talloc_strdup(con, mi_string); - return auth_imsi(&bsc->nat->imsi_black_list, - &bsc->nat->access_lists, - bsc->cfg->acc_lst_name, bsc->nat->acc_lst_name, - bsc->cfg->nr, mi_string, cause); + return auth_imsi(req, mi_string, cause); } @@ -339,6 +342,7 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, int *con_type, char **imsi, struct bsc_filter_reject_cause *cause) { + struct filter_request req; int ret = 0; uint8_t msg_type, proto; @@ -380,10 +384,12 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, return -1; /* now check the imsi */ - return auth_imsi(&bsc->nat->imsi_black_list, - &bsc->nat->access_lists, - bsc->cfg->acc_lst_name, bsc->nat->acc_lst_name, - bsc->cfg->nr, *imsi, cause); + req.black_list = &bsc->nat->imsi_black_list; + req.access_lists = &bsc->nat->access_lists; + req.local_lst_name = bsc->cfg->acc_lst_name; + req.global_lst_name = bsc->nat->acc_lst_name; + req.bsc_nr = bsc->cfg->nr; + return auth_imsi(&req, *imsi, cause); } int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, @@ -391,6 +397,7 @@ int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, struct nat_sccp_connection *con, struct bsc_filter_reject_cause *cause) { + struct filter_request req; uint8_t msg_type, proto; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; @@ -404,6 +411,11 @@ int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP) return 0; - return _dt_check_id_resp(bsc, &hdr48->data[0], + req.black_list = &bsc->nat->imsi_black_list; + req.access_lists = &bsc->nat->access_lists; + req.local_lst_name = bsc->cfg->acc_lst_name; + req.global_lst_name = bsc->nat->acc_lst_name; + req.bsc_nr = bsc->cfg->nr; + return _dt_check_id_resp(&req, &hdr48->data[0], len - sizeof(*hdr48), con, cause); } -- cgit v1.2.3 From 81dbfe412c8196df443f38bf4fbea4f9897607ef Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 18:58:55 +0200 Subject: filter: Remove the bsc_connection from the internal functions --- openbsc/src/libfilter/bsc_msg_filter.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index a24e9de8b..b836f432d 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -213,7 +213,7 @@ static int auth_imsi(struct filter_request *req, return 1; } -static int _cr_check_loc_upd(struct bsc_connection *bsc, +static int _cr_check_loc_upd(void *ctx, uint8_t *data, unsigned int length, char **imsi) { @@ -238,11 +238,11 @@ static int _cr_check_loc_upd(struct bsc_connection *bsc, return 0; gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len); - *imsi = talloc_strdup(bsc, mi_string); + *imsi = talloc_strdup(ctx, mi_string); return 1; } -static int _cr_check_cm_serv_req(struct bsc_connection *bsc, +static int _cr_check_cm_serv_req(void *ctx, uint8_t *data, unsigned int length, int *con_type, char **imsi) { @@ -276,11 +276,11 @@ static int _cr_check_cm_serv_req(struct bsc_connection *bsc, if (mi_type != GSM_MI_TYPE_IMSI) return 0; - *imsi = talloc_strdup(bsc, mi_string); + *imsi = talloc_strdup(ctx, mi_string); return 1; } -static int _cr_check_pag_resp(struct bsc_connection *bsc, +static int _cr_check_pag_resp(void *ctx, uint8_t *data, unsigned int length, char **imsi) { struct gsm48_pag_resp *resp; @@ -302,7 +302,7 @@ static int _cr_check_pag_resp(struct bsc_connection *bsc, if (mi_type != GSM_MI_TYPE_IMSI) return 0; - *imsi = talloc_strdup(bsc, mi_string); + *imsi = talloc_strdup(ctx, mi_string); return 1; } -- cgit v1.2.3 From c09f8a3b7fb94ccef41e33c32bfe2bff1ffe0e44 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 19:13:27 +0200 Subject: filter: Remove nat_sccp_connection from public API --- openbsc/include/openbsc/bsc_msg_filter.h | 9 ++++++--- openbsc/include/openbsc/bsc_nat_sccp.h | 6 ++++-- openbsc/src/libfilter/bsc_msg_filter.c | 16 ++++++++-------- openbsc/src/osmo-bsc_nat/bsc_nat.c | 12 +++++++----- openbsc/src/osmo-bsc_nat/bsc_nat_filter.c | 4 ++-- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 13 +++++++------ openbsc/tests/bsc-nat/bsc_nat_test.c | 2 +- 7 files changed, 35 insertions(+), 27 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index a974195c8..64890500c 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -11,9 +11,7 @@ struct vty; struct gsm48_hdr; /* TODO: remove */ -struct bsc_nat_parsed; struct bsc_connection; -struct nat_sccp_connection; struct bsc_filter_reject_cause { int lu_reject_cause; @@ -58,6 +56,11 @@ struct bsc_msg_acc_lst_entry { int lu_reject_cause; }; +struct bsc_filter_state { + char *imsi; + int imsi_checked; +}; + int bsc_filter_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); @@ -71,7 +74,7 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr, size_t size, struct bsc_filter_reject_cause *cause); int bsc_msg_filter_data(struct gsm48_hdr *hdr, size_t size, struct bsc_connection *bsc, - struct nat_sccp_connection *con, + struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause); /* IMSI allow/deny handling */ diff --git a/openbsc/include/openbsc/bsc_nat_sccp.h b/openbsc/include/openbsc/bsc_nat_sccp.h index 0561df1f4..d2490ca6a 100644 --- a/openbsc/include/openbsc/bsc_nat_sccp.h +++ b/openbsc/include/openbsc/bsc_nat_sccp.h @@ -22,6 +22,8 @@ #ifndef BSC_NAT_SCCP_H #define BSC_NAT_SCCP_H +#include "bsc_msg_filter.h" + #include /* @@ -80,8 +82,8 @@ struct nat_sccp_connection { int con_type; int con_local; int authorized; - int imsi_checked; - char *imsi; + + struct bsc_filter_state filter_state; uint16_t lac; uint16_t ci; diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index b836f432d..52c71b99c 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -306,9 +306,9 @@ static int _cr_check_pag_resp(void *ctx, return 1; } -static int _dt_check_id_resp(struct filter_request *req, +static int _dt_check_id_resp(void *ctx, struct filter_request *req, uint8_t *data, unsigned int length, - struct nat_sccp_connection *con, + struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause) { char mi_string[GSM48_MI_SIZE]; @@ -330,8 +330,8 @@ static int _dt_check_id_resp(struct filter_request *req, if (mi_type != GSM_MI_TYPE_IMSI) return 0; - con->imsi_checked = 1; - con->imsi = talloc_strdup(con, mi_string); + state->imsi_checked = 1; + state->imsi = talloc_strdup(ctx, mi_string); return auth_imsi(req, mi_string, cause); } @@ -394,7 +394,7 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, struct bsc_connection *bsc, - struct nat_sccp_connection *con, + struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause) { struct filter_request req; @@ -403,7 +403,7 @@ int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; - if (con->imsi_checked) + if (state->imsi_checked) return 0; proto = hdr48->proto_discr & 0x0f; @@ -416,6 +416,6 @@ int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, req.local_lst_name = bsc->cfg->acc_lst_name; req.global_lst_name = bsc->nat->acc_lst_name; req.bsc_nr = bsc->cfg->nr; - return _dt_check_id_resp(&req, &hdr48->data[0], - len - sizeof(*hdr48), con, cause); + return _dt_check_id_resp(bsc, &req, &hdr48->data[0], + len - sizeof(*hdr48), state, cause); } diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c index 116c612c1..3ec43b15f 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c @@ -1073,10 +1073,10 @@ static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) con->msc_con = bsc->nat->msc_con; con_msc = con->msc_con; con->con_type = con_type; - con->imsi_checked = filter; + con->filter_state.imsi_checked = filter; bsc_nat_extract_lac(bsc, con, parsed, msg); if (imsi) - con->imsi = talloc_steal(con, imsi); + con->filter_state.imsi = talloc_steal(con, imsi); imsi = NULL; con_bsc = con->bsc; handle_con_stats(con); @@ -1094,8 +1094,9 @@ static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) filter = bsc_nat_filter_dt(bsc, msg, con, parsed, &cause); if (filter < 0) { - if (con->imsi) - bsc_nat_inform_reject(bsc, con->imsi); + if (con->filter_state.imsi) + bsc_nat_inform_reject(bsc, + con->filter_state.imsi); bsc_stat_reject(filter, bsc, 1); bsc_send_con_release(bsc, con, &cause); con = NULL; @@ -1111,7 +1112,8 @@ static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) * replace the msg and the parsed structure becomes * invalid. */ - msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, con->imsi); + msg = bsc_nat_rewrite_msg(bsc->nat, msg, parsed, + con->filter_state.imsi); talloc_free(parsed); parsed = NULL; } else if (con->con_local == NAT_CON_END_USSD) { diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c index 11d370c4c..af0f7a189 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c @@ -90,7 +90,7 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; - if (con->imsi_checked) + if (con->filter_state.imsi_checked) return 0; /* only care about DTAP messages */ @@ -101,5 +101,5 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, if (!hdr48) return -1; - return bsc_msg_filter_data(hdr48, len, bsc, con, cause); + return bsc_msg_filter_data(hdr48, len, bsc, &con->filter_state, cause); } diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index 968456b95..224189608 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -351,7 +351,7 @@ static int forward_ussd(struct nat_sccp_connection *con, const struct ussd_reque state->invoke_id = req->invoke_id; memcpy(&state->src_ref, &con->remote_ref, sizeof(con->remote_ref)); memcpy(&state->dst_ref, &con->real_ref, sizeof(con->real_ref)); - memcpy(state->imsi, con->imsi, strlen(con->imsi)); + memcpy(state->imsi, con->filter_state.imsi, strlen(con->filter_state.imsi)); /* add additional tag/values */ lac = htons(con->lac); @@ -385,7 +385,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse if (con->con_type != NAT_CON_TYPE_SSA) return 0; - if (!con->imsi) + if (!con->filter_state.imsi) return 0; /* We have not verified the IMSI yet */ @@ -400,7 +400,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse if (parsed->bssap != BSSAP_MSG_DTAP) return 0; - if (strlen(con->imsi) >= GSM_IMSI_LENGTH) + if (strlen(con->filter_state.imsi) >= GSM_IMSI_LENGTH) return 0; hdr48 = bsc_unpack_dtap(parsed, msg, &len); @@ -421,7 +421,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse if (!lst) return 0; - if (bsc_msg_acc_lst_check_allow(lst, con->imsi) != 0) + if (bsc_msg_acc_lst_check_allow(lst, con->filter_state.imsi) != 0) return 0; /* now decode the message and see if we really want to handle it */ @@ -436,14 +436,15 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse return 0; /* found a USSD query for our subscriber */ - LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", con->imsi); + LOGP(DNAT, LOGL_NOTICE, "Found USSD query for %s\n", + con->filter_state.imsi); con->ussd_ti[ti] = 1; if (forward_ussd(con, &req, msg) != 0) return 0; return 1; } else if (msg_type == GSM0480_MTYPE_FACILITY && con->ussd_ti[ti]) { LOGP(DNAT, LOGL_NOTICE, "Forwarding message part of TI: %d %s\n", - ti, con->imsi); + ti, con->filter_state.imsi); if (forward_ussd_simple(con, msg) != 0) return 0; return 1; diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index 2020c7ad6..e883ddd43 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -992,7 +992,7 @@ static void test_dt_filter() if (!parsed) continue; - con->imsi_checked = 0; + con->filter_state.imsi_checked = 0; memset(&cause, 0, sizeof(cause)); bsc_nat_filter_dt(bsc, msg, con, parsed, &cause); } -- cgit v1.2.3 From 4e8176d0c9c9a98f32cb4541b4f986de0011d4b9 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 19:20:09 +0200 Subject: filter: Remove bsc_connection from the filter API Remove the last occurence of NAT datastructures in the filtering module and add the ctx to the filter request structure. --- openbsc/include/openbsc/bsc_msg_filter.h | 16 +++++++++---- openbsc/src/libfilter/bsc_msg_filter.c | 40 ++++++++----------------------- openbsc/src/osmo-bsc_nat/bsc_nat_filter.c | 18 ++++++++++++-- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index 64890500c..b09cd8aa6 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -10,9 +10,6 @@ struct vty; struct gsm48_hdr; -/* TODO: remove */ -struct bsc_connection; - struct bsc_filter_reject_cause { int lu_reject_cause; int cm_reject_cause; @@ -61,6 +58,15 @@ struct bsc_filter_state { int imsi_checked; }; +struct bsc_filter_request { + void *ctx; + struct rb_root *black_list; + struct llist_head *access_lists; + const char *local_lst_name; + const char *global_lst_name; + int bsc_nr; +}; + int bsc_filter_barr_adapt(void *ctx, struct rb_root *rbtree, const struct osmo_config_list *); int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu); @@ -69,11 +75,11 @@ int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *l * Content filtering. */ int bsc_msg_filter_initial(struct gsm48_hdr *hdr, size_t size, - struct bsc_connection *bsc, + struct bsc_filter_request *req, int *con_type, char **imsi, struct bsc_filter_reject_cause *cause); int bsc_msg_filter_data(struct gsm48_hdr *hdr, size_t size, - struct bsc_connection *bsc, + struct bsc_filter_request *req, struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause); diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 52c71b99c..159f6ccec 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -38,14 +38,6 @@ #include -struct filter_request { - struct rb_root *black_list; - struct llist_head *access_lists; - const char *local_lst_name; - const char *global_lst_name; - int bsc_nr; -}; - int bsc_filter_barr_find(struct rb_root *root, const char *imsi, int *cm, int *lu) { struct bsc_filter_barr_entry *n; @@ -150,7 +142,7 @@ static int lst_check_deny(struct bsc_msg_acc_lst *lst, const char *mi_string, } /* apply white/black list */ -static int auth_imsi(struct filter_request *req, +static int auth_imsi(struct bsc_filter_request *req, const char *imsi, struct bsc_filter_reject_cause *cause) { @@ -306,7 +298,7 @@ static int _cr_check_pag_resp(void *ctx, return 1; } -static int _dt_check_id_resp(void *ctx, struct filter_request *req, +static int _dt_check_id_resp(struct bsc_filter_request *req, uint8_t *data, unsigned int length, struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause) @@ -331,18 +323,17 @@ static int _dt_check_id_resp(void *ctx, struct filter_request *req, return 0; state->imsi_checked = 1; - state->imsi = talloc_strdup(ctx, mi_string); + state->imsi = talloc_strdup(req->ctx, mi_string); return auth_imsi(req, mi_string, cause); } /* Filter out CR data... */ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, - struct bsc_connection *bsc, + struct bsc_filter_request *req, int *con_type, char **imsi, struct bsc_filter_reject_cause *cause) { - struct filter_request req; int ret = 0; uint8_t msg_type, proto; @@ -356,18 +347,18 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, if (proto == GSM48_PDISC_MM && msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { *con_type = NAT_CON_TYPE_LU; - ret = _cr_check_loc_upd(bsc, &hdr48->data[0], + ret = _cr_check_loc_upd(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); } else if (proto == GSM48_PDISC_MM && msg_type == GSM48_MT_MM_CM_SERV_REQ) { *con_type = NAT_CON_TYPE_CM_SERV_REQ; - ret = _cr_check_cm_serv_req(bsc, &hdr48->data[0], + ret = _cr_check_cm_serv_req(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), con_type, imsi); } else if (proto == GSM48_PDISC_RR && msg_type == GSM48_MT_RR_PAG_RESP) { *con_type = NAT_CON_TYPE_PAG_RESP; - ret = _cr_check_pag_resp(bsc, &hdr48->data[0], + ret = _cr_check_pag_resp(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); } else { /* We only want to filter the above, let other things pass */ @@ -384,20 +375,14 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, return -1; /* now check the imsi */ - req.black_list = &bsc->nat->imsi_black_list; - req.access_lists = &bsc->nat->access_lists; - req.local_lst_name = bsc->cfg->acc_lst_name; - req.global_lst_name = bsc->nat->acc_lst_name; - req.bsc_nr = bsc->cfg->nr; - return auth_imsi(&req, *imsi, cause); + return auth_imsi(req, *imsi, cause); } int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, - struct bsc_connection *bsc, + struct bsc_filter_request *req, struct bsc_filter_state *state, struct bsc_filter_reject_cause *cause) { - struct filter_request req; uint8_t msg_type, proto; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; @@ -411,11 +396,6 @@ int bsc_msg_filter_data(struct gsm48_hdr *hdr48, size_t len, if (proto != GSM48_PDISC_MM || msg_type != GSM48_MT_MM_ID_RESP) return 0; - req.black_list = &bsc->nat->imsi_black_list; - req.access_lists = &bsc->nat->access_lists; - req.local_lst_name = bsc->cfg->acc_lst_name; - req.global_lst_name = bsc->nat->acc_lst_name; - req.bsc_nr = bsc->cfg->nr; - return _dt_check_id_resp(bsc, &req, &hdr48->data[0], + return _dt_check_id_resp(req, &hdr48->data[0], len - sizeof(*hdr48), state, cause); } diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c index af0f7a189..6883d6692 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c @@ -35,6 +35,7 @@ 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 bsc_filter_request req; struct tlv_parsed tp; struct gsm48_hdr *hdr48; int hdr48_len; @@ -77,7 +78,13 @@ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, } 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); + req.ctx = bsc; + req.black_list = &bsc->nat->imsi_black_list; + req.access_lists = &bsc->nat->access_lists; + req.local_lst_name = bsc->cfg->acc_lst_name; + req.global_lst_name = bsc->nat->acc_lst_name; + req.bsc_nr = bsc->cfg->nr; + return bsc_msg_filter_initial(hdr48, hdr48_len, &req, con_type, imsi, cause); } int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, @@ -86,6 +93,7 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, { uint32_t len; struct gsm48_hdr *hdr48; + struct bsc_filter_request req; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; @@ -101,5 +109,11 @@ int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, if (!hdr48) return -1; - return bsc_msg_filter_data(hdr48, len, bsc, &con->filter_state, cause); + req.ctx = bsc; + req.black_list = &bsc->nat->imsi_black_list; + req.access_lists = &bsc->nat->access_lists; + req.local_lst_name = bsc->cfg->acc_lst_name; + req.global_lst_name = bsc->nat->acc_lst_name; + req.bsc_nr = bsc->cfg->nr; + return bsc_msg_filter_data(hdr48, len, &req, &con->filter_state, cause); } -- cgit v1.2.3 From 06a88fa0ae1f86cef0a23a1462bfef950418ea36 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 19:34:12 +0200 Subject: filter: Move from DNAT to DFILTER category --- openbsc/include/openbsc/debug.h | 1 + openbsc/src/libcommon/debug.c | 5 +++++ openbsc/src/libfilter/bsc_msg_filter.c | 24 ++++++++++++------------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/openbsc/include/openbsc/debug.h b/openbsc/include/openbsc/debug.h index bbb3ee618..19d8fc2de 100644 --- a/openbsc/include/openbsc/debug.h +++ b/openbsc/include/openbsc/debug.h @@ -32,6 +32,7 @@ enum { DNAT, DCTRL, DSMPP, + DFILTER, Debug_LastEntry, }; diff --git a/openbsc/src/libcommon/debug.c b/openbsc/src/libcommon/debug.c index ca7ff5da6..7fb3ecb67 100644 --- a/openbsc/src/libcommon/debug.c +++ b/openbsc/src/libcommon/debug.c @@ -160,6 +160,11 @@ static const struct log_info_cat default_categories[] = { .description = "SMPP interface for external SMS apps", .enabled = 1, .loglevel = LOGL_DEBUG, }, + [DFILTER] = { + .name = "DFILTER", + .description = "BSC/NAT IMSI based filtering", + .enabled = 1, .loglevel = LOGL_DEBUG, + }, }; enum log_filter { diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 159f6ccec..c3d1bed9d 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -75,7 +75,7 @@ static int insert_barr_node(struct bsc_filter_barr_entry *entry, struct rb_root else if (rc > 0) new = &((*new)->rb_right); else { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "Duplicate entry for IMSI(%s)\n", entry->imsi); talloc_free(entry); return -1; @@ -108,7 +108,7 @@ int bsc_filter_barr_adapt(void *ctx, struct rb_root *root, struct bsc_filter_barr_entry *entry; entry = talloc_zero(ctx, struct bsc_filter_barr_entry); if (!entry) { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "Allocation of the barr entry failed.\n"); continue; } @@ -162,7 +162,7 @@ static int auth_imsi(struct bsc_filter_request *req, if (bsc_filter_barr_find(req->black_list, imsi, &cm, &lu)) { cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; - LOGP(DNAT, LOGL_DEBUG, + LOGP(DFILTER, LOGL_DEBUG, "Blocking subscriber IMSI %s with CM: %d LU: %d\n", imsi, cm, lu); return -4; @@ -180,7 +180,7 @@ static int auth_imsi(struct bsc_filter_request *req, /* 3. BSC deny */ if (lst_check_deny(bsc_lst, imsi, &cm, &lu) == 0) { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "Filtering %s by imsi_deny on config nr: %d.\n", imsi, req->bsc_nr); rate_ctr_inc(&bsc_lst->stats->ctr[ACC_LIST_LOCAL_FILTER]); cause->cm_reject_cause = cm; @@ -193,7 +193,7 @@ static int auth_imsi(struct bsc_filter_request *req, /* 4. NAT deny */ if (nat_lst) { if (lst_check_deny(nat_lst, imsi, &cm, &lu) == 0) { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "Filtering %s global imsi_deny on bsc nr: %d.\n", imsi, req->bsc_nr); rate_ctr_inc(&nat_lst->stats->ctr[ACC_LIST_GLOBAL_FILTER]); cause->cm_reject_cause = cm; @@ -214,7 +214,7 @@ static int _cr_check_loc_upd(void *ctx, char mi_string[GSM48_MI_SIZE]; if (length < sizeof(*lu)) { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "LU does not fit. Length is %d \n", length); return -1; } @@ -249,7 +249,7 @@ static int _cr_check_cm_serv_req(void *ctx, /* unfortunately in Phase1 the classmark2 length is variable */ if (length < sizeof(*req)) { - LOGP(DNAT, LOGL_ERROR, + LOGP(DFILTER, LOGL_ERROR, "CM Serv Req does not fit. Length is %d\n", length); return -1; } @@ -260,7 +260,7 @@ static int _cr_check_cm_serv_req(void *ctx, rc = gsm48_extract_mi((uint8_t *) &req->classmark, length - classmark_offset, mi_string, &mi_type); if (rc < 0) { - LOGP(DNAT, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); + LOGP(DFILTER, LOGL_ERROR, "Failed to parse the classmark2/mi. error: %d\n", rc); return -1; } @@ -280,13 +280,13 @@ static int _cr_check_pag_resp(void *ctx, uint8_t mi_type; if (length < sizeof(*resp)) { - LOGP(DNAT, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); + LOGP(DFILTER, LOGL_ERROR, "PAG RESP does not fit. Length was %d.\n", length); return -1; } resp = (struct gsm48_pag_resp *) data; if (gsm48_paging_extract_mi(resp, length, mi_string, &mi_type) < 0) { - LOGP(DNAT, LOGL_ERROR, "Failed to extract the MI.\n"); + LOGP(DFILTER, LOGL_ERROR, "Failed to extract the MI.\n"); return -1; } @@ -307,12 +307,12 @@ static int _dt_check_id_resp(struct bsc_filter_request *req, uint8_t mi_type; if (length < 2) { - LOGP(DNAT, LOGL_ERROR, "mi does not fit.\n"); + LOGP(DFILTER, LOGL_ERROR, "mi does not fit.\n"); return -1; } if (data[0] < length - 1) { - LOGP(DNAT, LOGL_ERROR, "mi length too big.\n"); + LOGP(DFILTER, LOGL_ERROR, "mi length too big.\n"); return -2; } -- cgit v1.2.3 From c652913674ecc30f8d234878a17baa623cbacf99 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 21:03:49 +0200 Subject: filter: Move the con_type into the filter_state --- openbsc/include/openbsc/bsc_msg_filter.h | 12 ++++++++++++ openbsc/include/openbsc/bsc_nat.h | 10 ---------- openbsc/include/openbsc/bsc_nat_sccp.h | 1 - openbsc/src/libfilter/bsc_msg_filter.c | 12 ++++++------ openbsc/src/osmo-bsc/osmo_bsc_api.c | 1 + openbsc/src/osmo-bsc_nat/bsc_nat.c | 12 ++++++------ openbsc/src/osmo-bsc_nat/bsc_nat_filter.c | 2 +- openbsc/src/osmo-bsc_nat/bsc_nat_utils.c | 30 +++++++++++++++--------------- openbsc/src/osmo-bsc_nat/bsc_nat_vty.c | 2 +- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 2 +- openbsc/tests/bsc-nat/bsc_nat_test.c | 22 +++++++++++----------- 11 files changed, 54 insertions(+), 52 deletions(-) diff --git a/openbsc/include/openbsc/bsc_msg_filter.h b/openbsc/include/openbsc/bsc_msg_filter.h index b09cd8aa6..a9dedf43c 100644 --- a/openbsc/include/openbsc/bsc_msg_filter.h +++ b/openbsc/include/openbsc/bsc_msg_filter.h @@ -53,9 +53,21 @@ struct bsc_msg_acc_lst_entry { int lu_reject_cause; }; +enum { + FLT_CON_TYPE_NONE, + FLT_CON_TYPE_LU, + FLT_CON_TYPE_CM_SERV_REQ, + FLT_CON_TYPE_PAG_RESP, + FLT_CON_TYPE_SSA, + FLT_CON_TYPE_LOCAL_REJECT, + FLT_CON_TYPE_OTHER, +}; + + struct bsc_filter_state { char *imsi; int imsi_checked; + int con_type; }; struct bsc_filter_request { diff --git a/openbsc/include/openbsc/bsc_nat.h b/openbsc/include/openbsc/bsc_nat.h index 10f11a62b..ae940b390 100644 --- a/openbsc/include/openbsc/bsc_nat.h +++ b/openbsc/include/openbsc/bsc_nat.h @@ -48,16 +48,6 @@ struct bsc_nat; struct bsc_nat_ussd_con; struct nat_rewrite_rule; -enum { - NAT_CON_TYPE_NONE, - NAT_CON_TYPE_LU, - NAT_CON_TYPE_CM_SERV_REQ, - NAT_CON_TYPE_PAG_RESP, - NAT_CON_TYPE_SSA, - NAT_CON_TYPE_LOCAL_REJECT, - NAT_CON_TYPE_OTHER, -}; - /* * Is this terminated to the MSC, to the local machine (release * handling for IMSI filtering) or to a USSD provider? diff --git a/openbsc/include/openbsc/bsc_nat_sccp.h b/openbsc/include/openbsc/bsc_nat_sccp.h index d2490ca6a..082466408 100644 --- a/openbsc/include/openbsc/bsc_nat_sccp.h +++ b/openbsc/include/openbsc/bsc_nat_sccp.h @@ -79,7 +79,6 @@ struct nat_sccp_connection { int has_remote_ref; /* status */ - int con_type; int con_local; int authorized; diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index c3d1bed9d..2bbfa078e 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -256,7 +256,7 @@ static int _cr_check_cm_serv_req(void *ctx, req = (struct gsm48_service_request *) data; if (req->cm_service_type == 0x8) - *con_type = NAT_CON_TYPE_SSA; + *con_type = FLT_CON_TYPE_SSA; rc = gsm48_extract_mi((uint8_t *) &req->classmark, length - classmark_offset, mi_string, &mi_type); if (rc < 0) { @@ -337,7 +337,7 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, int ret = 0; uint8_t msg_type, proto; - *con_type = NAT_CON_TYPE_NONE; + *con_type = FLT_CON_TYPE_NONE; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; *imsi = NULL; @@ -346,23 +346,23 @@ int bsc_msg_filter_initial(struct gsm48_hdr *hdr48, size_t hdr48_len, msg_type = hdr48->msg_type & 0xbf; if (proto == GSM48_PDISC_MM && msg_type == GSM48_MT_MM_LOC_UPD_REQUEST) { - *con_type = NAT_CON_TYPE_LU; + *con_type = FLT_CON_TYPE_LU; ret = _cr_check_loc_upd(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); } else if (proto == GSM48_PDISC_MM && msg_type == GSM48_MT_MM_CM_SERV_REQ) { - *con_type = NAT_CON_TYPE_CM_SERV_REQ; + *con_type = FLT_CON_TYPE_CM_SERV_REQ; ret = _cr_check_cm_serv_req(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), con_type, imsi); } else if (proto == GSM48_PDISC_RR && msg_type == GSM48_MT_RR_PAG_RESP) { - *con_type = NAT_CON_TYPE_PAG_RESP; + *con_type = FLT_CON_TYPE_PAG_RESP; ret = _cr_check_pag_resp(req->ctx, &hdr48->data[0], hdr48_len - sizeof(*hdr48), imsi); } else { /* We only want to filter the above, let other things pass */ - *con_type = NAT_CON_TYPE_OTHER; + *con_type = FLT_CON_TYPE_OTHER; return 0; } diff --git a/openbsc/src/osmo-bsc/osmo_bsc_api.c b/openbsc/src/osmo-bsc/osmo_bsc_api.c index 18b9607a0..a90ad1689 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_api.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_api.c @@ -210,6 +210,7 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, ci = get_ci_for_msc(conn->sccp_con->msc, conn->bts); bsc_scan_bts_msg(conn, msg); + resp = gsm0808_create_layer3(msg, network_code, country_code, lac, ci); if (!resp) { LOGP(DMSC, LOGL_DEBUG, "Failed to create layer3 message.\n"); diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c index 3ec43b15f..4357485ff 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c @@ -442,7 +442,7 @@ static void bsc_send_con_release(struct bsc_connection *bsc, con->msc_con = NULL; /* 2. release the BSC side */ - if (con->con_type == NAT_CON_TYPE_LU) { + if (con->filter_state.con_type == FLT_CON_TYPE_LU) { struct msgb *payload, *udt; payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); @@ -470,7 +470,7 @@ static void bsc_send_con_release(struct bsc_connection *bsc, return; } - con->con_type = NAT_CON_TYPE_LOCAL_REJECT; + con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT; bsc_write(bsc, rlsd, IPAC_PROTO_SCCP); } @@ -481,9 +481,9 @@ static void bsc_send_con_refuse(struct bsc_connection *bsc, struct msgb *payload; struct msgb *refuse; - if (con_type == NAT_CON_TYPE_LU) + if (con_type == FLT_CON_TYPE_LU) payload = gsm48_create_loc_upd_rej(cause->lu_reject_cause); - else if (con_type == NAT_CON_TYPE_CM_SERV_REQ || con_type == NAT_CON_TYPE_SSA) + else if (con_type == FLT_CON_TYPE_CM_SERV_REQ || con_type == FLT_CON_TYPE_SSA) payload = gsm48_create_mm_serv_rej(cause->cm_reject_cause); else { LOGP(DNAT, LOGL_ERROR, "Unknown connection type: %d\n", con_type); @@ -504,7 +504,7 @@ static void bsc_send_con_refuse(struct bsc_connection *bsc, goto send_refuse; /* declare it local and assign a unique remote_ref */ - con->con_type = NAT_CON_TYPE_LOCAL_REJECT; + con->filter_state.con_type = FLT_CON_TYPE_LOCAL_REJECT; con->con_local = NAT_CON_END_LOCAL; con->has_remote_ref = 1; con->remote_ref = con->patched_ref; @@ -1072,7 +1072,7 @@ static int forward_sccp_to_msc(struct bsc_connection *bsc, struct msgb *msg) con = patch_sccp_src_ref_to_msc(msg, parsed, bsc); con->msc_con = bsc->nat->msc_con; con_msc = con->msc_con; - con->con_type = con_type; + con->filter_state.con_type = con_type; con->filter_state.imsi_checked = filter; bsc_nat_extract_lac(bsc, con, parsed, msg); if (imsi) diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c index 6883d6692..393aea3ce 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_filter.c @@ -41,7 +41,7 @@ int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, int hdr48_len; int len; - *con_type = NAT_CON_TYPE_NONE; + *con_type = FLT_CON_TYPE_NONE; cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; *imsi = NULL; diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c index 89212087c..d95227dca 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_utils.c @@ -403,13 +403,13 @@ struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, } static const char *con_types [] = { - [NAT_CON_TYPE_NONE] = "n/a", - [NAT_CON_TYPE_LU] = "Location Update", - [NAT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", - [NAT_CON_TYPE_PAG_RESP] = "Paging Response", - [NAT_CON_TYPE_SSA] = "Supplementar Service Activation", - [NAT_CON_TYPE_LOCAL_REJECT] = "Local Reject", - [NAT_CON_TYPE_OTHER] = "Other", + [FLT_CON_TYPE_NONE] = "n/a", + [FLT_CON_TYPE_LU] = "Location Update", + [FLT_CON_TYPE_CM_SERV_REQ] = "CM Serv Req", + [FLT_CON_TYPE_PAG_RESP] = "Paging Response", + [FLT_CON_TYPE_SSA] = "Supplementar Service Activation", + [FLT_CON_TYPE_LOCAL_REJECT] = "Local Reject", + [FLT_CON_TYPE_OTHER] = "Other", }; const char *bsc_con_type_to_string(int type) @@ -423,18 +423,18 @@ int bsc_nat_msc_is_connected(struct bsc_nat *nat) } static const int con_to_ctr[] = { - [NAT_CON_TYPE_NONE] = -1, - [NAT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, - [NAT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, - [NAT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, - [NAT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, - [NAT_CON_TYPE_LOCAL_REJECT] = -1, - [NAT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, + [FLT_CON_TYPE_NONE] = -1, + [FLT_CON_TYPE_LU] = BCFG_CTR_CON_TYPE_LU, + [FLT_CON_TYPE_CM_SERV_REQ] = BCFG_CTR_CON_CMSERV_RQ, + [FLT_CON_TYPE_PAG_RESP] = BCFG_CTR_CON_PAG_RESP, + [FLT_CON_TYPE_SSA] = BCFG_CTR_CON_SSA, + [FLT_CON_TYPE_LOCAL_REJECT] = -1, + [FLT_CON_TYPE_OTHER] = BCFG_CTR_CON_OTHER, }; int bsc_conn_type_to_ctr(struct nat_sccp_connection *conn) { - return con_to_ctr[conn->con_type]; + return con_to_ctr[conn->filter_state.con_type]; } int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg) diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c index 596ecaee4..821e5226a 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c +++ b/openbsc/src/osmo-bsc_nat/bsc_nat_vty.c @@ -189,7 +189,7 @@ DEFUN(show_sccp, show_sccp_cmd, "show sccp connections", con->has_remote_ref, sccp_src_ref_to_int(&con->remote_ref), con->msc_endp, con->bsc_endp, - bsc_con_type_to_string(con->con_type), + bsc_con_type_to_string(con->filter_state.con_type), VTY_NEWLINE); } diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index 224189608..108241421 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -382,7 +382,7 @@ int bsc_ussd_check(struct nat_sccp_connection *con, struct bsc_nat_parsed *parse * decode if the connection was created for USSD, we do have a USSD access * list, a query, a IMSI and such... */ - if (con->con_type != NAT_CON_TYPE_SSA) + if (con->filter_state.con_type != FLT_CON_TYPE_SSA) return 0; if (!con->filter_state.imsi) diff --git a/openbsc/tests/bsc-nat/bsc_nat_test.c b/openbsc/tests/bsc-nat/bsc_nat_test.c index e883ddd43..b830eb0df 100644 --- a/openbsc/tests/bsc-nat/bsc_nat_test.c +++ b/openbsc/tests/bsc-nat/bsc_nat_test.c @@ -711,7 +711,7 @@ static struct cr_filter cr_filter[] = { .data = bssmap_cr, .length = sizeof(bssmap_cr), .result = 1, - .contype = NAT_CON_TYPE_CM_SERV_REQ, + .contype = FLT_CON_TYPE_CM_SERV_REQ, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -723,7 +723,7 @@ static struct cr_filter cr_filter[] = { .data = bss_lu, .length = sizeof(bss_lu), .result = 1, - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -735,7 +735,7 @@ static struct cr_filter cr_filter[] = { .data = pag_resp, .length = sizeof(pag_resp), .result = 1, - .contype = NAT_CON_TYPE_PAG_RESP, + .contype = FLT_CON_TYPE_PAG_RESP, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -749,7 +749,7 @@ static struct cr_filter cr_filter[] = { .length = sizeof(bss_lu), .result = -3, .nat_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -764,7 +764,7 @@ static struct cr_filter cr_filter[] = { .result = 1, .nat_imsi_deny = "[0-9]*", .bsc_imsi_allow = "2440[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -779,7 +779,7 @@ static struct cr_filter cr_filter[] = { .result = 1, .bsc_imsi_allow = "[0-9]*", .nat_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -795,7 +795,7 @@ static struct cr_filter cr_filter[] = { .bsc_imsi_deny = "[0-9]*", .bsc_imsi_allow = "[0-9]*", .nat_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -810,7 +810,7 @@ static struct cr_filter cr_filter[] = { .result = -3, .bsc_imsi_deny = "000[0-9]*", .nat_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -825,7 +825,7 @@ static struct cr_filter cr_filter[] = { .result = -3, .bsc_imsi_deny = "000[0-9]*", .nat_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = 0x23, .nat_lu_reject_cause = 0x42, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -839,7 +839,7 @@ static struct cr_filter cr_filter[] = { .length = sizeof(bss_lu), .result = -2, .bsc_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, @@ -853,7 +853,7 @@ static struct cr_filter cr_filter[] = { .length = sizeof(bss_lu), .result = -2, .bsc_imsi_deny = "[0-9]*", - .contype = NAT_CON_TYPE_LU, + .contype = FLT_CON_TYPE_LU, .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, .bsc_cm_reject_cause = 0x42, -- cgit v1.2.3 From d6332809d8313903c65ccd28646f41b7c1aa6b99 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 19:46:20 +0200 Subject: bsc: Add access lists to the MSC and the BSC It is a bit arbitary to decide which one is the global and which one is the local one. We might change it around. I don't think we want to introduce it based on BTS. --- openbsc/doc/examples/osmo-bsc/osmo-bsc.cfg | 5 +++ openbsc/include/openbsc/osmo_bsc.h | 3 ++ openbsc/include/openbsc/osmo_msc_data.h | 4 ++ openbsc/src/osmo-bsc/Makefile.am | 4 +- openbsc/src/osmo-bsc/osmo_bsc_main.c | 4 ++ openbsc/src/osmo-bsc/osmo_bsc_vty.c | 66 +++++++++++++++++++++++++++++- 6 files changed, 84 insertions(+), 2 deletions(-) diff --git a/openbsc/doc/examples/osmo-bsc/osmo-bsc.cfg b/openbsc/doc/examples/osmo-bsc/osmo-bsc.cfg index d7461964f..fd4c3d726 100644 --- a/openbsc/doc/examples/osmo-bsc/osmo-bsc.cfg +++ b/openbsc/doc/examples/osmo-bsc/osmo-bsc.cfg @@ -98,3 +98,8 @@ msc timeout-ping 20 timeout-pong 5 dest 192.168.100.11 6666 0 + access-list-name msc-list + no access-list-name +bsc + no access-list-name + access-list-name bsc-list diff --git a/openbsc/include/openbsc/osmo_bsc.h b/openbsc/include/openbsc/osmo_bsc.h index 19b879f56..7df16cc92 100644 --- a/openbsc/include/openbsc/osmo_bsc.h +++ b/openbsc/include/openbsc/osmo_bsc.h @@ -4,6 +4,7 @@ #define OSMO_BSC_H #include "bsc_api.h" +#include "bsc_msg_filter.h" #define BSS_SEND_USSD 1 @@ -41,6 +42,8 @@ struct osmo_bsc_sccp_con { struct gsm_subscriber_connection *conn; uint8_t new_subscriber; + + struct bsc_filter_state filter_state; }; struct bsc_api *osmo_bsc_api(); diff --git a/openbsc/include/openbsc/osmo_msc_data.h b/openbsc/include/openbsc/osmo_msc_data.h index bdc762aaf..2d863aa32 100644 --- a/openbsc/include/openbsc/osmo_msc_data.h +++ b/openbsc/include/openbsc/osmo_msc_data.h @@ -92,6 +92,8 @@ struct osmo_msc_data { /* ussd text when MSC has entered the grace period */ char *ussd_grace_txt; + + char *acc_lst_name; }; /* @@ -112,6 +114,8 @@ struct osmo_bsc_data { /* ussd text when there is no MSC available */ char *ussd_no_msc_txt; + + char *acc_lst_name; }; diff --git a/openbsc/src/osmo-bsc/Makefile.am b/openbsc/src/osmo-bsc/Makefile.am index 6248fcd6f..b4a2cba64 100644 --- a/openbsc/src/osmo-bsc/Makefile.am +++ b/openbsc/src/osmo-bsc/Makefile.am @@ -9,7 +9,9 @@ osmo_bsc_SOURCES = osmo_bsc_main.c osmo_bsc_vty.c osmo_bsc_api.c \ osmo_bsc_grace.c osmo_bsc_msc.c osmo_bsc_sccp.c \ osmo_bsc_filter.c osmo_bsc_bssap.c osmo_bsc_audio.c osmo_bsc_ctrl.c # once again since TRAU uses CC symbol :( -osmo_bsc_LDADD = $(top_builddir)/src/libbsc/libbsc.a \ +osmo_bsc_LDADD = \ + $(top_builddir)/src/libfilter/libfilter.a \ + $(top_builddir)/src/libbsc/libbsc.a \ $(top_builddir)/src/libmsc/libmsc.a \ $(top_builddir)/src/libbsc/libbsc.a \ $(top_builddir)/src/libtrau/libtrau.a \ diff --git a/openbsc/src/osmo-bsc/osmo_bsc_main.c b/openbsc/src/osmo-bsc/osmo_bsc_main.c index 5c3885575..77c9bf92b 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_main.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_main.c @@ -179,6 +179,7 @@ static void signal_handler(int signal) int main(int argc, char **argv) { + struct llist_head access_lists; struct osmo_msc_data *msc; struct osmo_bsc_data *data; int rc; @@ -196,6 +197,9 @@ int main(int argc, char **argv) vty_info.copyright = openbsc_copyright; vty_init(&vty_info); bsc_vty_init(&log_info); + bsc_msg_lst_vty_init(tall_bsc_ctx, &access_lists, BSC_NODE); + + INIT_LLIST_HEAD(&access_lists); /* parse options */ handle_options(argc, argv); diff --git a/openbsc/src/osmo-bsc/osmo_bsc_vty.c b/openbsc/src/osmo-bsc/osmo_bsc_vty.c index bbbba1cd2..06ad77d59 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_vty.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_vty.c @@ -1,5 +1,5 @@ /* Osmo BSC VTY Configuration */ -/* (C) 2009-2014 by Holger Hans Peter Freyther +/* (C) 2009-2015 by Holger Hans Peter Freyther * (C) 2009-2014 by On-Waves * All Rights Reserved * @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -175,6 +176,9 @@ static void write_msc(struct vty *vty, struct osmo_msc_data *msc) if (msc->local_pref) vty_out(vty, " local-prefix %s%s", msc->local_pref, VTY_NEWLINE); + if (msc->acc_lst_name) + vty_out(vty, " access-list-name %s%s", msc->acc_lst_name, VTY_NEWLINE); + /* write amr options */ write_msc_amr_options(vty, msc); } @@ -210,6 +214,8 @@ static int config_write_bsc(struct vty *vty) vty_out(vty, " missing-msc-text %s%s", bsc->ussd_no_msc_txt, VTY_NEWLINE); else vty_out(vty, " no missing-msc-text%s", VTY_NEWLINE); + if (bsc->acc_lst_name) + vty_out(vty, " access-list-name %s%s", bsc->acc_lst_name, VTY_NEWLINE); return CMD_SUCCESS; } @@ -625,6 +631,33 @@ AMR_COMMAND(5_90) AMR_COMMAND(5_15) AMR_COMMAND(4_75) +DEFUN(cfg_msc_acc_lst_name, + cfg_msc_acc_lst_name_cmd, + "access-list-name NAME", + "Set the name of the access list to use.\n" + "The name of the to be used access list.") +{ + struct osmo_msc_data *msc = osmo_msc_data(vty); + + bsc_replace_string(msc, &msc->acc_lst_name, argv[0]); + return CMD_SUCCESS; +} + +DEFUN(cfg_msc_no_acc_lst_name, + cfg_msc_no_acc_lst_name_cmd, + "no access-list-name", + NO_STR "Remove the access list from the NAT.\n") +{ + struct osmo_msc_data *msc = osmo_msc_data(vty); + + if (msc->acc_lst_name) { + talloc_free(msc->acc_lst_name); + msc->acc_lst_name = NULL; + } + + return CMD_SUCCESS; +} + DEFUN(cfg_net_bsc_mid_call_text, cfg_net_bsc_mid_call_text_cmd, "mid-call-text .TEXT", @@ -681,6 +714,33 @@ DEFUN(cfg_net_no_rf_off_time, return CMD_SUCCESS; } +DEFUN(cfg_bsc_acc_lst_name, + cfg_bsc_acc_lst_name_cmd, + "access-list-name NAME", + "Set the name of the access list to use.\n" + "The name of the to be used access list.") +{ + struct osmo_bsc_data *bsc = osmo_bsc_data(vty); + + bsc_replace_string(bsc, &bsc->acc_lst_name, argv[0]); + return CMD_SUCCESS; +} + +DEFUN(cfg_bsc_no_acc_lst_name, + cfg_bsc_no_acc_lst_name_cmd, + "no access-list-name", + NO_STR "Remove the access list from the BSC\n") +{ + struct osmo_bsc_data *bsc = osmo_bsc_data(vty); + + if (bsc->acc_lst_name) { + talloc_free(bsc->acc_lst_name); + bsc->acc_lst_name = NULL; + } + + return CMD_SUCCESS; +} + DEFUN(show_statistics, show_statistics_cmd, "show statistics", @@ -805,6 +865,8 @@ int bsc_vty_init_extra(void) install_element(BSC_NODE, &cfg_net_no_rf_off_time_cmd); install_element(BSC_NODE, &cfg_net_bsc_missing_msc_ussd_cmd); install_element(BSC_NODE, &cfg_net_bsc_no_missing_msc_text_cmd); + install_element(BSC_NODE, &cfg_bsc_acc_lst_name_cmd); + install_element(BSC_NODE, &cfg_bsc_no_acc_lst_name_cmd); install_node(&msc_node, config_write_msc); vty_install_default(MSC_NODE); @@ -839,6 +901,8 @@ int bsc_vty_init_extra(void) install_element(MSC_NODE, &cfg_net_msc_amr_5_90_cmd); install_element(MSC_NODE, &cfg_net_msc_amr_5_15_cmd); install_element(MSC_NODE, &cfg_net_msc_amr_4_75_cmd); + install_element(MSC_NODE, &cfg_msc_acc_lst_name_cmd); + install_element(MSC_NODE, &cfg_msc_no_acc_lst_name_cmd); install_element_ve(&show_statistics_cmd); install_element_ve(&show_mscs_cmd); -- cgit v1.2.3 From ec0cb7c64d5d42e8d0d599b209a3eabacfceba60 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 20:53:42 +0200 Subject: bsc: Add access list filtering to the BSC --- openbsc/include/openbsc/osmo_bsc.h | 2 ++ openbsc/src/libfilter/bsc_msg_filter.c | 2 +- openbsc/src/osmo-bsc/osmo_bsc_api.c | 56 ++++++++++++++++++++++++++++++++-- openbsc/src/osmo-bsc/osmo_bsc_main.c | 7 ++++- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/openbsc/include/openbsc/osmo_bsc.h b/openbsc/include/openbsc/osmo_bsc.h index 7df16cc92..fd5303dc6 100644 --- a/openbsc/include/openbsc/osmo_bsc.h +++ b/openbsc/include/openbsc/osmo_bsc.h @@ -66,4 +66,6 @@ int bsc_ctrl_cmds_install(); void bsc_gen_location_state_trap(struct gsm_bts *bts); +struct llist_head *bsc_access_lists(void); + #endif diff --git a/openbsc/src/libfilter/bsc_msg_filter.c b/openbsc/src/libfilter/bsc_msg_filter.c index 2bbfa078e..be518a478 100644 --- a/openbsc/src/libfilter/bsc_msg_filter.c +++ b/openbsc/src/libfilter/bsc_msg_filter.c @@ -159,7 +159,7 @@ static int auth_imsi(struct bsc_filter_request *req, struct bsc_msg_acc_lst *bsc_lst = NULL; /* 1. global check for barred imsis */ - if (bsc_filter_barr_find(req->black_list, imsi, &cm, &lu)) { + if (req->black_list && bsc_filter_barr_find(req->black_list, imsi, &cm, &lu)) { cause->cm_reject_cause = cm; cause->lu_reject_cause = lu; LOGP(DFILTER, LOGL_DEBUG, diff --git a/openbsc/src/osmo-bsc/osmo_bsc_api.c b/openbsc/src/osmo-bsc/osmo_bsc_api.c index a90ad1689..17026af53 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_api.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_api.c @@ -1,4 +1,4 @@ -/* (C) 2009-2011 by Holger Hans Peter Freyther +/* (C) 2009-2015 by Holger Hans Peter Freyther * (C) 2009-2011 by On-Waves * All Rights Reserved * @@ -79,6 +79,45 @@ static uint16_t get_ci_for_msc(struct osmo_msc_data *msc, struct gsm_bts *bts) return bts->cell_identity; } +static int bsc_filter_initial(struct osmo_bsc_data *bsc, + struct osmo_msc_data *msc, + struct gsm_subscriber_connection *conn, + struct msgb *msg, char **imsi) +{ + int con_type; + struct bsc_filter_request req; + struct bsc_filter_reject_cause cause; + struct gsm48_hdr *gh = msgb_l3(msg); + + req.ctx = conn; + req.black_list = NULL; + req.access_lists = bsc_access_lists(); + req.local_lst_name = msc->acc_lst_name; + req.global_lst_name = conn->bts->network->bsc_data->acc_lst_name; + req.bsc_nr = 0; + + return bsc_msg_filter_initial(gh, msgb_l3len(msg), &req, + &con_type, imsi, &cause); +} + +static int bsc_filter_data(struct gsm_subscriber_connection *conn, + struct msgb *msg) +{ + struct bsc_filter_request req; + struct gsm48_hdr *gh = msgb_l3(msg); + struct bsc_filter_reject_cause cause; + + req.ctx = conn; + req.black_list = NULL; + req.access_lists = bsc_access_lists(); + req.local_lst_name = conn->sccp_con->msc->acc_lst_name; + req.global_lst_name = conn->bts->network->bsc_data->acc_lst_name; + req.bsc_nr = 0; + + return bsc_msg_filter_data(gh, msgb_l3len(msg), &req, + &conn->sccp_con->filter_state, + &cause); +} static void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci) { @@ -172,6 +211,7 @@ static int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg static int complete_layer3(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_msc_data *msc) { + char *imsi = NULL; struct timeval tv; struct msgb *resp; uint16_t network_code; @@ -189,6 +229,10 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, if (send_ping && osmo_timer_remaining(&msc->ping_timer, NULL, &tv) == -1) send_ping = 0; + /* Check the filter */ + if (bsc_filter_initial(msc->network->bsc_data, msc, conn, msg, &imsi) < 0) + return BSC_API_CONN_POL_REJECT; + /* allocate resource for a new connection */ ret = bsc_create_new_connection(conn, msc, send_ping); @@ -202,6 +246,9 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, return BSC_API_CONN_POL_REJECT; } + if (imsi) + conn->sccp_con->filter_state.imsi = talloc_steal(conn, imsi); + /* check return value, if failed check msg for and send USSD */ network_code = get_network_code_for_msc(conn->sccp_con->msc); @@ -333,8 +380,13 @@ static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, st if (handle_cc_setup(conn, msg) >= 1) return; - bsc_scan_bts_msg(conn, msg); + /* Check the filter */ + if (bsc_filter_data(conn, msg) < 0) { + bsc_clear_request(conn, 0); + return; + } + bsc_scan_bts_msg(conn, msg); resp = gsm0808_create_dtap(msg, link_id); queue_msg_or_return(resp); diff --git a/openbsc/src/osmo-bsc/osmo_bsc_main.c b/openbsc/src/osmo-bsc/osmo_bsc_main.c index 77c9bf92b..ee86cb647 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_main.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_main.c @@ -59,6 +59,12 @@ static const char *config_file = "openbsc.cfg"; static const char *rf_ctrl = NULL; extern const char *openbsc_copyright; static int daemonize = 0; +static struct llist_head access_lists; + +struct llist_head *bsc_access_lists(void) +{ + return &access_lists; +} static void print_usage() { @@ -179,7 +185,6 @@ static void signal_handler(int signal) int main(int argc, char **argv) { - struct llist_head access_lists; struct osmo_msc_data *msc; struct osmo_bsc_data *data; int rc; -- cgit v1.2.3 From d26b8fcbe22eb4a56c87f0114a0e826551243511 Mon Sep 17 00:00:00 2001 From: Holger Hans Peter Freyther Date: Sun, 5 Apr 2015 22:45:32 +0200 Subject: bsc: Send a LU Reject in case it has been filtered In case we filter the request and it was a Location Updating Procedure we should reject it. --- openbsc/src/osmo-bsc/osmo_bsc_api.c | 50 +++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/openbsc/src/osmo-bsc/osmo_bsc_api.c b/openbsc/src/osmo-bsc/osmo_bsc_api.c index 17026af53..5a01d6b82 100644 --- a/openbsc/src/osmo-bsc/osmo_bsc_api.c +++ b/openbsc/src/osmo-bsc/osmo_bsc_api.c @@ -79,15 +79,34 @@ static uint16_t get_ci_for_msc(struct osmo_msc_data *msc, struct gsm_bts *bts) return bts->cell_identity; } +static void bsc_maybe_lu_reject(struct gsm_subscriber_connection *conn, int con_type, int cause) +{ + struct msgb *msg; + + /* ignore cm service request or such */ + if (con_type != FLT_CON_TYPE_LU) + return; + + msg = gsm48_create_loc_upd_rej(cause); + if (!msg) { + LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n"); + return; + } + + msg->lchan = conn->lchan; + gsm0808_submit_dtap(conn, msg, 0, 0); +} + static int bsc_filter_initial(struct osmo_bsc_data *bsc, struct osmo_msc_data *msc, struct gsm_subscriber_connection *conn, - struct msgb *msg, char **imsi) + struct msgb *msg, char **imsi, int *con_type, + int *lu_cause) { - int con_type; struct bsc_filter_request req; struct bsc_filter_reject_cause cause; struct gsm48_hdr *gh = msgb_l3(msg); + int rc; req.ctx = conn; req.black_list = NULL; @@ -96,16 +115,19 @@ static int bsc_filter_initial(struct osmo_bsc_data *bsc, req.global_lst_name = conn->bts->network->bsc_data->acc_lst_name; req.bsc_nr = 0; - return bsc_msg_filter_initial(gh, msgb_l3len(msg), &req, - &con_type, imsi, &cause); + rc = bsc_msg_filter_initial(gh, msgb_l3len(msg), &req, + con_type, imsi, &cause); + *lu_cause = cause.lu_reject_cause; + return rc; } static int bsc_filter_data(struct gsm_subscriber_connection *conn, - struct msgb *msg) + struct msgb *msg, int *lu_cause) { struct bsc_filter_request req; struct gsm48_hdr *gh = msgb_l3(msg); struct bsc_filter_reject_cause cause; + int rc; req.ctx = conn; req.black_list = NULL; @@ -114,9 +136,11 @@ static int bsc_filter_data(struct gsm_subscriber_connection *conn, req.global_lst_name = conn->bts->network->bsc_data->acc_lst_name; req.bsc_nr = 0; - return bsc_msg_filter_data(gh, msgb_l3len(msg), &req, + rc = bsc_msg_filter_data(gh, msgb_l3len(msg), &req, &conn->sccp_con->filter_state, &cause); + *lu_cause = cause.lu_reject_cause; + return rc; } static void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci) @@ -211,6 +235,7 @@ static int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg static int complete_layer3(struct gsm_subscriber_connection *conn, struct msgb *msg, struct osmo_msc_data *msc) { + int con_type, rc, lu_cause; char *imsi = NULL; struct timeval tv; struct msgb *resp; @@ -230,8 +255,12 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, send_ping = 0; /* Check the filter */ - if (bsc_filter_initial(msc->network->bsc_data, msc, conn, msg, &imsi) < 0) + rc = bsc_filter_initial(msc->network->bsc_data, msc, conn, msg, + &imsi, &con_type, &lu_cause); + if (rc < 0) { + bsc_maybe_lu_reject(conn, con_type, lu_cause); return BSC_API_CONN_POL_REJECT; + } /* allocate resource for a new connection */ ret = bsc_create_new_connection(conn, msc, send_ping); @@ -248,6 +277,7 @@ static int complete_layer3(struct gsm_subscriber_connection *conn, if (imsi) conn->sccp_con->filter_state.imsi = talloc_steal(conn, imsi); + conn->sccp_con->filter_state.con_type = con_type; /* check return value, if failed check msg for and send USSD */ @@ -368,6 +398,7 @@ static int handle_cc_setup(struct gsm_subscriber_connection *conn, static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) { + int lu_cause; struct msgb *resp; return_when_not_connected(conn); @@ -381,7 +412,10 @@ static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, st return; /* Check the filter */ - if (bsc_filter_data(conn, msg) < 0) { + if (bsc_filter_data(conn, msg, &lu_cause) < 0) { + bsc_maybe_lu_reject(conn, + conn->sccp_con->filter_state.con_type, + lu_cause); bsc_clear_request(conn, 0); return; } -- cgit v1.2.3