blob: a42206db85d8c2f3f62da770dea1c2c68c1eb6fc [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>
Neels Hofmeyr979cd262017-07-19 16:48:42 +020023#include <openbsc/debug.h>
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020024
25#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck46caed82015-11-02 15:15:38 +010026#include <osmocom/core/stats.h>
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020027
28#include <string.h>
29
30static const struct rate_ctr_desc acc_list_ctr_description[] = {
Holger Hans Peter Freyther14b2cd92015-04-05 16:50:34 +020031 [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 Freyther4579bb12015-04-04 21:55:08 +020033};
34
35static 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 Erlbeck46caed82015-11-02 15:15:38 +010040 .class_id = OSMO_STATS_CLASS_GLOBAL,
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020041};
42
43
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020044int 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 +020045{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020046 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020047
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 Freythera1e6bd62015-04-04 22:40:12 +020058struct 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 +020059{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020060 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020061
62 if (!name)
63 return NULL;
64
Holger Hans Peter Freytherd7e04b92015-04-04 22:28:32 +020065 llist_for_each_entry(lst, head, list)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020066 if (strcmp(lst->name, name) == 0)
67 return lst;
68
69 return NULL;
70}
71
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020072struct 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 +020073{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020074 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020075
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020076 lst = bsc_msg_acc_lst_find(head, name);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020077 if (lst)
78 return lst;
79
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020080 lst = talloc_zero(ctx, struct bsc_msg_acc_lst);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020081 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 Freytherd7e04b92015-04-04 22:28:32 +020095 llist_add_tail(&lst->list, head);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020096 return lst;
97}
98
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020099void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200100{
101 llist_del(&lst->list);
102 rate_ctr_group_free(lst->stats);
103 talloc_free(lst);
104}
105
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200106struct 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 +0200107{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200108 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200109
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200110 entry = talloc_zero(lst, struct bsc_msg_acc_lst_entry);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200111 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