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