blob: bfc5bdd3f80307f3217f2cab72b406e93e706b16 [file] [log] [blame]
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +02001/*
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 Erlbeck46caed82015-11-02 15:15:38 +010025#include <osmocom/core/stats.h>
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020026
27#include <string.h>
28
29static const struct rate_ctr_desc acc_list_ctr_description[] = {
Holger Hans Peter Freyther14b2cd92015-04-05 16:50:34 +020030 [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 Freyther4579bb12015-04-04 21:55:08 +020032};
33
34static 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 Erlbeck46caed82015-11-02 15:15:38 +010039 .class_id = OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020040};
41
42
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020043int bsc_msg_acc_lst_check_allow(struct bsc_msg_acc_lst *lst, const char *mi_string)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020044{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020045 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020046
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 Freythera1e6bd62015-04-04 22:40:12 +020057struct bsc_msg_acc_lst *bsc_msg_acc_lst_find(struct llist_head *head, const char *name)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020058{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020059 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020060
61 if (!name)
62 return NULL;
63
Holger Hans Peter Freytherd7e04b92015-04-04 22:28:32 +020064 llist_for_each_entry(lst, head, list)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020065 if (strcmp(lst->name, name) == 0)
66 return lst;
67
68 return NULL;
69}
70
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020071struct bsc_msg_acc_lst *bsc_msg_acc_lst_get(void *ctx, struct llist_head *head, const char *name)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020072{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020073 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020074
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020075 lst = bsc_msg_acc_lst_find(head, name);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020076 if (lst)
77 return lst;
78
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020079 lst = talloc_zero(ctx, struct bsc_msg_acc_lst);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020080 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 Freytherd7e04b92015-04-04 22:28:32 +020094 llist_add_tail(&lst->list, head);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020095 return lst;
96}
97
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020098void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020099{
100 llist_del(&lst->list);
101 rate_ctr_group_free(lst->stats);
102 talloc_free(lst);
103}
104
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200105struct bsc_msg_acc_lst_entry *bsc_msg_acc_lst_entry_create(struct bsc_msg_acc_lst *lst)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200106{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200107 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200108
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200109 entry = talloc_zero(lst, struct bsc_msg_acc_lst_entry);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200110 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