Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010-2015 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010-2012 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <openbsc/bsc_nat.h> |
| 22 | #include <openbsc/bsc_nat_sccp.h> |
| 23 | #include <openbsc/bsc_msg_filter.h> |
| 24 | #include <openbsc/debug.h> |
| 25 | |
| 26 | #include <osmocom/gsm/gsm0808.h> |
| 27 | |
| 28 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 29 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
| 30 | |
| 31 | #include <osmocom/sccp/sccp.h> |
| 32 | |
| 33 | /* Filter out CR data... */ |
| 34 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 35 | struct bsc_nat_parsed *parsed, int *con_type, |
| 36 | char **imsi, struct bsc_filter_reject_cause *cause) |
| 37 | { |
Holger Hans Peter Freyther | 4e8176d | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 38 | struct bsc_filter_request req; |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 39 | struct tlv_parsed tp; |
| 40 | struct gsm48_hdr *hdr48; |
| 41 | int hdr48_len; |
| 42 | int len; |
| 43 | |
Holger Hans Peter Freyther | c652913 | 2015-04-05 21:03:49 +0200 | [diff] [blame] | 44 | *con_type = FLT_CON_TYPE_NONE; |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 45 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 46 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 47 | *imsi = NULL; |
| 48 | |
| 49 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 50 | LOGP(DNAT, LOGL_ERROR, |
| 51 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | /* the parsed has had some basic l3 length check */ |
| 56 | len = msg->l3h[1]; |
| 57 | if (msgb_l3len(msg) - 3 < len) { |
| 58 | LOGP(DNAT, LOGL_ERROR, |
| 59 | "The CR Data has not enough space...\n"); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | msg->l4h = &msg->l3h[3]; |
| 64 | len -= 1; |
| 65 | |
| 66 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 67 | |
| 68 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 69 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 74 | |
| 75 | if (hdr48_len < sizeof(*hdr48)) { |
| 76 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 77 | return -1; |
| 78 | } |
| 79 | |
| 80 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
Holger Hans Peter Freyther | 4e8176d | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 81 | req.ctx = bsc; |
| 82 | req.black_list = &bsc->nat->imsi_black_list; |
| 83 | req.access_lists = &bsc->nat->access_lists; |
| 84 | req.local_lst_name = bsc->cfg->acc_lst_name; |
| 85 | req.global_lst_name = bsc->nat->acc_lst_name; |
| 86 | req.bsc_nr = bsc->cfg->nr; |
| 87 | return bsc_msg_filter_initial(hdr48, hdr48_len, &req, con_type, imsi, cause); |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 91 | struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, |
| 92 | struct bsc_filter_reject_cause *cause) |
| 93 | { |
| 94 | uint32_t len; |
| 95 | struct gsm48_hdr *hdr48; |
Holger Hans Peter Freyther | 4e8176d | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 96 | struct bsc_filter_request req; |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 97 | |
| 98 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 99 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 100 | |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame] | 101 | if (con->filter_state.imsi_checked) |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 102 | return 0; |
| 103 | |
| 104 | /* only care about DTAP messages */ |
| 105 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 106 | return 0; |
| 107 | |
| 108 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 109 | if (!hdr48) |
| 110 | return -1; |
| 111 | |
Holger Hans Peter Freyther | 5b692d0 | 2016-05-23 17:58:03 +0200 | [diff] [blame] | 112 | req.ctx = con; |
Holger Hans Peter Freyther | 4e8176d | 2015-04-05 19:20:09 +0200 | [diff] [blame] | 113 | req.black_list = &bsc->nat->imsi_black_list; |
| 114 | req.access_lists = &bsc->nat->access_lists; |
| 115 | req.local_lst_name = bsc->cfg->acc_lst_name; |
| 116 | req.global_lst_name = bsc->nat->acc_lst_name; |
| 117 | req.bsc_nr = bsc->cfg->nr; |
| 118 | return bsc_msg_filter_data(hdr48, len, &req, &con->filter_state, cause); |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 119 | } |