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_fw.c Classifier mapping ipchains' fwmark to traffic class. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 6 | * |
| 7 | * Changes: |
| 8 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_walk off by one |
| 9 | * Karlis Peisenieks <karlis@mt.lv> : 990415 : fw_delete killed all the filter (and kernel). |
| 10 | * Alex <alex@pilotsoft.com> : 2004xxyy: Added Action extension |
| 11 | * |
| 12 | * JHS: We should remove the CONFIG_NET_CLS_IND from here |
| 13 | * eventually when the meta match extension is made available |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/skbuff.h> |
Patrick McHardy | 0ba4805 | 2007-07-02 22:49:07 -0700 | [diff] [blame] | 23 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <net/act_api.h> |
| 25 | #include <net/pkt_cls.h> |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 26 | #include <net/sch_generic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 28 | #define HTSIZE 256 |
Thomas Graf | c5c13fa | 2005-04-24 20:19:54 -0700 | [diff] [blame] | 29 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 30 | struct fw_head { |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 31 | u32 mask; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 32 | struct fw_filter __rcu *ht[HTSIZE]; |
| 33 | struct rcu_head rcu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 36 | struct fw_filter { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 37 | struct fw_filter __rcu *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | u32 id; |
| 39 | struct tcf_result res; |
| 40 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 41 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #endif /* CONFIG_NET_CLS_IND */ |
| 43 | struct tcf_exts exts; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 44 | struct tcf_proto *tp; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 45 | struct rcu_work rwork; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 48 | static u32 fw_hash(u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Eric Dumazet | d37d8ac | 2014-03-17 20:20:49 -0700 | [diff] [blame] | 50 | handle ^= (handle >> 16); |
| 51 | handle ^= (handle >> 8); |
| 52 | return handle % HTSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 55 | static int fw_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 56 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 58 | struct fw_head *head = rcu_dereference_bh(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | struct fw_filter *f; |
| 60 | int r; |
Patrick McHardy | 5c804bf | 2006-12-05 13:46:13 -0800 | [diff] [blame] | 61 | u32 id = skb->mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | if (head != NULL) { |
Patrick McHardy | 5c804bf | 2006-12-05 13:46:13 -0800 | [diff] [blame] | 64 | id &= head->mask; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 65 | |
| 66 | for (f = rcu_dereference_bh(head->ht[fw_hash(id)]); f; |
| 67 | f = rcu_dereference_bh(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (f->id == id) { |
| 69 | *res = f->res; |
| 70 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 71 | if (!tcf_match_indev(skb, f->ifindex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | continue; |
| 73 | #endif /* CONFIG_NET_CLS_IND */ |
| 74 | r = tcf_exts_exec(skb, &f->exts, res); |
| 75 | if (r < 0) |
| 76 | continue; |
| 77 | |
| 78 | return r; |
| 79 | } |
| 80 | } |
| 81 | } else { |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 82 | struct Qdisc *q = tcf_block_q(tp->chain->block); |
| 83 | |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 84 | /* Old method: classify the packet using its skb mark. */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 85 | if (id && (TC_H_MAJ(id) == 0 || |
Jiri Pirko | 1abf272 | 2017-10-13 14:01:03 +0200 | [diff] [blame] | 86 | !(TC_H_MAJ(id ^ q->handle)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | res->classid = id; |
| 88 | res->class = 0; |
| 89 | return 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return -1; |
| 94 | } |
| 95 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 96 | static void *fw_get(struct tcf_proto *tp, u32 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 98 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | struct fw_filter *f; |
| 100 | |
| 101 | if (head == NULL) |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 102 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 104 | f = rtnl_dereference(head->ht[fw_hash(handle)]); |
| 105 | for (; f; f = rtnl_dereference(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | if (f->id == handle) |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 107 | return f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 109 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | static int fw_init(struct tcf_proto *tp) |
| 113 | { |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 114 | /* We don't allocate fw_head here, because in the old method |
| 115 | * we don't need it at all. |
| 116 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 120 | static void __fw_delete_filter(struct fw_filter *f) |
| 121 | { |
| 122 | tcf_exts_destroy(&f->exts); |
| 123 | tcf_exts_put_net(&f->exts); |
| 124 | kfree(f); |
| 125 | } |
| 126 | |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame] | 127 | static void fw_delete_filter_work(struct work_struct *work) |
| 128 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 129 | struct fw_filter *f = container_of(to_rcu_work(work), |
| 130 | struct fw_filter, |
| 131 | rwork); |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame] | 132 | rtnl_lock(); |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 133 | __fw_delete_filter(f); |
Cong Wang | e071dff | 2017-10-26 18:24:34 -0700 | [diff] [blame] | 134 | rtnl_unlock(); |
| 135 | } |
| 136 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 137 | static void fw_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 138 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 140 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct fw_filter *f; |
| 142 | int h; |
| 143 | |
| 144 | if (head == NULL) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 145 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 147 | for (h = 0; h < HTSIZE; h++) { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 148 | while ((f = rtnl_dereference(head->ht[h])) != NULL) { |
| 149 | RCU_INIT_POINTER(head->ht[h], |
| 150 | rtnl_dereference(f->next)); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 151 | tcf_unbind_filter(tp, &f->res); |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 152 | if (tcf_exts_get_net(&f->exts)) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 153 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 154 | else |
| 155 | __fw_delete_filter(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 158 | kfree_rcu(head, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 161 | static int fw_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 162 | bool rtnl_held, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 164 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 165 | struct fw_filter *f = arg; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 166 | struct fw_filter __rcu **fp; |
| 167 | struct fw_filter *pfp; |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 168 | int ret = -EINVAL; |
| 169 | int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (head == NULL || f == NULL) |
| 172 | goto out; |
| 173 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 174 | fp = &head->ht[fw_hash(f->id)]; |
| 175 | |
| 176 | for (pfp = rtnl_dereference(*fp); pfp; |
| 177 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) { |
| 178 | if (pfp == f) { |
| 179 | RCU_INIT_POINTER(*fp, rtnl_dereference(f->next)); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 180 | tcf_unbind_filter(tp, &f->res); |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 181 | tcf_exts_get_net(&f->exts); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 182 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 183 | ret = 0; |
| 184 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 187 | |
| 188 | *last = true; |
| 189 | for (h = 0; h < HTSIZE; h++) { |
| 190 | if (rcu_access_pointer(head->ht[h])) { |
| 191 | *last = false; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | out: |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 197 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 200 | static const struct nla_policy fw_policy[TCA_FW_MAX + 1] = { |
| 201 | [TCA_FW_CLASSID] = { .type = NLA_U32 }, |
| 202 | [TCA_FW_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, |
| 203 | [TCA_FW_MASK] = { .type = NLA_U32 }, |
| 204 | }; |
| 205 | |
Jiri Pirko | 1e5003a | 2017-08-04 14:29:05 +0200 | [diff] [blame] | 206 | static int fw_set_parms(struct net *net, struct tcf_proto *tp, |
| 207 | struct fw_filter *f, struct nlattr **tb, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 208 | struct nlattr **tca, unsigned long base, bool ovr, |
| 209 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 211 | struct fw_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 212 | u32 mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | int err; |
| 214 | |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 215 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &f->exts, ovr, |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 216 | true, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (err < 0) |
| 218 | return err; |
| 219 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 220 | if (tb[TCA_FW_CLASSID]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 221 | f->res.classid = nla_get_u32(tb[TCA_FW_CLASSID]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | tcf_bind_filter(tp, &f->res, base); |
| 223 | } |
| 224 | |
| 225 | #ifdef CONFIG_NET_CLS_IND |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 226 | if (tb[TCA_FW_INDEV]) { |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 227 | int ret; |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 228 | ret = tcf_change_indev(net, tb[TCA_FW_INDEV], extack); |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 229 | if (ret < 0) |
| 230 | return ret; |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 231 | f->ifindex = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | #endif /* CONFIG_NET_CLS_IND */ |
| 234 | |
Wei Yongjun | cb95ec6 | 2013-04-17 16:49:10 +0000 | [diff] [blame] | 235 | err = -EINVAL; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 236 | if (tb[TCA_FW_MASK]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 237 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 238 | if (mask != head->mask) |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 239 | return err; |
Patrick McHardy | b4e9b52 | 2006-08-25 16:11:42 -0700 | [diff] [blame] | 240 | } else if (head->mask != 0xFFFFFFFF) |
Jiri Pirko | 94611bf | 2017-08-04 14:29:07 +0200 | [diff] [blame] | 241 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 246 | static int fw_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 247 | struct tcf_proto *tp, unsigned long base, |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 248 | u32 handle, struct nlattr **tca, void **arg, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 249 | bool ovr, bool rtnl_held, |
| 250 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 252 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 253 | struct fw_filter *f = *arg; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 254 | struct nlattr *opt = tca[TCA_OPTIONS]; |
| 255 | struct nlattr *tb[TCA_FW_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | int err; |
| 257 | |
| 258 | if (!opt) |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 259 | return handle ? -EINVAL : 0; /* Succeed if it is old method. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 261 | err = nla_parse_nested_deprecated(tb, TCA_FW_MAX, opt, fw_policy, |
| 262 | NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 263 | if (err < 0) |
| 264 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 266 | if (f) { |
| 267 | struct fw_filter *pfp, *fnew; |
| 268 | struct fw_filter __rcu **fp; |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | if (f->id != handle && handle) |
| 271 | return -EINVAL; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 272 | |
| 273 | fnew = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
| 274 | if (!fnew) |
| 275 | return -ENOBUFS; |
| 276 | |
| 277 | fnew->id = f->id; |
| 278 | fnew->res = f->res; |
| 279 | #ifdef CONFIG_NET_CLS_IND |
| 280 | fnew->ifindex = f->ifindex; |
| 281 | #endif /* CONFIG_NET_CLS_IND */ |
| 282 | fnew->tp = f->tp; |
| 283 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 284 | err = tcf_exts_init(&fnew->exts, net, TCA_FW_ACT, |
| 285 | TCA_FW_POLICE); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 286 | if (err < 0) { |
| 287 | kfree(fnew); |
| 288 | return err; |
| 289 | } |
John Fastabend | e1f93eb | 2014-09-15 23:31:42 -0700 | [diff] [blame] | 290 | |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 291 | err = fw_set_parms(net, tp, fnew, tb, tca, base, ovr, extack); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 292 | if (err < 0) { |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 293 | tcf_exts_destroy(&fnew->exts); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 294 | kfree(fnew); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | fp = &head->ht[fw_hash(fnew->id)]; |
| 299 | for (pfp = rtnl_dereference(*fp); pfp; |
| 300 | fp = &pfp->next, pfp = rtnl_dereference(*fp)) |
| 301 | if (pfp == f) |
| 302 | break; |
| 303 | |
| 304 | RCU_INIT_POINTER(fnew->next, rtnl_dereference(pfp->next)); |
| 305 | rcu_assign_pointer(*fp, fnew); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 306 | tcf_unbind_filter(tp, &f->res); |
Cong Wang | d5f984f | 2017-11-06 13:47:25 -0800 | [diff] [blame] | 307 | tcf_exts_get_net(&f->exts); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 308 | tcf_queue_work(&f->rwork, fw_delete_filter_work); |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 309 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 310 | *arg = fnew; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 311 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (!handle) |
| 315 | return -EINVAL; |
| 316 | |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 317 | if (!head) { |
| 318 | u32 mask = 0xFFFFFFFF; |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 319 | if (tb[TCA_FW_MASK]) |
WANG Cong | d8aecb1 | 2015-09-22 17:01:11 -0700 | [diff] [blame] | 320 | mask = nla_get_u32(tb[TCA_FW_MASK]); |
| 321 | |
| 322 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 323 | if (!head) |
| 324 | return -ENOBUFS; |
| 325 | head->mask = mask; |
| 326 | |
| 327 | rcu_assign_pointer(tp->root, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 330 | f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (f == NULL) |
| 332 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 334 | err = tcf_exts_init(&f->exts, net, TCA_FW_ACT, TCA_FW_POLICE); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 335 | if (err < 0) |
| 336 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | f->id = handle; |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 338 | f->tp = tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 340 | err = fw_set_parms(net, tp, f, tb, tca, base, ovr, extack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | if (err < 0) |
| 342 | goto errout; |
| 343 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 344 | RCU_INIT_POINTER(f->next, head->ht[fw_hash(handle)]); |
| 345 | rcu_assign_pointer(head->ht[fw_hash(handle)], f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 347 | *arg = f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return 0; |
| 349 | |
| 350 | errout: |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 351 | tcf_exts_destroy(&f->exts); |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 352 | kfree(f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return err; |
| 354 | } |
| 355 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 356 | static void fw_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 357 | bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 359 | struct fw_head *head = rtnl_dereference(tp->root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | int h; |
| 361 | |
Vlad Buslov | 1d99787 | 2019-02-27 15:49:17 +0200 | [diff] [blame] | 362 | if (head == NULL) |
| 363 | arg->stop = 1; |
| 364 | |
| 365 | if (arg->stop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return; |
| 367 | |
Thomas Graf | c5c13fa | 2005-04-24 20:19:54 -0700 | [diff] [blame] | 368 | for (h = 0; h < HTSIZE; h++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | struct fw_filter *f; |
| 370 | |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 371 | for (f = rtnl_dereference(head->ht[h]); f; |
| 372 | f = rtnl_dereference(f->next)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (arg->count < arg->skip) { |
| 374 | arg->count++; |
| 375 | continue; |
| 376 | } |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 377 | if (arg->fn(tp, f, arg) < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | arg->stop = 1; |
| 379 | return; |
| 380 | } |
| 381 | arg->count++; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 386 | static int fw_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 387 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
John Fastabend | e35a8ee | 2014-09-12 20:07:22 -0700 | [diff] [blame] | 389 | struct fw_head *head = rtnl_dereference(tp->root); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 390 | struct fw_filter *f = fh; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 391 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | if (f == NULL) |
| 394 | return skb->len; |
| 395 | |
| 396 | t->tcm_handle = f->id; |
| 397 | |
Jiri Pirko | 6fc6d06 | 2017-08-04 14:29:00 +0200 | [diff] [blame] | 398 | if (!f->res.classid && !tcf_exts_has_actions(&f->exts)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | return skb->len; |
| 400 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 401 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 402 | if (nest == NULL) |
| 403 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 405 | if (f->res.classid && |
| 406 | nla_put_u32(skb, TCA_FW_CLASSID, f->res.classid)) |
| 407 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 409 | if (f->ifindex) { |
| 410 | struct net_device *dev; |
| 411 | dev = __dev_get_by_index(net, f->ifindex); |
| 412 | if (dev && nla_put_string(skb, TCA_FW_INDEV, dev->name)) |
| 413 | goto nla_put_failure; |
| 414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | #endif /* CONFIG_NET_CLS_IND */ |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 416 | if (head->mask != 0xFFFFFFFF && |
| 417 | nla_put_u32(skb, TCA_FW_MASK, head->mask)) |
| 418 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 420 | if (tcf_exts_dump(skb, &f->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 421 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 423 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 425 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 426 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | return skb->len; |
| 429 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 430 | nla_put_failure: |
Jiri Pirko | 6ea3b44 | 2014-12-09 22:23:29 +0100 | [diff] [blame] | 431 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return -1; |
| 433 | } |
| 434 | |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 435 | static void fw_bind_class(void *fh, u32 classid, unsigned long cl) |
| 436 | { |
| 437 | struct fw_filter *f = fh; |
| 438 | |
| 439 | if (f && f->res.classid == classid) |
| 440 | f->res.class = cl; |
| 441 | } |
| 442 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 443 | static struct tcf_proto_ops cls_fw_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | .kind = "fw", |
| 445 | .classify = fw_classify, |
| 446 | .init = fw_init, |
| 447 | .destroy = fw_destroy, |
| 448 | .get = fw_get, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | .change = fw_change, |
| 450 | .delete = fw_delete, |
| 451 | .walk = fw_walk, |
| 452 | .dump = fw_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 453 | .bind_class = fw_bind_class, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | .owner = THIS_MODULE, |
| 455 | }; |
| 456 | |
| 457 | static int __init init_fw(void) |
| 458 | { |
| 459 | return register_tcf_proto_ops(&cls_fw_ops); |
| 460 | } |
| 461 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 462 | static void __exit exit_fw(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
| 464 | unregister_tcf_proto_ops(&cls_fw_ops); |
| 465 | } |
| 466 | |
| 467 | module_init(init_fw) |
| 468 | module_exit(exit_fw) |
| 469 | MODULE_LICENSE("GPL"); |