Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010-2015 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010-2011 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_msg_filter.h> |
| 22 | #include <openbsc/bsc_nat.h> |
Neels Hofmeyr | 979cd26 | 2017-07-19 16:48:42 +0200 | [diff] [blame] | 23 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 26 | #include <osmocom/core/stats.h> |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 27 | |
| 28 | #include <string.h> |
| 29 | |
| 30 | static const struct rate_ctr_desc acc_list_ctr_description[] = { |
Holger Hans Peter Freyther | 14b2cd9 | 2015-04-05 16:50:34 +0200 | [diff] [blame] | 31 | [ACC_LIST_LOCAL_FILTER] = { "access-list.local-filter", "Rejected by rule for local"}, |
| 32 | [ACC_LIST_GLOBAL_FILTER]= { "access-list.global-filter", "Rejected by rule for global"}, |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | static const struct rate_ctr_group_desc bsc_cfg_acc_list_desc = { |
| 36 | .group_name_prefix = "nat.filter", |
| 37 | .group_description = "NAT Access-List Statistics", |
| 38 | .num_ctr = ARRAY_SIZE(acc_list_ctr_description), |
| 39 | .ctr_desc = acc_list_ctr_description, |
Jacob Erlbeck | 46caed8 | 2015-11-02 15:15:38 +0100 | [diff] [blame] | 40 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 44 | int bsc_msg_acc_lst_check_allow(struct bsc_msg_acc_lst *lst, const char *mi_string) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 45 | { |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 46 | struct bsc_msg_acc_lst_entry *entry; |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 47 | |
| 48 | llist_for_each_entry(entry, &lst->fltr_list, list) { |
| 49 | if (!entry->imsi_allow) |
| 50 | continue; |
| 51 | if (regexec(&entry->imsi_allow_re, mi_string, 0, NULL, 0) == 0) |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | return 1; |
| 56 | } |
| 57 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 58 | struct bsc_msg_acc_lst *bsc_msg_acc_lst_find(struct llist_head *head, const char *name) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 59 | { |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 60 | struct bsc_msg_acc_lst *lst; |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 61 | |
| 62 | if (!name) |
| 63 | return NULL; |
| 64 | |
Holger Hans Peter Freyther | d7e04b9 | 2015-04-04 22:28:32 +0200 | [diff] [blame] | 65 | llist_for_each_entry(lst, head, list) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 66 | if (strcmp(lst->name, name) == 0) |
| 67 | return lst; |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 72 | struct bsc_msg_acc_lst *bsc_msg_acc_lst_get(void *ctx, struct llist_head *head, const char *name) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 73 | { |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 74 | struct bsc_msg_acc_lst *lst; |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 75 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 76 | lst = bsc_msg_acc_lst_find(head, name); |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 77 | if (lst) |
| 78 | return lst; |
| 79 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 80 | lst = talloc_zero(ctx, struct bsc_msg_acc_lst); |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 81 | if (!lst) { |
| 82 | LOGP(DNAT, LOGL_ERROR, "Failed to allocate access list"); |
| 83 | return NULL; |
| 84 | } |
| 85 | |
| 86 | /* TODO: get the index right */ |
| 87 | lst->stats = rate_ctr_group_alloc(lst, &bsc_cfg_acc_list_desc, 0); |
| 88 | if (!lst->stats) { |
| 89 | talloc_free(lst); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | INIT_LLIST_HEAD(&lst->fltr_list); |
| 94 | lst->name = talloc_strdup(lst, name); |
Holger Hans Peter Freyther | d7e04b9 | 2015-04-04 22:28:32 +0200 | [diff] [blame] | 95 | llist_add_tail(&lst->list, head); |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 96 | return lst; |
| 97 | } |
| 98 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 99 | void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 100 | { |
| 101 | llist_del(&lst->list); |
| 102 | rate_ctr_group_free(lst->stats); |
| 103 | talloc_free(lst); |
| 104 | } |
| 105 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 106 | struct bsc_msg_acc_lst_entry *bsc_msg_acc_lst_entry_create(struct bsc_msg_acc_lst *lst) |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 107 | { |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 108 | struct bsc_msg_acc_lst_entry *entry; |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 109 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 110 | entry = talloc_zero(lst, struct bsc_msg_acc_lst_entry); |
Holger Hans Peter Freyther | 4579bb1 | 2015-04-04 21:55:08 +0200 | [diff] [blame] | 111 | if (!entry) |
| 112 | return NULL; |
| 113 | |
| 114 | entry->cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 115 | entry->lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED; |
| 116 | llist_add_tail(&entry->list, &lst->fltr_list); |
| 117 | return entry; |
| 118 | } |
| 119 | |