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