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 | { |
| 38 | struct tlv_parsed tp; |
| 39 | struct gsm48_hdr *hdr48; |
| 40 | int hdr48_len; |
| 41 | int len; |
| 42 | |
| 43 | *con_type = NAT_CON_TYPE_NONE; |
| 44 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 45 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 46 | *imsi = NULL; |
| 47 | |
| 48 | if (parsed->gsm_type != BSS_MAP_MSG_COMPLETE_LAYER_3) { |
| 49 | LOGP(DNAT, LOGL_ERROR, |
| 50 | "Rejecting CR message due wrong GSM Type %d\n", parsed->gsm_type); |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | /* the parsed has had some basic l3 length check */ |
| 55 | len = msg->l3h[1]; |
| 56 | if (msgb_l3len(msg) - 3 < len) { |
| 57 | LOGP(DNAT, LOGL_ERROR, |
| 58 | "The CR Data has not enough space...\n"); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | msg->l4h = &msg->l3h[3]; |
| 63 | len -= 1; |
| 64 | |
| 65 | tlv_parse(&tp, gsm0808_att_tlvdef(), msg->l4h, len, 0, 0); |
| 66 | |
| 67 | if (!TLVP_PRESENT(&tp, GSM0808_IE_LAYER_3_INFORMATION)) { |
| 68 | LOGP(DNAT, LOGL_ERROR, "CR Data does not contain layer3 information.\n"); |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | hdr48_len = TLVP_LEN(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 73 | |
| 74 | if (hdr48_len < sizeof(*hdr48)) { |
| 75 | LOGP(DNAT, LOGL_ERROR, "GSM48 header does not fit.\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | hdr48 = (struct gsm48_hdr *) TLVP_VAL(&tp, GSM0808_IE_LAYER_3_INFORMATION); |
| 80 | return bsc_msg_filter_initial(hdr48, hdr48_len, bsc, con_type, imsi, cause); |
| 81 | } |
| 82 | |
| 83 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 84 | struct nat_sccp_connection *con, struct bsc_nat_parsed *parsed, |
| 85 | struct bsc_filter_reject_cause *cause) |
| 86 | { |
| 87 | uint32_t len; |
| 88 | struct gsm48_hdr *hdr48; |
| 89 | |
| 90 | cause->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 91 | cause->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 92 | |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame^] | 93 | if (con->filter_state.imsi_checked) |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 94 | return 0; |
| 95 | |
| 96 | /* only care about DTAP messages */ |
| 97 | if (parsed->bssap != BSSAP_MSG_DTAP) |
| 98 | return 0; |
| 99 | |
| 100 | hdr48 = bsc_unpack_dtap(parsed, msg, &len); |
| 101 | if (!hdr48) |
| 102 | return -1; |
| 103 | |
Holger Hans Peter Freyther | c09f8a3 | 2015-04-05 19:13:27 +0200 | [diff] [blame^] | 104 | return bsc_msg_filter_data(hdr48, len, bsc, &con->filter_state, cause); |
Holger Hans Peter Freyther | 4ba947b | 2015-04-05 18:07:45 +0200 | [diff] [blame] | 105 | } |