blob: 6258b35773c6892c4626c509f0183416afd3e8b2 [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>
25
26#include <string.h>
27
28static const struct rate_ctr_desc acc_list_ctr_description[] = {
Holger Hans Peter Freyther14b2cd92015-04-05 16:50:34 +020029 [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 Freyther4579bb12015-04-04 21:55:08 +020031};
32
33static 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 Freythera1e6bd62015-04-04 22:40:12 +020041int 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 +020042{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020043 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020044
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 Freythera1e6bd62015-04-04 22:40:12 +020055struct 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 +020056{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020057 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020058
59 if (!name)
60 return NULL;
61
Holger Hans Peter Freytherd7e04b92015-04-04 22:28:32 +020062 llist_for_each_entry(lst, head, list)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020063 if (strcmp(lst->name, name) == 0)
64 return lst;
65
66 return NULL;
67}
68
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020069struct 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 +020070{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020071 struct bsc_msg_acc_lst *lst;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020072
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020073 lst = bsc_msg_acc_lst_find(head, name);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020074 if (lst)
75 return lst;
76
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020077 lst = talloc_zero(ctx, struct bsc_msg_acc_lst);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020078 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 Freytherd7e04b92015-04-04 22:28:32 +020092 llist_add_tail(&lst->list, head);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020093 return lst;
94}
95
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +020096void bsc_msg_acc_lst_delete(struct bsc_msg_acc_lst *lst)
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +020097{
98 llist_del(&lst->list);
99 rate_ctr_group_free(lst->stats);
100 talloc_free(lst);
101}
102
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200103struct 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 +0200104{
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200105 struct bsc_msg_acc_lst_entry *entry;
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200106
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200107 entry = talloc_zero(lst, struct bsc_msg_acc_lst_entry);
Holger Hans Peter Freyther4579bb12015-04-04 21:55:08 +0200108 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