Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * net/sched/cls_basic.c Basic Packet Classifier. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Thomas Graf <tgraf@suug.ch> |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/rtnetlink.h> |
| 15 | #include <linux/skbuff.h> |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 16 | #include <linux/idr.h> |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 17 | #include <linux/percpu.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 18 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <net/act_api.h> |
| 20 | #include <net/pkt_cls.h> |
| 21 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 22 | struct basic_head { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | struct list_head flist; |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 24 | struct idr handle_idr; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 25 | struct rcu_head rcu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 28 | struct basic_filter { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | u32 handle; |
| 30 | struct tcf_exts exts; |
| 31 | struct tcf_ematch_tree ematches; |
| 32 | struct tcf_result res; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 33 | struct tcf_proto *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | struct list_head link; |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 35 | struct tc_basic_pcnt __percpu *pf; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 36 | struct rcu_work rwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 39 | static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct tcf_result *res) |
| 41 | { |
| 42 | int r; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 43 | struct basic_head *head = rcu_dereference_bh(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct basic_filter *f; |
| 45 | |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 46 | list_for_each_entry_rcu(f, &head->flist, link) { |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 47 | __this_cpu_inc(f->pf->rcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | if (!tcf_em_tree_match(skb, &f->ematches, NULL)) |
| 49 | continue; |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 50 | __this_cpu_inc(f->pf->rhit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | *res = f->res; |
| 52 | r = tcf_exts_exec(skb, &f->exts, res); |
| 53 | if (r < 0) |
| 54 | continue; |
| 55 | return r; |
| 56 | } |
| 57 | return -1; |
| 58 | } |
| 59 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 60 | static void *basic_get(struct tcf_proto *tp, u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 62 | struct basic_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | struct basic_filter *f; |
| 64 | |
Daniel Borkmann | 1c1bc6b | 2015-01-22 10:58:18 +0100 | [diff] [blame] | 65 | list_for_each_entry(f, &head->flist, link) { |
| 66 | if (f->handle == handle) { |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 67 | return f; |
Daniel Borkmann | 1c1bc6b | 2015-01-22 10:58:18 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 71 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | static int basic_init(struct tcf_proto *tp) |
| 75 | { |
Patrick McHardy | d3fa76e | 2007-03-24 22:13:06 -0700 | [diff] [blame] | 76 | struct basic_head *head; |
| 77 | |
| 78 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 79 | if (head == NULL) |
| 80 | return -ENOBUFS; |
| 81 | INIT_LIST_HEAD(&head->flist); |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 82 | idr_init(&head->handle_idr); |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 83 | rcu_assign_pointer(tp->root, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 87 | static void __basic_delete_filter(struct basic_filter *f) |
| 88 | { |
| 89 | tcf_exts_destroy(&f->exts); |
| 90 | tcf_em_tree_destroy(&f->ematches); |
| 91 | tcf_exts_put_net(&f->exts); |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 92 | free_percpu(f->pf); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 93 | kfree(f); |
| 94 | } |
| 95 | |
Cong Wang | c96a483 | 2017-10-26 18:24:29 -0700 | [diff] [blame] | 96 | static void basic_delete_filter_work(struct work_struct *work) |
| 97 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 98 | struct basic_filter *f = container_of(to_rcu_work(work), |
| 99 | struct basic_filter, |
| 100 | rwork); |
Cong Wang | c96a483 | 2017-10-26 18:24:29 -0700 | [diff] [blame] | 101 | rtnl_lock(); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 102 | __basic_delete_filter(f); |
Cong Wang | c96a483 | 2017-10-26 18:24:29 -0700 | [diff] [blame] | 103 | rtnl_unlock(); |
Cong Wang | c96a483 | 2017-10-26 18:24:29 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 106 | static void basic_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 107 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 109 | struct basic_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct basic_filter *f, *n; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | list_for_each_entry_safe(f, n, &head->flist, link) { |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 113 | list_del_rcu(&f->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 114 | tcf_unbind_filter(tp, &f->res); |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 115 | idr_remove(&head->handle_idr, f->handle); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 116 | if (tcf_exts_get_net(&f->exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 117 | tcf_queue_work(&f->rwork, basic_delete_filter_work); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 118 | else |
| 119 | __basic_delete_filter(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 121 | idr_destroy(&head->handle_idr); |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 122 | kfree_rcu(head, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 125 | static int basic_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 126 | bool rtnl_held, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 128 | struct basic_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 129 | struct basic_filter *f = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Jiri Pirko | e438645 | 2014-12-02 18:00:31 +0100 | [diff] [blame] | 131 | list_del_rcu(&f->link); |
| 132 | tcf_unbind_filter(tp, &f->res); |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 133 | idr_remove(&head->handle_idr, f->handle); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 134 | tcf_exts_get_net(&f->exts); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 135 | tcf_queue_work(&f->rwork, basic_delete_filter_work); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 136 | *last = list_empty(&head->flist); |
Jiri Pirko | e438645 | 2014-12-02 18:00:31 +0100 | [diff] [blame] | 137 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 140 | static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = { |
| 141 | [TCA_BASIC_CLASSID] = { .type = NLA_U32 }, |
| 142 | [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED }, |
| 143 | }; |
| 144 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 145 | static int basic_set_parms(struct net *net, struct tcf_proto *tp, |
| 146 | struct basic_filter *f, unsigned long base, |
| 147 | struct nlattr **tb, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 148 | struct nlattr *est, u32 flags, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 149 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
stephen hemminger | 6459082 | 2013-09-26 17:42:16 -0700 | [diff] [blame] | 151 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 153 | err = tcf_exts_validate(net, tp, tb, est, &f->exts, flags, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (err < 0) |
| 155 | return err; |
| 156 | |
Jiri Pirko | 4ebc1e3 | 2017-08-04 14:28:57 +0200 | [diff] [blame] | 157 | err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | if (err < 0) |
Jiri Pirko | ff1f8ca | 2017-08-04 14:29:09 +0200 | [diff] [blame] | 159 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 161 | if (tb[TCA_BASIC_CLASSID]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 162 | f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | tcf_bind_filter(tp, &f->res, base); |
| 164 | } |
| 165 | |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 166 | f->tp = tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 170 | static int basic_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 171 | struct tcf_proto *tp, unsigned long base, u32 handle, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 172 | struct nlattr **tca, void **arg, |
| 173 | u32 flags, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 175 | int err; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 176 | struct basic_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 177 | struct nlattr *tb[TCA_BASIC_MAX + 1]; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 178 | struct basic_filter *fold = (struct basic_filter *) *arg; |
| 179 | struct basic_filter *fnew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 181 | if (tca[TCA_OPTIONS] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | return -EINVAL; |
| 183 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 184 | err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS], |
| 185 | basic_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 186 | if (err < 0) |
| 187 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 189 | if (fold != NULL) { |
| 190 | if (handle && fold->handle != handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 194 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); |
Jiri Pirko | bd42b78 | 2014-12-05 15:50:22 +0100 | [diff] [blame] | 195 | if (!fnew) |
| 196 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 198 | err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 199 | if (err < 0) |
| 200 | goto errout; |
| 201 | |
Matthew Wilcox | 05af0eb | 2017-11-28 10:36:09 -0500 | [diff] [blame] | 202 | if (!handle) { |
| 203 | handle = 1; |
| 204 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 205 | INT_MAX, GFP_KERNEL); |
| 206 | } else if (!fold) { |
| 207 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 208 | handle, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
Matthew Wilcox | 05af0eb | 2017-11-28 10:36:09 -0500 | [diff] [blame] | 210 | if (err) |
| 211 | goto errout; |
| 212 | fnew->handle = handle; |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 213 | fnew->pf = alloc_percpu(struct tc_basic_pcnt); |
| 214 | if (!fnew->pf) { |
| 215 | err = -ENOMEM; |
| 216 | goto errout; |
| 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 219 | err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], flags, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 220 | extack); |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 221 | if (err < 0) { |
| 222 | if (!fold) |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 223 | idr_remove(&head->handle_idr, fnew->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | goto errout; |
Cong Wang | 1d8134f | 2017-09-25 10:13:50 -0700 | [diff] [blame] | 225 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 227 | *arg = fnew; |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 228 | |
| 229 | if (fold) { |
Matthew Wilcox | 234a462 | 2017-11-28 09:56:36 -0500 | [diff] [blame] | 230 | idr_replace(&head->handle_idr, fnew, fnew->handle); |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 231 | list_replace_rcu(&fold->link, &fnew->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 232 | tcf_unbind_filter(tp, &fold->res); |
Cong Wang | 0b2a598 | 2017-11-06 13:47:20 -0800 | [diff] [blame] | 233 | tcf_exts_get_net(&fold->exts); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 234 | tcf_queue_work(&fold->rwork, basic_delete_filter_work); |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 235 | } else { |
| 236 | list_add_rcu(&fnew->link, &head->flist); |
| 237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | errout: |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 241 | free_percpu(fnew->pf); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 242 | tcf_exts_destroy(&fnew->exts); |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 243 | kfree(fnew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return err; |
| 245 | } |
| 246 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 247 | static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 248 | bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
John Fastabend | 9888faef | 2014-09-12 20:05:59 -0700 | [diff] [blame] | 250 | struct basic_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | struct basic_filter *f; |
| 252 | |
| 253 | list_for_each_entry(f, &head->flist, link) { |
| 254 | if (arg->count < arg->skip) |
| 255 | goto skip; |
| 256 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 257 | if (arg->fn(tp, f, arg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | arg->stop = 1; |
| 259 | break; |
| 260 | } |
| 261 | skip: |
| 262 | arg->count++; |
| 263 | } |
| 264 | } |
| 265 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 266 | static void basic_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
| 267 | unsigned long base) |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 268 | { |
| 269 | struct basic_filter *f = fh; |
| 270 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 271 | if (f && f->res.classid == classid) { |
| 272 | if (cl) |
| 273 | __tcf_bind_filter(q, &f->res, base); |
| 274 | else |
| 275 | __tcf_unbind_filter(q, &f->res); |
| 276 | } |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 279 | static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 280 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 282 | struct tc_basic_pcnt gpf = {}; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 283 | struct basic_filter *f = fh; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 284 | struct nlattr *nest; |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 285 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (f == NULL) |
| 288 | return skb->len; |
| 289 | |
| 290 | t->tcm_handle = f->handle; |
| 291 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 292 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 293 | if (nest == NULL) |
| 294 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 296 | if (f->res.classid && |
| 297 | nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid)) |
| 298 | goto nla_put_failure; |
Thomas Graf | e1e284a | 2005-06-08 15:11:02 -0700 | [diff] [blame] | 299 | |
Cong Wang | 5954894 | 2019-01-17 17:14:01 -0800 | [diff] [blame] | 300 | for_each_possible_cpu(cpu) { |
| 301 | struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu); |
| 302 | |
| 303 | gpf.rcnt += pf->rcnt; |
| 304 | gpf.rhit += pf->rhit; |
| 305 | } |
| 306 | |
| 307 | if (nla_put_64bit(skb, TCA_BASIC_PCNT, |
| 308 | sizeof(struct tc_basic_pcnt), |
| 309 | &gpf, TCA_BASIC_PAD)) |
| 310 | goto nla_put_failure; |
| 311 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 312 | if (tcf_exts_dump(skb, &f->exts) < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 314 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 316 | nla_nest_end(skb, nest); |
stephen hemminger | 4c46ee5 | 2010-11-04 11:47:04 +0000 | [diff] [blame] | 317 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 318 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
stephen hemminger | 4c46ee5 | 2010-11-04 11:47:04 +0000 | [diff] [blame] | 319 | goto nla_put_failure; |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | return skb->len; |
| 322 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 323 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 324 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return -1; |
| 326 | } |
| 327 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 328 | static struct tcf_proto_ops cls_basic_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | .kind = "basic", |
| 330 | .classify = basic_classify, |
| 331 | .init = basic_init, |
| 332 | .destroy = basic_destroy, |
| 333 | .get = basic_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | .change = basic_change, |
| 335 | .delete = basic_delete, |
| 336 | .walk = basic_walk, |
| 337 | .dump = basic_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 338 | .bind_class = basic_bind_class, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | .owner = THIS_MODULE, |
| 340 | }; |
| 341 | |
| 342 | static int __init init_basic(void) |
| 343 | { |
| 344 | return register_tcf_proto_ops(&cls_basic_ops); |
| 345 | } |
| 346 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 347 | static void __exit exit_basic(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | unregister_tcf_proto_ops(&cls_basic_ops); |
| 350 | } |
| 351 | |
| 352 | module_init(init_basic) |
| 353 | module_exit(exit_basic) |
| 354 | MODULE_LICENSE("GPL"); |