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