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_api.c Packet classifier API. |
| 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 | * |
| 9 | * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/errno.h> |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 17 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/kmod.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 22 | #include <linux/idr.h> |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 23 | #include <linux/rhashtable.h> |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 24 | #include <linux/jhash.h> |
Paul Blakey | 4371929 | 2020-02-16 12:01:23 +0200 | [diff] [blame] | 25 | #include <linux/rculist.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 26 | #include <net/net_namespace.h> |
| 27 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 28 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/pkt_sched.h> |
| 30 | #include <net/pkt_cls.h> |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 31 | #include <net/tc_act/tc_pedit.h> |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 32 | #include <net/tc_act/tc_mirred.h> |
| 33 | #include <net/tc_act/tc_vlan.h> |
| 34 | #include <net/tc_act/tc_tunnel_key.h> |
| 35 | #include <net/tc_act/tc_csum.h> |
| 36 | #include <net/tc_act/tc_gact.h> |
Pieter Jansen van Vuuren | 8c8cfc6 | 2019-05-04 04:46:22 -0700 | [diff] [blame] | 37 | #include <net/tc_act/tc_police.h> |
Pieter Jansen van Vuuren | a7a7be6 | 2019-05-04 04:46:16 -0700 | [diff] [blame] | 38 | #include <net/tc_act/tc_sample.h> |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 39 | #include <net/tc_act/tc_skbedit.h> |
Paul Blakey | b57dc7c | 2019-07-09 10:30:48 +0300 | [diff] [blame] | 40 | #include <net/tc_act/tc_ct.h> |
John Hurley | 6749d590 | 2019-07-23 15:33:59 +0100 | [diff] [blame] | 41 | #include <net/tc_act/tc_mpls.h> |
Po Liu | d29bdd6 | 2020-05-01 08:53:16 +0800 | [diff] [blame] | 42 | #include <net/tc_act/tc_gate.h> |
wenxu | 4e48190 | 2019-08-07 09:13:52 +0800 | [diff] [blame] | 43 | #include <net/flow_offload.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame] | 45 | extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1]; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* The list of all installed classifier types */ |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 48 | static LIST_HEAD(tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | /* Protects list of registered TC modules. It is pure SMP lock. */ |
| 51 | static DEFINE_RWLOCK(cls_mod_lock); |
| 52 | |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 53 | static u32 destroy_obj_hashfn(const struct tcf_proto *tp) |
| 54 | { |
| 55 | return jhash_3words(tp->chain->index, tp->prio, |
| 56 | (__force __u32)tp->protocol, 0); |
| 57 | } |
| 58 | |
| 59 | static void tcf_proto_signal_destroying(struct tcf_chain *chain, |
| 60 | struct tcf_proto *tp) |
| 61 | { |
| 62 | struct tcf_block *block = chain->block; |
| 63 | |
| 64 | mutex_lock(&block->proto_destroy_lock); |
| 65 | hash_add_rcu(block->proto_destroy_ht, &tp->destroy_ht_node, |
| 66 | destroy_obj_hashfn(tp)); |
| 67 | mutex_unlock(&block->proto_destroy_lock); |
| 68 | } |
| 69 | |
| 70 | static bool tcf_proto_cmp(const struct tcf_proto *tp1, |
| 71 | const struct tcf_proto *tp2) |
| 72 | { |
| 73 | return tp1->chain->index == tp2->chain->index && |
| 74 | tp1->prio == tp2->prio && |
| 75 | tp1->protocol == tp2->protocol; |
| 76 | } |
| 77 | |
| 78 | static bool tcf_proto_exists_destroying(struct tcf_chain *chain, |
| 79 | struct tcf_proto *tp) |
| 80 | { |
| 81 | u32 hash = destroy_obj_hashfn(tp); |
| 82 | struct tcf_proto *iter; |
| 83 | bool found = false; |
| 84 | |
| 85 | rcu_read_lock(); |
| 86 | hash_for_each_possible_rcu(chain->block->proto_destroy_ht, iter, |
| 87 | destroy_ht_node, hash) { |
| 88 | if (tcf_proto_cmp(tp, iter)) { |
| 89 | found = true; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | rcu_read_unlock(); |
| 94 | |
| 95 | return found; |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | tcf_proto_signal_destroyed(struct tcf_chain *chain, struct tcf_proto *tp) |
| 100 | { |
| 101 | struct tcf_block *block = chain->block; |
| 102 | |
| 103 | mutex_lock(&block->proto_destroy_lock); |
| 104 | if (hash_hashed(&tp->destroy_ht_node)) |
| 105 | hash_del_rcu(&tp->destroy_ht_node); |
| 106 | mutex_unlock(&block->proto_destroy_lock); |
| 107 | } |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | /* Find classifier type by string name */ |
| 110 | |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 111 | static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 113 | const struct tcf_proto_ops *t, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | if (kind) { |
| 116 | read_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 117 | list_for_each_entry(t, &tcf_proto_base, head) { |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 118 | if (strcmp(kind, t->kind) == 0) { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 119 | if (try_module_get(t->owner)) |
| 120 | res = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
| 124 | read_unlock(&cls_mod_lock); |
| 125 | } |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 126 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 129 | static const struct tcf_proto_ops * |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 130 | tcf_proto_lookup_ops(const char *kind, bool rtnl_held, |
| 131 | struct netlink_ext_ack *extack) |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 132 | { |
| 133 | const struct tcf_proto_ops *ops; |
| 134 | |
| 135 | ops = __tcf_proto_lookup_ops(kind); |
| 136 | if (ops) |
| 137 | return ops; |
| 138 | #ifdef CONFIG_MODULES |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 139 | if (rtnl_held) |
| 140 | rtnl_unlock(); |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 141 | request_module("cls_%s", kind); |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 142 | if (rtnl_held) |
| 143 | rtnl_lock(); |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 144 | ops = __tcf_proto_lookup_ops(kind); |
| 145 | /* We dropped the RTNL semaphore in order to perform |
| 146 | * the module load. So, even if we succeeded in loading |
| 147 | * the module we have to replay the request. We indicate |
| 148 | * this using -EAGAIN. |
| 149 | */ |
| 150 | if (ops) { |
| 151 | module_put(ops->owner); |
| 152 | return ERR_PTR(-EAGAIN); |
| 153 | } |
| 154 | #endif |
| 155 | NL_SET_ERR_MSG(extack, "TC classifier not found"); |
| 156 | return ERR_PTR(-ENOENT); |
| 157 | } |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* Register(unregister) new classifier type */ |
| 160 | |
| 161 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 162 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 163 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | int rc = -EEXIST; |
| 165 | |
| 166 | write_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 167 | list_for_each_entry(t, &tcf_proto_base, head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (!strcmp(ops->kind, t->kind)) |
| 169 | goto out; |
| 170 | |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 171 | list_add_tail(&ops->head, &tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | rc = 0; |
| 173 | out: |
| 174 | write_unlock(&cls_mod_lock); |
| 175 | return rc; |
| 176 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL(register_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 179 | static struct workqueue_struct *tc_filter_wq; |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 182 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 183 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int rc = -ENOENT; |
| 185 | |
Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 186 | /* Wait for outstanding call_rcu()s, if any, from a |
| 187 | * tcf_proto_ops's destroy() handler. |
| 188 | */ |
| 189 | rcu_barrier(); |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 190 | flush_workqueue(tc_filter_wq); |
Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | write_lock(&cls_mod_lock); |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 193 | list_for_each_entry(t, &tcf_proto_base, head) { |
| 194 | if (t == ops) { |
| 195 | list_del(&t->head); |
| 196 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | break; |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | write_unlock(&cls_mod_lock); |
| 201 | return rc; |
| 202 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 203 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 205 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 206 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 207 | INIT_RCU_WORK(rwork, func); |
| 208 | return queue_rcu_work(tc_filter_wq, rwork); |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 209 | } |
| 210 | EXPORT_SYMBOL(tcf_queue_work); |
| 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | /* Select new prio value from the range, managed by kernel. */ |
| 213 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 214 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 216 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | if (tp) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 219 | first = tp->prio - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Jiri Pirko | 7961973 | 2017-05-17 11:07:58 +0200 | [diff] [blame] | 221 | return TC_H_MAJ(first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 224 | static bool tcf_proto_check_kind(struct nlattr *kind, char *name) |
| 225 | { |
| 226 | if (kind) |
| 227 | return nla_strlcpy(name, kind, IFNAMSIZ) >= IFNAMSIZ; |
| 228 | memset(name, 0, IFNAMSIZ); |
| 229 | return false; |
| 230 | } |
| 231 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 232 | static bool tcf_proto_is_unlocked(const char *kind) |
| 233 | { |
| 234 | const struct tcf_proto_ops *ops; |
| 235 | bool ret; |
| 236 | |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 237 | if (strlen(kind) == 0) |
| 238 | return false; |
| 239 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 240 | ops = tcf_proto_lookup_ops(kind, false, NULL); |
| 241 | /* On error return false to take rtnl lock. Proto lookup/create |
| 242 | * functions will perform lookup again and properly handle errors. |
| 243 | */ |
| 244 | if (IS_ERR(ops)) |
| 245 | return false; |
| 246 | |
| 247 | ret = !!(ops->flags & TCF_PROTO_OPS_DOIT_UNLOCKED); |
| 248 | module_put(ops->owner); |
| 249 | return ret; |
| 250 | } |
| 251 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 252 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 253 | u32 prio, struct tcf_chain *chain, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 254 | bool rtnl_held, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 255 | struct netlink_ext_ack *extack) |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 256 | { |
| 257 | struct tcf_proto *tp; |
| 258 | int err; |
| 259 | |
| 260 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); |
| 261 | if (!tp) |
| 262 | return ERR_PTR(-ENOBUFS); |
| 263 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 264 | tp->ops = tcf_proto_lookup_ops(kind, rtnl_held, extack); |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 265 | if (IS_ERR(tp->ops)) { |
| 266 | err = PTR_ERR(tp->ops); |
Jiri Pirko | d68d75f | 2018-05-11 17:45:32 +0200 | [diff] [blame] | 267 | goto errout; |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 268 | } |
| 269 | tp->classify = tp->ops->classify; |
| 270 | tp->protocol = protocol; |
| 271 | tp->prio = prio; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 272 | tp->chain = chain; |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 273 | spin_lock_init(&tp->lock); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 274 | refcount_set(&tp->refcnt, 1); |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 275 | |
| 276 | err = tp->ops->init(tp); |
| 277 | if (err) { |
| 278 | module_put(tp->ops->owner); |
| 279 | goto errout; |
| 280 | } |
| 281 | return tp; |
| 282 | |
| 283 | errout: |
| 284 | kfree(tp); |
| 285 | return ERR_PTR(err); |
| 286 | } |
| 287 | |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 288 | static void tcf_proto_get(struct tcf_proto *tp) |
| 289 | { |
| 290 | refcount_inc(&tp->refcnt); |
| 291 | } |
| 292 | |
| 293 | static void tcf_chain_put(struct tcf_chain *chain); |
| 294 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 295 | static void tcf_proto_destroy(struct tcf_proto *tp, bool rtnl_held, |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 296 | bool sig_destroy, struct netlink_ext_ack *extack) |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 297 | { |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 298 | tp->ops->destroy(tp, rtnl_held, extack); |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 299 | if (sig_destroy) |
| 300 | tcf_proto_signal_destroyed(tp->chain, tp); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 301 | tcf_chain_put(tp->chain); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 302 | module_put(tp->ops->owner); |
| 303 | kfree_rcu(tp, rcu); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 306 | static void tcf_proto_put(struct tcf_proto *tp, bool rtnl_held, |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 307 | struct netlink_ext_ack *extack) |
| 308 | { |
| 309 | if (refcount_dec_and_test(&tp->refcnt)) |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 310 | tcf_proto_destroy(tp, rtnl_held, true, extack); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 313 | static bool tcf_proto_check_delete(struct tcf_proto *tp) |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 314 | { |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 315 | if (tp->ops->delete_empty) |
| 316 | return tp->ops->delete_empty(tp); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 317 | |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 318 | tp->deleting = true; |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 319 | return tp->deleting; |
| 320 | } |
| 321 | |
| 322 | static void tcf_proto_mark_delete(struct tcf_proto *tp) |
| 323 | { |
| 324 | spin_lock(&tp->lock); |
| 325 | tp->deleting = true; |
| 326 | spin_unlock(&tp->lock); |
| 327 | } |
| 328 | |
| 329 | static bool tcf_proto_is_deleting(struct tcf_proto *tp) |
| 330 | { |
| 331 | bool deleting; |
| 332 | |
| 333 | spin_lock(&tp->lock); |
| 334 | deleting = tp->deleting; |
| 335 | spin_unlock(&tp->lock); |
| 336 | |
| 337 | return deleting; |
| 338 | } |
| 339 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 340 | #define ASSERT_BLOCK_LOCKED(block) \ |
| 341 | lockdep_assert_held(&(block)->lock) |
| 342 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 343 | struct tcf_filter_chain_list_item { |
| 344 | struct list_head list; |
| 345 | tcf_chain_head_change_t *chain_head_change; |
| 346 | void *chain_head_change_priv; |
| 347 | }; |
| 348 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 349 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
| 350 | u32 chain_index) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 351 | { |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 352 | struct tcf_chain *chain; |
| 353 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 354 | ASSERT_BLOCK_LOCKED(block); |
| 355 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 356 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| 357 | if (!chain) |
| 358 | return NULL; |
Paul Blakey | 4371929 | 2020-02-16 12:01:23 +0200 | [diff] [blame] | 359 | list_add_tail_rcu(&chain->list, &block->chain_list); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 360 | mutex_init(&chain->filter_chain_lock); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 361 | chain->block = block; |
| 362 | chain->index = chain_index; |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 363 | chain->refcnt = 1; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 364 | if (!chain->index) |
| 365 | block->chain0.chain = chain; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 366 | return chain; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 369 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, |
| 370 | struct tcf_proto *tp_head) |
| 371 | { |
| 372 | if (item->chain_head_change) |
| 373 | item->chain_head_change(tp_head, item->chain_head_change_priv); |
| 374 | } |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 375 | |
| 376 | static void tcf_chain0_head_change(struct tcf_chain *chain, |
| 377 | struct tcf_proto *tp_head) |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 378 | { |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 379 | struct tcf_filter_chain_list_item *item; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 380 | struct tcf_block *block = chain->block; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 381 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 382 | if (chain->index) |
| 383 | return; |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 384 | |
| 385 | mutex_lock(&block->lock); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 386 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 387 | tcf_chain_head_change_item(item, tp_head); |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 388 | mutex_unlock(&block->lock); |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 391 | /* Returns true if block can be safely freed. */ |
| 392 | |
| 393 | static bool tcf_chain_detach(struct tcf_chain *chain) |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 394 | { |
Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 395 | struct tcf_block *block = chain->block; |
| 396 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 397 | ASSERT_BLOCK_LOCKED(block); |
| 398 | |
Paul Blakey | 4371929 | 2020-02-16 12:01:23 +0200 | [diff] [blame] | 399 | list_del_rcu(&chain->list); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 400 | if (!chain->index) |
| 401 | block->chain0.chain = NULL; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 402 | |
| 403 | if (list_empty(&block->chain_list) && |
| 404 | refcount_read(&block->refcnt) == 0) |
| 405 | return true; |
| 406 | |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | static void tcf_block_destroy(struct tcf_block *block) |
| 411 | { |
| 412 | mutex_destroy(&block->lock); |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 413 | mutex_destroy(&block->proto_destroy_lock); |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 414 | kfree_rcu(block, rcu); |
| 415 | } |
| 416 | |
| 417 | static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block) |
| 418 | { |
| 419 | struct tcf_block *block = chain->block; |
| 420 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 421 | mutex_destroy(&chain->filter_chain_lock); |
Davide Caratti | ee3bbfe | 2019-03-20 15:00:16 +0100 | [diff] [blame] | 422 | kfree_rcu(chain, rcu); |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 423 | if (free_block) |
| 424 | tcf_block_destroy(block); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 425 | } |
Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 426 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 427 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 428 | { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 429 | ASSERT_BLOCK_LOCKED(chain->block); |
| 430 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 431 | ++chain->refcnt; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 434 | static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 435 | { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 436 | ASSERT_BLOCK_LOCKED(chain->block); |
| 437 | |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 438 | /* In case all the references are action references, this |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 439 | * chain should not be shown to the user. |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 440 | */ |
| 441 | return chain->refcnt == chain->action_refcnt; |
| 442 | } |
| 443 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 444 | static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, |
| 445 | u32 chain_index) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 446 | { |
| 447 | struct tcf_chain *chain; |
| 448 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 449 | ASSERT_BLOCK_LOCKED(block); |
| 450 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 451 | list_for_each_entry(chain, &block->chain_list, list) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 452 | if (chain->index == chain_index) |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 453 | return chain; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 454 | } |
| 455 | return NULL; |
| 456 | } |
| 457 | |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 458 | #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT) |
| 459 | static struct tcf_chain *tcf_chain_lookup_rcu(const struct tcf_block *block, |
| 460 | u32 chain_index) |
| 461 | { |
| 462 | struct tcf_chain *chain; |
| 463 | |
| 464 | list_for_each_entry_rcu(chain, &block->chain_list, list) { |
| 465 | if (chain->index == chain_index) |
| 466 | return chain; |
| 467 | } |
| 468 | return NULL; |
| 469 | } |
| 470 | #endif |
| 471 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 472 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 473 | u32 seq, u16 flags, int event, bool unicast); |
| 474 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 475 | static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, |
| 476 | u32 chain_index, bool create, |
| 477 | bool by_act) |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 478 | { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 479 | struct tcf_chain *chain = NULL; |
| 480 | bool is_first_reference; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 481 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 482 | mutex_lock(&block->lock); |
| 483 | chain = tcf_chain_lookup(block, chain_index); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 484 | if (chain) { |
| 485 | tcf_chain_hold(chain); |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 486 | } else { |
| 487 | if (!create) |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 488 | goto errout; |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 489 | chain = tcf_chain_create(block, chain_index); |
| 490 | if (!chain) |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 491 | goto errout; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 492 | } |
Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 493 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 494 | if (by_act) |
| 495 | ++chain->action_refcnt; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 496 | is_first_reference = chain->refcnt - chain->action_refcnt == 1; |
| 497 | mutex_unlock(&block->lock); |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 498 | |
| 499 | /* Send notification only in case we got the first |
| 500 | * non-action reference. Until then, the chain acts only as |
| 501 | * a placeholder for actions pointing to it and user ought |
| 502 | * not know about them. |
| 503 | */ |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 504 | if (is_first_reference && !by_act) |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 505 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 506 | RTM_NEWCHAIN, false); |
| 507 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 508 | return chain; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 509 | |
| 510 | errout: |
| 511 | mutex_unlock(&block->lock); |
| 512 | return chain; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 513 | } |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 514 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 515 | static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 516 | bool create) |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 517 | { |
| 518 | return __tcf_chain_get(block, chain_index, create, false); |
| 519 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 520 | |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 521 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) |
| 522 | { |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 523 | return __tcf_chain_get(block, chain_index, true, true); |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 524 | } |
| 525 | EXPORT_SYMBOL(tcf_chain_get_by_act); |
| 526 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 527 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, |
| 528 | void *tmplt_priv); |
| 529 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, |
| 530 | void *tmplt_priv, u32 chain_index, |
| 531 | struct tcf_block *block, struct sk_buff *oskb, |
| 532 | u32 seq, u16 flags, bool unicast); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 533 | |
Vlad Buslov | 91052fa | 2019-02-11 10:55:33 +0200 | [diff] [blame] | 534 | static void __tcf_chain_put(struct tcf_chain *chain, bool by_act, |
| 535 | bool explicitly_created) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 536 | { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 537 | struct tcf_block *block = chain->block; |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 538 | const struct tcf_proto_ops *tmplt_ops; |
Vlad Buslov | b62989f | 2019-03-06 17:50:43 +0200 | [diff] [blame] | 539 | bool free_block = false; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 540 | unsigned int refcnt; |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 541 | void *tmplt_priv; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 542 | |
| 543 | mutex_lock(&block->lock); |
Vlad Buslov | 91052fa | 2019-02-11 10:55:33 +0200 | [diff] [blame] | 544 | if (explicitly_created) { |
| 545 | if (!chain->explicitly_created) { |
| 546 | mutex_unlock(&block->lock); |
| 547 | return; |
| 548 | } |
| 549 | chain->explicitly_created = false; |
| 550 | } |
| 551 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 552 | if (by_act) |
| 553 | chain->action_refcnt--; |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 554 | |
| 555 | /* tc_chain_notify_delete can't be called while holding block lock. |
| 556 | * However, when block is unlocked chain can be changed concurrently, so |
| 557 | * save these to temporary variables. |
| 558 | */ |
| 559 | refcnt = --chain->refcnt; |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 560 | tmplt_ops = chain->tmplt_ops; |
| 561 | tmplt_priv = chain->tmplt_priv; |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 562 | |
| 563 | /* The last dropped non-action reference will trigger notification. */ |
Vlad Buslov | b62989f | 2019-03-06 17:50:43 +0200 | [diff] [blame] | 564 | if (refcnt - chain->action_refcnt == 0 && !by_act) { |
| 565 | tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain->index, |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 566 | block, NULL, 0, 0, false); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 567 | /* Last reference to chain, no need to lock. */ |
| 568 | chain->flushing = false; |
| 569 | } |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 570 | |
Vlad Buslov | b62989f | 2019-03-06 17:50:43 +0200 | [diff] [blame] | 571 | if (refcnt == 0) |
| 572 | free_block = tcf_chain_detach(chain); |
| 573 | mutex_unlock(&block->lock); |
| 574 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 575 | if (refcnt == 0) { |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 576 | tc_chain_tmplt_del(tmplt_ops, tmplt_priv); |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 577 | tcf_chain_destroy(chain, free_block); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 578 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 579 | } |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 580 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 581 | static void tcf_chain_put(struct tcf_chain *chain) |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 582 | { |
Vlad Buslov | 91052fa | 2019-02-11 10:55:33 +0200 | [diff] [blame] | 583 | __tcf_chain_put(chain, false, false); |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 584 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 585 | |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 586 | void tcf_chain_put_by_act(struct tcf_chain *chain) |
| 587 | { |
Vlad Buslov | 91052fa | 2019-02-11 10:55:33 +0200 | [diff] [blame] | 588 | __tcf_chain_put(chain, true, false); |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 589 | } |
| 590 | EXPORT_SYMBOL(tcf_chain_put_by_act); |
| 591 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 592 | static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) |
| 593 | { |
Vlad Buslov | 91052fa | 2019-02-11 10:55:33 +0200 | [diff] [blame] | 594 | __tcf_chain_put(chain, false, true); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 597 | static void tcf_chain_flush(struct tcf_chain *chain, bool rtnl_held) |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 598 | { |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 599 | struct tcf_proto *tp, *tp_next; |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 600 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 601 | mutex_lock(&chain->filter_chain_lock); |
| 602 | tp = tcf_chain_dereference(chain->filter_chain, chain); |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 603 | while (tp) { |
| 604 | tp_next = rcu_dereference_protected(tp->next, 1); |
| 605 | tcf_proto_signal_destroying(chain, tp); |
| 606 | tp = tp_next; |
| 607 | } |
| 608 | tp = tcf_chain_dereference(chain->filter_chain, chain); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 609 | RCU_INIT_POINTER(chain->filter_chain, NULL); |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 610 | tcf_chain0_head_change(chain, NULL); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 611 | chain->flushing = true; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 612 | mutex_unlock(&chain->filter_chain_lock); |
| 613 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 614 | while (tp) { |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 615 | tp_next = rcu_dereference_protected(tp->next, 1); |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 616 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 617 | tp = tp_next; |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
wenxu | 4e48190 | 2019-08-07 09:13:52 +0800 | [diff] [blame] | 621 | static int tcf_block_setup(struct tcf_block *block, |
| 622 | struct flow_block_offload *bo); |
| 623 | |
Pablo Neira Ayuso | 324a823 | 2020-05-29 02:25:36 +0200 | [diff] [blame] | 624 | static void tcf_block_offload_init(struct flow_block_offload *bo, |
| 625 | struct net_device *dev, |
| 626 | enum flow_block_command command, |
| 627 | enum flow_block_binder_type binder_type, |
| 628 | struct flow_block *flow_block, |
| 629 | bool shared, struct netlink_ext_ack *extack) |
| 630 | { |
| 631 | bo->net = dev_net(dev); |
| 632 | bo->command = command; |
| 633 | bo->binder_type = binder_type; |
| 634 | bo->block = flow_block; |
| 635 | bo->block_shared = shared; |
| 636 | bo->extack = extack; |
| 637 | INIT_LIST_HEAD(&bo->cb_list); |
| 638 | } |
| 639 | |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 640 | static void tcf_block_unbind(struct tcf_block *block, |
| 641 | struct flow_block_offload *bo); |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 642 | |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 643 | static void tc_block_indr_cleanup(struct flow_block_cb *block_cb) |
| 644 | { |
| 645 | struct tcf_block *block = block_cb->indr.data; |
| 646 | struct net_device *dev = block_cb->indr.dev; |
| 647 | struct netlink_ext_ack extack = {}; |
| 648 | struct flow_block_offload bo; |
| 649 | |
| 650 | tcf_block_offload_init(&bo, dev, FLOW_BLOCK_UNBIND, |
| 651 | block_cb->indr.binder_type, |
| 652 | &block->flow_block, tcf_block_shared(block), |
| 653 | &extack); |
| 654 | down_write(&block->cb_lock); |
| 655 | list_move(&block_cb->list, &bo.cb_list); |
| 656 | up_write(&block->cb_lock); |
| 657 | rtnl_lock(); |
| 658 | tcf_block_unbind(block, &bo); |
| 659 | rtnl_unlock(); |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 660 | } |
| 661 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 662 | static bool tcf_block_offload_in_use(struct tcf_block *block) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 663 | { |
Vlad Buslov | 97394be | 2019-08-26 16:44:58 +0300 | [diff] [blame] | 664 | return atomic_read(&block->offloadcnt); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static int tcf_block_offload_cmd(struct tcf_block *block, |
| 668 | struct net_device *dev, |
| 669 | struct tcf_block_ext_info *ei, |
Pablo Neira Ayuso | 9c0e189 | 2019-07-09 22:55:40 +0200 | [diff] [blame] | 670 | enum flow_block_command command, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 671 | struct netlink_ext_ack *extack) |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 672 | { |
Pablo Neira Ayuso | 955bcb6 | 2019-07-09 22:55:46 +0200 | [diff] [blame] | 673 | struct flow_block_offload bo = {}; |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 674 | int err; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 675 | |
Pablo Neira Ayuso | 324a823 | 2020-05-29 02:25:36 +0200 | [diff] [blame] | 676 | tcf_block_offload_init(&bo, dev, command, ei->binder_type, |
| 677 | &block->flow_block, tcf_block_shared(block), |
| 678 | extack); |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 679 | |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 680 | if (dev->netdev_ops->ndo_setup_tc) |
| 681 | err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
| 682 | else |
| 683 | err = flow_indr_dev_setup_offload(dev, TC_SETUP_BLOCK, block, |
| 684 | &bo, tc_block_indr_cleanup); |
| 685 | |
Jesper Dangaard Brouer | b70ba69 | 2020-04-23 16:57:45 +0200 | [diff] [blame] | 686 | if (err < 0) { |
| 687 | if (err != -EOPNOTSUPP) |
| 688 | NL_SET_ERR_MSG(extack, "Driver ndo_setup_tc failed"); |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 689 | return err; |
Jesper Dangaard Brouer | b70ba69 | 2020-04-23 16:57:45 +0200 | [diff] [blame] | 690 | } |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 691 | |
| 692 | return tcf_block_setup(block, &bo); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 695 | static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 696 | struct tcf_block_ext_info *ei, |
| 697 | struct netlink_ext_ack *extack) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 698 | { |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 699 | struct net_device *dev = q->dev_queue->dev; |
| 700 | int err; |
| 701 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 702 | down_write(&block->cb_lock); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 703 | |
| 704 | /* If tc offload feature is disabled and the block we try to bind |
| 705 | * to already has some offloaded filters, forbid to bind. |
| 706 | */ |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 707 | if (dev->netdev_ops->ndo_setup_tc && |
| 708 | !tc_can_offload(dev) && |
| 709 | tcf_block_offload_in_use(block)) { |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 710 | NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled"); |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 711 | err = -EOPNOTSUPP; |
| 712 | goto err_unlock; |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 713 | } |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 714 | |
Pablo Neira Ayuso | 9c0e189 | 2019-07-09 22:55:40 +0200 | [diff] [blame] | 715 | err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_BIND, extack); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 716 | if (err == -EOPNOTSUPP) |
| 717 | goto no_offload_dev_inc; |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 718 | if (err) |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 719 | goto err_unlock; |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 720 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 721 | up_write(&block->cb_lock); |
John Hurley | 7f76fa3 | 2018-11-09 21:21:26 -0800 | [diff] [blame] | 722 | return 0; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 723 | |
| 724 | no_offload_dev_inc: |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 725 | if (tcf_block_offload_in_use(block)) |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 726 | goto err_unlock; |
Pablo Neira Ayuso | 0fdcf78 | 2020-05-29 02:25:37 +0200 | [diff] [blame] | 727 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 728 | err = 0; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 729 | block->nooffloaddevcnt++; |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 730 | err_unlock: |
| 731 | up_write(&block->cb_lock); |
| 732 | return err; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, |
| 736 | struct tcf_block_ext_info *ei) |
| 737 | { |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 738 | struct net_device *dev = q->dev_queue->dev; |
| 739 | int err; |
| 740 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 741 | down_write(&block->cb_lock); |
Pablo Neira Ayuso | 9c0e189 | 2019-07-09 22:55:40 +0200 | [diff] [blame] | 742 | err = tcf_block_offload_cmd(block, dev, ei, FLOW_BLOCK_UNBIND, NULL); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 743 | if (err == -EOPNOTSUPP) |
| 744 | goto no_offload_dev_dec; |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 745 | up_write(&block->cb_lock); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 746 | return; |
| 747 | |
| 748 | no_offload_dev_dec: |
| 749 | WARN_ON(block->nooffloaddevcnt-- == 0); |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 750 | up_write(&block->cb_lock); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 751 | } |
| 752 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 753 | static int |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 754 | tcf_chain0_head_change_cb_add(struct tcf_block *block, |
| 755 | struct tcf_block_ext_info *ei, |
| 756 | struct netlink_ext_ack *extack) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 757 | { |
| 758 | struct tcf_filter_chain_list_item *item; |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 759 | struct tcf_chain *chain0; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 760 | |
| 761 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 762 | if (!item) { |
| 763 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); |
| 764 | return -ENOMEM; |
| 765 | } |
| 766 | item->chain_head_change = ei->chain_head_change; |
| 767 | item->chain_head_change_priv = ei->chain_head_change_priv; |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 768 | |
| 769 | mutex_lock(&block->lock); |
| 770 | chain0 = block->chain0.chain; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 771 | if (chain0) |
| 772 | tcf_chain_hold(chain0); |
| 773 | else |
| 774 | list_add(&item->list, &block->chain0.filter_chain_list); |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 775 | mutex_unlock(&block->lock); |
| 776 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 777 | if (chain0) { |
| 778 | struct tcf_proto *tp_head; |
| 779 | |
| 780 | mutex_lock(&chain0->filter_chain_lock); |
| 781 | |
| 782 | tp_head = tcf_chain_dereference(chain0->filter_chain, chain0); |
| 783 | if (tp_head) |
| 784 | tcf_chain_head_change_item(item, tp_head); |
| 785 | |
| 786 | mutex_lock(&block->lock); |
| 787 | list_add(&item->list, &block->chain0.filter_chain_list); |
| 788 | mutex_unlock(&block->lock); |
| 789 | |
| 790 | mutex_unlock(&chain0->filter_chain_lock); |
| 791 | tcf_chain_put(chain0); |
| 792 | } |
| 793 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static void |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 798 | tcf_chain0_head_change_cb_del(struct tcf_block *block, |
| 799 | struct tcf_block_ext_info *ei) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 800 | { |
| 801 | struct tcf_filter_chain_list_item *item; |
| 802 | |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 803 | mutex_lock(&block->lock); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 804 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) { |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 805 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) || |
| 806 | (item->chain_head_change == ei->chain_head_change && |
| 807 | item->chain_head_change_priv == ei->chain_head_change_priv)) { |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 808 | if (block->chain0.chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 809 | tcf_chain_head_change_item(item, NULL); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 810 | list_del(&item->list); |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 811 | mutex_unlock(&block->lock); |
| 812 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 813 | kfree(item); |
| 814 | return; |
| 815 | } |
| 816 | } |
Vlad Buslov | 165f013 | 2019-02-11 10:55:35 +0200 | [diff] [blame] | 817 | mutex_unlock(&block->lock); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 818 | WARN_ON(1); |
| 819 | } |
| 820 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 821 | struct tcf_net { |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 822 | spinlock_t idr_lock; /* Protects idr */ |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 823 | struct idr idr; |
| 824 | }; |
| 825 | |
| 826 | static unsigned int tcf_net_id; |
| 827 | |
| 828 | static int tcf_block_insert(struct tcf_block *block, struct net *net, |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 829 | struct netlink_ext_ack *extack) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 830 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 831 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 832 | int err; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 833 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 834 | idr_preload(GFP_KERNEL); |
| 835 | spin_lock(&tn->idr_lock); |
| 836 | err = idr_alloc_u32(&tn->idr, block, &block->index, block->index, |
| 837 | GFP_NOWAIT); |
| 838 | spin_unlock(&tn->idr_lock); |
| 839 | idr_preload_end(); |
| 840 | |
| 841 | return err; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 842 | } |
| 843 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 844 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 845 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 846 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 847 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 848 | spin_lock(&tn->idr_lock); |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 849 | idr_remove(&tn->idr, block->index); |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 850 | spin_unlock(&tn->idr_lock); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 854 | u32 block_index, |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 855 | struct netlink_ext_ack *extack) |
| 856 | { |
| 857 | struct tcf_block *block; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 858 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 859 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 860 | if (!block) { |
| 861 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 862 | return ERR_PTR(-ENOMEM); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 863 | } |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 864 | mutex_init(&block->lock); |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 865 | mutex_init(&block->proto_destroy_lock); |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 866 | init_rwsem(&block->cb_lock); |
Pablo Neira Ayuso | 14bfb13 | 2019-07-19 18:20:16 +0200 | [diff] [blame] | 867 | flow_block_init(&block->flow_block); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 868 | INIT_LIST_HEAD(&block->chain_list); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 869 | INIT_LIST_HEAD(&block->owner_list); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 870 | INIT_LIST_HEAD(&block->chain0.filter_chain_list); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 871 | |
Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame] | 872 | refcount_set(&block->refcnt, 1); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 873 | block->net = net; |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 874 | block->index = block_index; |
| 875 | |
| 876 | /* Don't store q pointer for blocks which are shared */ |
| 877 | if (!tcf_block_shared(block)) |
| 878 | block->q = q; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 879 | return block; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) |
| 883 | { |
| 884 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 885 | |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 886 | return idr_find(&tn->idr, block_index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 889 | static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index) |
| 890 | { |
| 891 | struct tcf_block *block; |
| 892 | |
| 893 | rcu_read_lock(); |
| 894 | block = tcf_block_lookup(net, block_index); |
| 895 | if (block && !refcount_inc_not_zero(&block->refcnt)) |
| 896 | block = NULL; |
| 897 | rcu_read_unlock(); |
| 898 | |
| 899 | return block; |
| 900 | } |
| 901 | |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 902 | static struct tcf_chain * |
| 903 | __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) |
| 904 | { |
| 905 | mutex_lock(&block->lock); |
| 906 | if (chain) |
| 907 | chain = list_is_last(&chain->list, &block->chain_list) ? |
| 908 | NULL : list_next_entry(chain, list); |
| 909 | else |
| 910 | chain = list_first_entry_or_null(&block->chain_list, |
| 911 | struct tcf_chain, list); |
| 912 | |
| 913 | /* skip all action-only chains */ |
| 914 | while (chain && tcf_chain_held_by_acts_only(chain)) |
| 915 | chain = list_is_last(&chain->list, &block->chain_list) ? |
| 916 | NULL : list_next_entry(chain, list); |
| 917 | |
| 918 | if (chain) |
| 919 | tcf_chain_hold(chain); |
| 920 | mutex_unlock(&block->lock); |
| 921 | |
| 922 | return chain; |
| 923 | } |
| 924 | |
| 925 | /* Function to be used by all clients that want to iterate over all chains on |
| 926 | * block. It properly obtains block->lock and takes reference to chain before |
| 927 | * returning it. Users of this function must be tolerant to concurrent chain |
| 928 | * insertion/deletion or ensure that no concurrent chain modification is |
| 929 | * possible. Note that all netlink dump callbacks cannot guarantee to provide |
| 930 | * consistent dump because rtnl lock is released each time skb is filled with |
| 931 | * data and sent to user-space. |
| 932 | */ |
| 933 | |
| 934 | struct tcf_chain * |
| 935 | tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) |
| 936 | { |
| 937 | struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain); |
| 938 | |
| 939 | if (chain) |
| 940 | tcf_chain_put(chain); |
| 941 | |
| 942 | return chain_next; |
| 943 | } |
| 944 | EXPORT_SYMBOL(tcf_get_next_chain); |
| 945 | |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 946 | static struct tcf_proto * |
| 947 | __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) |
| 948 | { |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 949 | u32 prio = 0; |
| 950 | |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 951 | ASSERT_RTNL(); |
| 952 | mutex_lock(&chain->filter_chain_lock); |
| 953 | |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 954 | if (!tp) { |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 955 | tp = tcf_chain_dereference(chain->filter_chain, chain); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 956 | } else if (tcf_proto_is_deleting(tp)) { |
| 957 | /* 'deleting' flag is set and chain->filter_chain_lock was |
| 958 | * unlocked, which means next pointer could be invalid. Restart |
| 959 | * search. |
| 960 | */ |
| 961 | prio = tp->prio + 1; |
| 962 | tp = tcf_chain_dereference(chain->filter_chain, chain); |
| 963 | |
| 964 | for (; tp; tp = tcf_chain_dereference(tp->next, chain)) |
| 965 | if (!tp->deleting && tp->prio >= prio) |
| 966 | break; |
| 967 | } else { |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 968 | tp = tcf_chain_dereference(tp->next, chain); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 969 | } |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 970 | |
| 971 | if (tp) |
| 972 | tcf_proto_get(tp); |
| 973 | |
| 974 | mutex_unlock(&chain->filter_chain_lock); |
| 975 | |
| 976 | return tp; |
| 977 | } |
| 978 | |
| 979 | /* Function to be used by all clients that want to iterate over all tp's on |
| 980 | * chain. Users of this function must be tolerant to concurrent tp |
| 981 | * insertion/deletion or ensure that no concurrent chain modification is |
| 982 | * possible. Note that all netlink dump callbacks cannot guarantee to provide |
| 983 | * consistent dump because rtnl lock is released each time skb is filled with |
| 984 | * data and sent to user-space. |
| 985 | */ |
| 986 | |
| 987 | struct tcf_proto * |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 988 | tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp, |
| 989 | bool rtnl_held) |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 990 | { |
| 991 | struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp); |
| 992 | |
| 993 | if (tp) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 994 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 995 | |
| 996 | return tp_next; |
| 997 | } |
| 998 | EXPORT_SYMBOL(tcf_get_next_proto); |
| 999 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1000 | static void tcf_block_flush_all_chains(struct tcf_block *block, bool rtnl_held) |
Vlad Buslov | f002343 | 2018-09-24 19:22:55 +0300 | [diff] [blame] | 1001 | { |
| 1002 | struct tcf_chain *chain; |
| 1003 | |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 1004 | /* Last reference to block. At this point chains cannot be added or |
| 1005 | * removed concurrently. |
Vlad Buslov | f002343 | 2018-09-24 19:22:55 +0300 | [diff] [blame] | 1006 | */ |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 1007 | for (chain = tcf_get_next_chain(block, NULL); |
| 1008 | chain; |
| 1009 | chain = tcf_get_next_chain(block, chain)) { |
Vlad Buslov | f002343 | 2018-09-24 19:22:55 +0300 | [diff] [blame] | 1010 | tcf_chain_put_explicitly_created(chain); |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1011 | tcf_chain_flush(chain, rtnl_held); |
Vlad Buslov | f002343 | 2018-09-24 19:22:55 +0300 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1015 | /* Lookup Qdisc and increments its reference counter. |
| 1016 | * Set parent, if necessary. |
| 1017 | */ |
| 1018 | |
| 1019 | static int __tcf_qdisc_find(struct net *net, struct Qdisc **q, |
| 1020 | u32 *parent, int ifindex, bool rtnl_held, |
| 1021 | struct netlink_ext_ack *extack) |
| 1022 | { |
| 1023 | const struct Qdisc_class_ops *cops; |
| 1024 | struct net_device *dev; |
| 1025 | int err = 0; |
| 1026 | |
| 1027 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
| 1028 | return 0; |
| 1029 | |
| 1030 | rcu_read_lock(); |
| 1031 | |
| 1032 | /* Find link */ |
| 1033 | dev = dev_get_by_index_rcu(net, ifindex); |
| 1034 | if (!dev) { |
| 1035 | rcu_read_unlock(); |
| 1036 | return -ENODEV; |
| 1037 | } |
| 1038 | |
| 1039 | /* Find qdisc */ |
| 1040 | if (!*parent) { |
| 1041 | *q = dev->qdisc; |
| 1042 | *parent = (*q)->handle; |
| 1043 | } else { |
| 1044 | *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); |
| 1045 | if (!*q) { |
| 1046 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 1047 | err = -EINVAL; |
| 1048 | goto errout_rcu; |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | *q = qdisc_refcount_inc_nz(*q); |
| 1053 | if (!*q) { |
| 1054 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 1055 | err = -EINVAL; |
| 1056 | goto errout_rcu; |
| 1057 | } |
| 1058 | |
| 1059 | /* Is it classful? */ |
| 1060 | cops = (*q)->ops->cl_ops; |
| 1061 | if (!cops) { |
| 1062 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); |
| 1063 | err = -EINVAL; |
| 1064 | goto errout_qdisc; |
| 1065 | } |
| 1066 | |
| 1067 | if (!cops->tcf_block) { |
| 1068 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); |
| 1069 | err = -EOPNOTSUPP; |
| 1070 | goto errout_qdisc; |
| 1071 | } |
| 1072 | |
| 1073 | errout_rcu: |
| 1074 | /* At this point we know that qdisc is not noop_qdisc, |
| 1075 | * which means that qdisc holds a reference to net_device |
| 1076 | * and we hold a reference to qdisc, so it is safe to release |
| 1077 | * rcu read lock. |
| 1078 | */ |
| 1079 | rcu_read_unlock(); |
| 1080 | return err; |
| 1081 | |
| 1082 | errout_qdisc: |
| 1083 | rcu_read_unlock(); |
| 1084 | |
| 1085 | if (rtnl_held) |
| 1086 | qdisc_put(*q); |
| 1087 | else |
| 1088 | qdisc_put_unlocked(*q); |
| 1089 | *q = NULL; |
| 1090 | |
| 1091 | return err; |
| 1092 | } |
| 1093 | |
| 1094 | static int __tcf_qdisc_cl_find(struct Qdisc *q, u32 parent, unsigned long *cl, |
| 1095 | int ifindex, struct netlink_ext_ack *extack) |
| 1096 | { |
| 1097 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
| 1098 | return 0; |
| 1099 | |
| 1100 | /* Do we search for filter, attached to class? */ |
| 1101 | if (TC_H_MIN(parent)) { |
| 1102 | const struct Qdisc_class_ops *cops = q->ops->cl_ops; |
| 1103 | |
| 1104 | *cl = cops->find(q, parent); |
| 1105 | if (*cl == 0) { |
| 1106 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); |
| 1107 | return -ENOENT; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
| 1114 | static struct tcf_block *__tcf_block_find(struct net *net, struct Qdisc *q, |
| 1115 | unsigned long cl, int ifindex, |
| 1116 | u32 block_index, |
| 1117 | struct netlink_ext_ack *extack) |
| 1118 | { |
| 1119 | struct tcf_block *block; |
| 1120 | |
| 1121 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 1122 | block = tcf_block_refcnt_get(net, block_index); |
| 1123 | if (!block) { |
| 1124 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); |
| 1125 | return ERR_PTR(-EINVAL); |
| 1126 | } |
| 1127 | } else { |
| 1128 | const struct Qdisc_class_ops *cops = q->ops->cl_ops; |
| 1129 | |
| 1130 | block = cops->tcf_block(q, cl, extack); |
| 1131 | if (!block) |
| 1132 | return ERR_PTR(-EINVAL); |
| 1133 | |
| 1134 | if (tcf_block_shared(block)) { |
| 1135 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); |
| 1136 | return ERR_PTR(-EOPNOTSUPP); |
| 1137 | } |
| 1138 | |
| 1139 | /* Always take reference to block in order to support execution |
| 1140 | * of rules update path of cls API without rtnl lock. Caller |
| 1141 | * must release block when it is finished using it. 'if' block |
| 1142 | * of this conditional obtain reference to block by calling |
| 1143 | * tcf_block_refcnt_get(). |
| 1144 | */ |
| 1145 | refcount_inc(&block->refcnt); |
| 1146 | } |
| 1147 | |
| 1148 | return block; |
| 1149 | } |
| 1150 | |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1151 | static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1152 | struct tcf_block_ext_info *ei, bool rtnl_held) |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1153 | { |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 1154 | if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) { |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1155 | /* Flushing/putting all chains will cause the block to be |
| 1156 | * deallocated when last chain is freed. However, if chain_list |
| 1157 | * is empty, block has to be manually deallocated. After block |
| 1158 | * reference counter reached 0, it is no longer possible to |
| 1159 | * increment it or add new chains to block. |
| 1160 | */ |
| 1161 | bool free_block = list_empty(&block->chain_list); |
| 1162 | |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 1163 | mutex_unlock(&block->lock); |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1164 | if (tcf_block_shared(block)) |
| 1165 | tcf_block_remove(block, block->net); |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1166 | |
| 1167 | if (q) |
| 1168 | tcf_block_offload_unbind(block, q, ei); |
| 1169 | |
| 1170 | if (free_block) |
Vlad Buslov | c266f64 | 2019-02-11 10:55:32 +0200 | [diff] [blame] | 1171 | tcf_block_destroy(block); |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1172 | else |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1173 | tcf_block_flush_all_chains(block, rtnl_held); |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1174 | } else if (q) { |
| 1175 | tcf_block_offload_unbind(block, q, ei); |
| 1176 | } |
| 1177 | } |
| 1178 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1179 | static void tcf_block_refcnt_put(struct tcf_block *block, bool rtnl_held) |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1180 | { |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1181 | __tcf_block_put(block, NULL, NULL, rtnl_held); |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 1182 | } |
| 1183 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1184 | /* Find tcf block. |
| 1185 | * Set q, parent, cl when appropriate. |
| 1186 | */ |
| 1187 | |
| 1188 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, |
| 1189 | u32 *parent, unsigned long *cl, |
| 1190 | int ifindex, u32 block_index, |
| 1191 | struct netlink_ext_ack *extack) |
| 1192 | { |
| 1193 | struct tcf_block *block; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1194 | int err = 0; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1195 | |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1196 | ASSERT_RTNL(); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1197 | |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1198 | err = __tcf_qdisc_find(net, q, parent, ifindex, true, extack); |
| 1199 | if (err) |
| 1200 | goto errout; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1201 | |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1202 | err = __tcf_qdisc_cl_find(*q, *parent, cl, ifindex, extack); |
| 1203 | if (err) |
| 1204 | goto errout_qdisc; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1205 | |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1206 | block = __tcf_block_find(net, *q, *cl, ifindex, block_index, extack); |
Dan Carpenter | af736bf | 2019-02-18 12:26:32 +0300 | [diff] [blame] | 1207 | if (IS_ERR(block)) { |
| 1208 | err = PTR_ERR(block); |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1209 | goto errout_qdisc; |
Dan Carpenter | af736bf | 2019-02-18 12:26:32 +0300 | [diff] [blame] | 1210 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1211 | |
| 1212 | return block; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1213 | |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1214 | errout_qdisc: |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1215 | if (*q) |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1216 | qdisc_put(*q); |
Vlad Buslov | 18d3eef | 2019-02-11 10:55:47 +0200 | [diff] [blame] | 1217 | errout: |
| 1218 | *q = NULL; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1219 | return ERR_PTR(err); |
| 1220 | } |
| 1221 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1222 | static void tcf_block_release(struct Qdisc *q, struct tcf_block *block, |
| 1223 | bool rtnl_held) |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1224 | { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1225 | if (!IS_ERR_OR_NULL(block)) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1226 | tcf_block_refcnt_put(block, rtnl_held); |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1227 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 1228 | if (q) { |
| 1229 | if (rtnl_held) |
| 1230 | qdisc_put(q); |
| 1231 | else |
| 1232 | qdisc_put_unlocked(q); |
| 1233 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1234 | } |
| 1235 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1236 | struct tcf_block_owner_item { |
| 1237 | struct list_head list; |
| 1238 | struct Qdisc *q; |
Pablo Neira Ayuso | 32f8c40 | 2019-07-09 22:55:41 +0200 | [diff] [blame] | 1239 | enum flow_block_binder_type binder_type; |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1240 | }; |
| 1241 | |
| 1242 | static void |
| 1243 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, |
| 1244 | struct Qdisc *q, |
Pablo Neira Ayuso | 32f8c40 | 2019-07-09 22:55:41 +0200 | [diff] [blame] | 1245 | enum flow_block_binder_type binder_type) |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1246 | { |
| 1247 | if (block->keep_dst && |
Pablo Neira Ayuso | 32f8c40 | 2019-07-09 22:55:41 +0200 | [diff] [blame] | 1248 | binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS && |
| 1249 | binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1250 | netif_keep_dst(qdisc_dev(q)); |
| 1251 | } |
| 1252 | |
| 1253 | void tcf_block_netif_keep_dst(struct tcf_block *block) |
| 1254 | { |
| 1255 | struct tcf_block_owner_item *item; |
| 1256 | |
| 1257 | block->keep_dst = true; |
| 1258 | list_for_each_entry(item, &block->owner_list, list) |
| 1259 | tcf_block_owner_netif_keep_dst(block, item->q, |
| 1260 | item->binder_type); |
| 1261 | } |
| 1262 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); |
| 1263 | |
| 1264 | static int tcf_block_owner_add(struct tcf_block *block, |
| 1265 | struct Qdisc *q, |
Pablo Neira Ayuso | 32f8c40 | 2019-07-09 22:55:41 +0200 | [diff] [blame] | 1266 | enum flow_block_binder_type binder_type) |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1267 | { |
| 1268 | struct tcf_block_owner_item *item; |
| 1269 | |
| 1270 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 1271 | if (!item) |
| 1272 | return -ENOMEM; |
| 1273 | item->q = q; |
| 1274 | item->binder_type = binder_type; |
| 1275 | list_add(&item->list, &block->owner_list); |
| 1276 | return 0; |
| 1277 | } |
| 1278 | |
| 1279 | static void tcf_block_owner_del(struct tcf_block *block, |
| 1280 | struct Qdisc *q, |
Pablo Neira Ayuso | 32f8c40 | 2019-07-09 22:55:41 +0200 | [diff] [blame] | 1281 | enum flow_block_binder_type binder_type) |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1282 | { |
| 1283 | struct tcf_block_owner_item *item; |
| 1284 | |
| 1285 | list_for_each_entry(item, &block->owner_list, list) { |
| 1286 | if (item->q == q && item->binder_type == binder_type) { |
| 1287 | list_del(&item->list); |
| 1288 | kfree(item); |
| 1289 | return; |
| 1290 | } |
| 1291 | } |
| 1292 | WARN_ON(1); |
| 1293 | } |
| 1294 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1295 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
| 1296 | struct tcf_block_ext_info *ei, |
| 1297 | struct netlink_ext_ack *extack) |
| 1298 | { |
| 1299 | struct net *net = qdisc_net(q); |
| 1300 | struct tcf_block *block = NULL; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1301 | int err; |
| 1302 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1303 | if (ei->block_index) |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1304 | /* block_index not 0 means the shared block is requested */ |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1305 | block = tcf_block_refcnt_get(net, ei->block_index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1306 | |
| 1307 | if (!block) { |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 1308 | block = tcf_block_create(net, q, ei->block_index, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1309 | if (IS_ERR(block)) |
| 1310 | return PTR_ERR(block); |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 1311 | if (tcf_block_shared(block)) { |
| 1312 | err = tcf_block_insert(block, net, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1313 | if (err) |
| 1314 | goto err_block_insert; |
| 1315 | } |
| 1316 | } |
| 1317 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1318 | err = tcf_block_owner_add(block, q, ei->binder_type); |
| 1319 | if (err) |
| 1320 | goto err_block_owner_add; |
| 1321 | |
| 1322 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); |
| 1323 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1324 | err = tcf_chain0_head_change_cb_add(block, ei, extack); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 1325 | if (err) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1326 | goto err_chain0_head_change_cb_add; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 1327 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 1328 | err = tcf_block_offload_bind(block, q, ei, extack); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 1329 | if (err) |
| 1330 | goto err_block_offload_bind; |
| 1331 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1332 | *p_block = block; |
| 1333 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1334 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 1335 | err_block_offload_bind: |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1336 | tcf_chain0_head_change_cb_del(block, ei); |
| 1337 | err_chain0_head_change_cb_add: |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1338 | tcf_block_owner_del(block, q, ei->binder_type); |
| 1339 | err_block_owner_add: |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1340 | err_block_insert: |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1341 | tcf_block_refcnt_put(block, true); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1342 | return err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1343 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1344 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 1345 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1346 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
| 1347 | { |
| 1348 | struct tcf_proto __rcu **p_filter_chain = priv; |
| 1349 | |
| 1350 | rcu_assign_pointer(*p_filter_chain, tp_head); |
| 1351 | } |
| 1352 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1353 | int tcf_block_get(struct tcf_block **p_block, |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 1354 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 1355 | struct netlink_ext_ack *extack) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1356 | { |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1357 | struct tcf_block_ext_info ei = { |
| 1358 | .chain_head_change = tcf_chain_head_change_dflt, |
| 1359 | .chain_head_change_priv = p_filter_chain, |
| 1360 | }; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1361 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1362 | WARN_ON(!p_filter_chain); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 1363 | return tcf_block_get_ext(p_block, q, &ei, extack); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1364 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1365 | EXPORT_SYMBOL(tcf_block_get); |
| 1366 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 1367 | /* XXX: Standalone actions are not allowed to jump to any chain, and bound |
Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 1368 | * actions should be all removed after flushing. |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 1369 | */ |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1370 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, |
David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 1371 | struct tcf_block_ext_info *ei) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 1372 | { |
David S. Miller | c30abd5 | 2017-12-16 22:11:55 -0500 | [diff] [blame] | 1373 | if (!block) |
| 1374 | return; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1375 | tcf_chain0_head_change_cb_del(block, ei); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 1376 | tcf_block_owner_del(block, q, ei->binder_type); |
Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 1377 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1378 | __tcf_block_put(block, q, ei, true); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1379 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1380 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 1381 | |
| 1382 | void tcf_block_put(struct tcf_block *block) |
| 1383 | { |
| 1384 | struct tcf_block_ext_info ei = {0, }; |
| 1385 | |
Jiri Pirko | 4853f12 | 2017-12-21 13:13:59 +0100 | [diff] [blame] | 1386 | if (!block) |
| 1387 | return; |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1388 | tcf_block_put_ext(block, block->q, &ei); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 1389 | } |
David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 1390 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1391 | EXPORT_SYMBOL(tcf_block_put); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 1392 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1393 | static int |
Pablo Neira Ayuso | a732331 | 2019-07-19 18:20:15 +0200 | [diff] [blame] | 1394 | tcf_block_playback_offloads(struct tcf_block *block, flow_setup_cb_t *cb, |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1395 | void *cb_priv, bool add, bool offload_in_use, |
| 1396 | struct netlink_ext_ack *extack) |
| 1397 | { |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 1398 | struct tcf_chain *chain, *chain_prev; |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 1399 | struct tcf_proto *tp, *tp_prev; |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1400 | int err; |
| 1401 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 1402 | lockdep_assert_held(&block->cb_lock); |
| 1403 | |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 1404 | for (chain = __tcf_get_next_chain(block, NULL); |
| 1405 | chain; |
| 1406 | chain_prev = chain, |
| 1407 | chain = __tcf_get_next_chain(block, chain), |
| 1408 | tcf_chain_put(chain_prev)) { |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 1409 | for (tp = __tcf_get_next_proto(chain, NULL); tp; |
| 1410 | tp_prev = tp, |
| 1411 | tp = __tcf_get_next_proto(chain, tp), |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1412 | tcf_proto_put(tp_prev, true, NULL)) { |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1413 | if (tp->ops->reoffload) { |
| 1414 | err = tp->ops->reoffload(tp, add, cb, cb_priv, |
| 1415 | extack); |
| 1416 | if (err && add) |
| 1417 | goto err_playback_remove; |
| 1418 | } else if (add && offload_in_use) { |
| 1419 | err = -EOPNOTSUPP; |
| 1420 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); |
| 1421 | goto err_playback_remove; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | return 0; |
| 1427 | |
| 1428 | err_playback_remove: |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1429 | tcf_proto_put(tp, true, NULL); |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 1430 | tcf_chain_put(chain); |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1431 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
| 1432 | extack); |
| 1433 | return err; |
| 1434 | } |
| 1435 | |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1436 | static int tcf_block_bind(struct tcf_block *block, |
| 1437 | struct flow_block_offload *bo) |
| 1438 | { |
| 1439 | struct flow_block_cb *block_cb, *next; |
| 1440 | int err, i = 0; |
| 1441 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 1442 | lockdep_assert_held(&block->cb_lock); |
| 1443 | |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1444 | list_for_each_entry(block_cb, &bo->cb_list, list) { |
| 1445 | err = tcf_block_playback_offloads(block, block_cb->cb, |
| 1446 | block_cb->cb_priv, true, |
| 1447 | tcf_block_offload_in_use(block), |
| 1448 | bo->extack); |
| 1449 | if (err) |
| 1450 | goto err_unroll; |
Vlad Buslov | c9f1447 | 2019-08-26 16:45:01 +0300 | [diff] [blame] | 1451 | if (!bo->unlocked_driver_cb) |
| 1452 | block->lockeddevcnt++; |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1453 | |
| 1454 | i++; |
| 1455 | } |
Pablo Neira Ayuso | 14bfb13 | 2019-07-19 18:20:16 +0200 | [diff] [blame] | 1456 | list_splice(&bo->cb_list, &block->flow_block.cb_list); |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1457 | |
| 1458 | return 0; |
| 1459 | |
| 1460 | err_unroll: |
| 1461 | list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { |
| 1462 | if (i-- > 0) { |
| 1463 | list_del(&block_cb->list); |
| 1464 | tcf_block_playback_offloads(block, block_cb->cb, |
| 1465 | block_cb->cb_priv, false, |
| 1466 | tcf_block_offload_in_use(block), |
| 1467 | NULL); |
Vlad Buslov | c9f1447 | 2019-08-26 16:45:01 +0300 | [diff] [blame] | 1468 | if (!bo->unlocked_driver_cb) |
| 1469 | block->lockeddevcnt--; |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1470 | } |
| 1471 | flow_block_cb_free(block_cb); |
| 1472 | } |
| 1473 | |
| 1474 | return err; |
| 1475 | } |
| 1476 | |
| 1477 | static void tcf_block_unbind(struct tcf_block *block, |
| 1478 | struct flow_block_offload *bo) |
| 1479 | { |
| 1480 | struct flow_block_cb *block_cb, *next; |
| 1481 | |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 1482 | lockdep_assert_held(&block->cb_lock); |
| 1483 | |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1484 | list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) { |
| 1485 | tcf_block_playback_offloads(block, block_cb->cb, |
| 1486 | block_cb->cb_priv, false, |
| 1487 | tcf_block_offload_in_use(block), |
| 1488 | NULL); |
| 1489 | list_del(&block_cb->list); |
| 1490 | flow_block_cb_free(block_cb); |
Vlad Buslov | c9f1447 | 2019-08-26 16:45:01 +0300 | [diff] [blame] | 1491 | if (!bo->unlocked_driver_cb) |
| 1492 | block->lockeddevcnt--; |
Pablo Neira Ayuso | 59094b1 | 2019-07-09 22:55:45 +0200 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | static int tcf_block_setup(struct tcf_block *block, |
| 1497 | struct flow_block_offload *bo) |
| 1498 | { |
| 1499 | int err; |
| 1500 | |
| 1501 | switch (bo->command) { |
| 1502 | case FLOW_BLOCK_BIND: |
| 1503 | err = tcf_block_bind(block, bo); |
| 1504 | break; |
| 1505 | case FLOW_BLOCK_UNBIND: |
| 1506 | err = 0; |
| 1507 | tcf_block_unbind(block, bo); |
| 1508 | break; |
| 1509 | default: |
| 1510 | WARN_ON_ONCE(1); |
| 1511 | err = -EOPNOTSUPP; |
| 1512 | } |
| 1513 | |
| 1514 | return err; |
| 1515 | } |
| 1516 | |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1517 | /* Main classifier routine: scans classifier chain attached |
| 1518 | * to this qdisc, (optionally) tests for protocol and asks |
| 1519 | * specific classifiers. |
| 1520 | */ |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1521 | static inline int __tcf_classify(struct sk_buff *skb, |
| 1522 | const struct tcf_proto *tp, |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1523 | const struct tcf_proto *orig_tp, |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1524 | struct tcf_result *res, |
| 1525 | bool compat_mode, |
| 1526 | u32 *last_executed_chain) |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1527 | { |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1528 | #ifdef CONFIG_NET_CLS_ACT |
| 1529 | const int max_reclassify_loop = 4; |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1530 | const struct tcf_proto *first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1531 | int limit = 0; |
| 1532 | |
| 1533 | reclassify: |
| 1534 | #endif |
| 1535 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
Cong Wang | cd0c4e7 | 2019-01-11 18:55:42 -0800 | [diff] [blame] | 1536 | __be16 protocol = tc_skb_protocol(skb); |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1537 | int err; |
| 1538 | |
| 1539 | if (tp->protocol != protocol && |
| 1540 | tp->protocol != htons(ETH_P_ALL)) |
| 1541 | continue; |
| 1542 | |
| 1543 | err = tp->classify(skb, tp, res); |
| 1544 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1545 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1546 | first_tp = orig_tp; |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1547 | *last_executed_chain = first_tp->chain->index; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1548 | goto reset; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1549 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1550 | first_tp = res->goto_tp; |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1551 | *last_executed_chain = err & TC_ACT_EXT_VAL_MASK; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1552 | goto reset; |
| 1553 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1554 | #endif |
| 1555 | if (err >= 0) |
| 1556 | return err; |
| 1557 | } |
| 1558 | |
| 1559 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 1560 | #ifdef CONFIG_NET_CLS_ACT |
| 1561 | reset: |
| 1562 | if (unlikely(limit++ >= max_reclassify_loop)) { |
Jiri Pirko | 9d3aaff | 2018-01-17 11:46:47 +0100 | [diff] [blame] | 1563 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
| 1564 | tp->chain->block->index, |
| 1565 | tp->prio & 0xffff, |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1566 | ntohs(tp->protocol)); |
| 1567 | return TC_ACT_SHOT; |
| 1568 | } |
| 1569 | |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1570 | tp = first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1571 | goto reclassify; |
| 1572 | #endif |
| 1573 | } |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1574 | |
| 1575 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 1576 | struct tcf_result *res, bool compat_mode) |
| 1577 | { |
| 1578 | u32 last_executed_chain = 0; |
| 1579 | |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1580 | return __tcf_classify(skb, tp, tp, res, compat_mode, |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1581 | &last_executed_chain); |
| 1582 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1583 | EXPORT_SYMBOL(tcf_classify); |
| 1584 | |
Paul Blakey | 7d17c54 | 2020-02-16 12:01:22 +0200 | [diff] [blame] | 1585 | int tcf_classify_ingress(struct sk_buff *skb, |
| 1586 | const struct tcf_block *ingress_block, |
| 1587 | const struct tcf_proto *tp, |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1588 | struct tcf_result *res, bool compat_mode) |
| 1589 | { |
| 1590 | #if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT) |
| 1591 | u32 last_executed_chain = 0; |
| 1592 | |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1593 | return __tcf_classify(skb, tp, tp, res, compat_mode, |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1594 | &last_executed_chain); |
| 1595 | #else |
| 1596 | u32 last_executed_chain = tp ? tp->chain->index : 0; |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1597 | const struct tcf_proto *orig_tp = tp; |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1598 | struct tc_skb_ext *ext; |
| 1599 | int ret; |
| 1600 | |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1601 | ext = skb_ext_find(skb, TC_SKB_EXT); |
| 1602 | |
| 1603 | if (ext && ext->chain) { |
| 1604 | struct tcf_chain *fchain; |
| 1605 | |
| 1606 | fchain = tcf_chain_lookup_rcu(ingress_block, ext->chain); |
| 1607 | if (!fchain) |
| 1608 | return TC_ACT_SHOT; |
| 1609 | |
| 1610 | /* Consume, so cloned/redirect skbs won't inherit ext */ |
| 1611 | skb_ext_del(skb, TC_SKB_EXT); |
| 1612 | |
| 1613 | tp = rcu_dereference_bh(fchain->filter_chain); |
Paul Blakey | a080da6 | 2020-04-06 18:36:56 +0300 | [diff] [blame] | 1614 | last_executed_chain = fchain->index; |
Paul Blakey | af69962 | 2020-02-16 12:01:24 +0200 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | ret = __tcf_classify(skb, tp, orig_tp, res, compat_mode, |
| 1618 | &last_executed_chain); |
Paul Blakey | 9410c94 | 2020-02-16 12:01:21 +0200 | [diff] [blame] | 1619 | |
| 1620 | /* If we missed on some chain */ |
| 1621 | if (ret == TC_ACT_UNSPEC && last_executed_chain) { |
| 1622 | ext = skb_ext_add(skb, TC_SKB_EXT); |
| 1623 | if (WARN_ON_ONCE(!ext)) |
| 1624 | return TC_ACT_SHOT; |
| 1625 | ext->chain = last_executed_chain; |
| 1626 | } |
| 1627 | |
| 1628 | return ret; |
| 1629 | #endif |
| 1630 | } |
| 1631 | EXPORT_SYMBOL(tcf_classify_ingress); |
| 1632 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1633 | struct tcf_chain_info { |
| 1634 | struct tcf_proto __rcu **pprev; |
| 1635 | struct tcf_proto __rcu *next; |
| 1636 | }; |
| 1637 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1638 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain, |
| 1639 | struct tcf_chain_info *chain_info) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1640 | { |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1641 | return tcf_chain_dereference(*chain_info->pprev, chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1644 | static int tcf_chain_tp_insert(struct tcf_chain *chain, |
| 1645 | struct tcf_chain_info *chain_info, |
| 1646 | struct tcf_proto *tp) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1647 | { |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1648 | if (chain->flushing) |
| 1649 | return -EAGAIN; |
| 1650 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1651 | if (*chain_info->pprev == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1652 | tcf_chain0_head_change(chain, tp); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 1653 | tcf_proto_get(tp); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1654 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info)); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1655 | rcu_assign_pointer(*chain_info->pprev, tp); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1656 | |
| 1657 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 1661 | struct tcf_chain_info *chain_info, |
| 1662 | struct tcf_proto *tp) |
| 1663 | { |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1664 | struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1665 | |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1666 | tcf_proto_mark_delete(tp); |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1667 | if (tp == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1668 | tcf_chain0_head_change(chain, next); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1669 | RCU_INIT_POINTER(*chain_info->pprev, next); |
| 1670 | } |
| 1671 | |
| 1672 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 1673 | struct tcf_chain_info *chain_info, |
| 1674 | u32 protocol, u32 prio, |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1675 | bool prio_allocate); |
| 1676 | |
| 1677 | /* Try to insert new proto. |
| 1678 | * If proto with specified priority already exists, free new proto |
| 1679 | * and return existing one. |
| 1680 | */ |
| 1681 | |
| 1682 | static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain, |
| 1683 | struct tcf_proto *tp_new, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1684 | u32 protocol, u32 prio, |
| 1685 | bool rtnl_held) |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1686 | { |
| 1687 | struct tcf_chain_info chain_info; |
| 1688 | struct tcf_proto *tp; |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1689 | int err = 0; |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1690 | |
| 1691 | mutex_lock(&chain->filter_chain_lock); |
| 1692 | |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 1693 | if (tcf_proto_exists_destroying(chain, tp_new)) { |
| 1694 | mutex_unlock(&chain->filter_chain_lock); |
| 1695 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL); |
| 1696 | return ERR_PTR(-EAGAIN); |
| 1697 | } |
| 1698 | |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1699 | tp = tcf_chain_tp_find(chain, &chain_info, |
| 1700 | protocol, prio, false); |
| 1701 | if (!tp) |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1702 | err = tcf_chain_tp_insert(chain, &chain_info, tp_new); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1703 | mutex_unlock(&chain->filter_chain_lock); |
| 1704 | |
| 1705 | if (tp) { |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 1706 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1707 | tp_new = tp; |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1708 | } else if (err) { |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 1709 | tcf_proto_destroy(tp_new, rtnl_held, false, NULL); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 1710 | tp_new = ERR_PTR(err); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | return tp_new; |
| 1714 | } |
| 1715 | |
| 1716 | static void tcf_chain_tp_delete_empty(struct tcf_chain *chain, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1717 | struct tcf_proto *tp, bool rtnl_held, |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1718 | struct netlink_ext_ack *extack) |
| 1719 | { |
| 1720 | struct tcf_chain_info chain_info; |
| 1721 | struct tcf_proto *tp_iter; |
| 1722 | struct tcf_proto **pprev; |
| 1723 | struct tcf_proto *next; |
| 1724 | |
| 1725 | mutex_lock(&chain->filter_chain_lock); |
| 1726 | |
| 1727 | /* Atomically find and remove tp from chain. */ |
| 1728 | for (pprev = &chain->filter_chain; |
| 1729 | (tp_iter = tcf_chain_dereference(*pprev, chain)); |
| 1730 | pprev = &tp_iter->next) { |
| 1731 | if (tp_iter == tp) { |
| 1732 | chain_info.pprev = pprev; |
| 1733 | chain_info.next = tp_iter->next; |
| 1734 | WARN_ON(tp_iter->deleting); |
| 1735 | break; |
| 1736 | } |
| 1737 | } |
| 1738 | /* Verify that tp still exists and no new filters were inserted |
| 1739 | * concurrently. |
| 1740 | * Mark tp for deletion if it is empty. |
| 1741 | */ |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 1742 | if (!tp_iter || !tcf_proto_check_delete(tp)) { |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1743 | mutex_unlock(&chain->filter_chain_lock); |
| 1744 | return; |
| 1745 | } |
| 1746 | |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 1747 | tcf_proto_signal_destroying(chain, tp); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1748 | next = tcf_chain_dereference(chain_info.next, chain); |
| 1749 | if (tp == chain->filter_chain) |
| 1750 | tcf_chain0_head_change(chain, next); |
| 1751 | RCU_INIT_POINTER(*chain_info.pprev, next); |
| 1752 | mutex_unlock(&chain->filter_chain_lock); |
| 1753 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1754 | tcf_proto_put(tp, rtnl_held, extack); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 1758 | struct tcf_chain_info *chain_info, |
| 1759 | u32 protocol, u32 prio, |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1760 | bool prio_allocate) |
| 1761 | { |
| 1762 | struct tcf_proto **pprev; |
| 1763 | struct tcf_proto *tp; |
| 1764 | |
| 1765 | /* Check the chain for existence of proto-tcf with this priority */ |
| 1766 | for (pprev = &chain->filter_chain; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 1767 | (tp = tcf_chain_dereference(*pprev, chain)); |
| 1768 | pprev = &tp->next) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1769 | if (tp->prio >= prio) { |
| 1770 | if (tp->prio == prio) { |
| 1771 | if (prio_allocate || |
| 1772 | (tp->protocol != protocol && protocol)) |
| 1773 | return ERR_PTR(-EINVAL); |
| 1774 | } else { |
| 1775 | tp = NULL; |
| 1776 | } |
| 1777 | break; |
| 1778 | } |
| 1779 | } |
| 1780 | chain_info->pprev = pprev; |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 1781 | if (tp) { |
| 1782 | chain_info->next = tp->next; |
| 1783 | tcf_proto_get(tp); |
| 1784 | } else { |
| 1785 | chain_info->next = NULL; |
| 1786 | } |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1787 | return tp; |
| 1788 | } |
| 1789 | |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1790 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1791 | struct tcf_proto *tp, struct tcf_block *block, |
| 1792 | struct Qdisc *q, u32 parent, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1793 | u32 portid, u32 seq, u16 flags, int event, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 1794 | bool terse_dump, bool rtnl_held) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1795 | { |
| 1796 | struct tcmsg *tcm; |
| 1797 | struct nlmsghdr *nlh; |
| 1798 | unsigned char *b = skb_tail_pointer(skb); |
| 1799 | |
| 1800 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1801 | if (!nlh) |
| 1802 | goto out_nlmsg_trim; |
| 1803 | tcm = nlmsg_data(nlh); |
| 1804 | tcm->tcm_family = AF_UNSPEC; |
| 1805 | tcm->tcm__pad1 = 0; |
| 1806 | tcm->tcm__pad2 = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1807 | if (q) { |
| 1808 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 1809 | tcm->tcm_parent = parent; |
| 1810 | } else { |
| 1811 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1812 | tcm->tcm_block_index = block->index; |
| 1813 | } |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1814 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 1815 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 1816 | goto nla_put_failure; |
| 1817 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 1818 | goto nla_put_failure; |
| 1819 | if (!fh) { |
| 1820 | tcm->tcm_handle = 0; |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 1821 | } else if (terse_dump) { |
| 1822 | if (tp->ops->terse_dump) { |
| 1823 | if (tp->ops->terse_dump(net, tp, fh, skb, tcm, |
| 1824 | rtnl_held) < 0) |
| 1825 | goto nla_put_failure; |
| 1826 | } else { |
| 1827 | goto cls_op_not_supp; |
| 1828 | } |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1829 | } else { |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1830 | if (tp->ops->dump && |
| 1831 | tp->ops->dump(net, tp, fh, skb, tcm, rtnl_held) < 0) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1832 | goto nla_put_failure; |
| 1833 | } |
| 1834 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1835 | return skb->len; |
| 1836 | |
| 1837 | out_nlmsg_trim: |
| 1838 | nla_put_failure: |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 1839 | cls_op_not_supp: |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1840 | nlmsg_trim(skb, b); |
| 1841 | return -1; |
| 1842 | } |
| 1843 | |
| 1844 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 1845 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1846 | struct tcf_block *block, struct Qdisc *q, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1847 | u32 parent, void *fh, int event, bool unicast, |
| 1848 | bool rtnl_held) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1849 | { |
| 1850 | struct sk_buff *skb; |
| 1851 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 1852 | int err = 0; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1853 | |
| 1854 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1855 | if (!skb) |
| 1856 | return -ENOBUFS; |
| 1857 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1858 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1859 | n->nlmsg_seq, n->nlmsg_flags, event, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 1860 | false, rtnl_held) <= 0) { |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1861 | kfree_skb(skb); |
| 1862 | return -EINVAL; |
| 1863 | } |
| 1864 | |
| 1865 | if (unicast) |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 1866 | err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1867 | else |
| 1868 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1869 | n->nlmsg_flags & NLM_F_ECHO); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1870 | |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 1871 | if (err > 0) |
| 1872 | err = 0; |
| 1873 | return err; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 1877 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1878 | struct tcf_block *block, struct Qdisc *q, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1879 | u32 parent, void *fh, bool unicast, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1880 | bool rtnl_held, struct netlink_ext_ack *extack) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1881 | { |
| 1882 | struct sk_buff *skb; |
| 1883 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1884 | int err; |
| 1885 | |
| 1886 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1887 | if (!skb) |
| 1888 | return -ENOBUFS; |
| 1889 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1890 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1891 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 1892 | false, rtnl_held) <= 0) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1893 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1894 | kfree_skb(skb); |
| 1895 | return -EINVAL; |
| 1896 | } |
| 1897 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1898 | err = tp->ops->delete(tp, fh, last, rtnl_held, extack); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1899 | if (err) { |
| 1900 | kfree_skb(skb); |
| 1901 | return err; |
| 1902 | } |
| 1903 | |
| 1904 | if (unicast) |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 1905 | err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1906 | else |
| 1907 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1908 | n->nlmsg_flags & NLM_F_ECHO); |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1909 | if (err < 0) |
| 1910 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 1911 | |
| 1912 | if (err > 0) |
| 1913 | err = 0; |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1914 | return err; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1918 | struct tcf_block *block, struct Qdisc *q, |
| 1919 | u32 parent, struct nlmsghdr *n, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1920 | struct tcf_chain *chain, int event, |
| 1921 | bool rtnl_held) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1922 | { |
| 1923 | struct tcf_proto *tp; |
| 1924 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1925 | for (tp = tcf_get_next_proto(chain, NULL, rtnl_held); |
| 1926 | tp; tp = tcf_get_next_proto(chain, tp, rtnl_held)) |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1927 | tfilter_notify(net, oskb, n, tp, block, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1928 | q, parent, NULL, event, false, rtnl_held); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 1931 | static void tfilter_put(struct tcf_proto *tp, void *fh) |
| 1932 | { |
| 1933 | if (tp->ops->put && fh) |
| 1934 | tp->ops->put(tp, fh); |
| 1935 | } |
| 1936 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1937 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1938 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1940 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1941 | struct nlattr *tca[TCA_MAX + 1]; |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 1942 | char name[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | struct tcmsg *t; |
| 1944 | u32 protocol; |
| 1945 | u32 prio; |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1946 | bool prio_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1948 | u32 chain_index; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1949 | struct Qdisc *q = NULL; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1950 | struct tcf_chain_info chain_info; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1951 | struct tcf_chain *chain = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1952 | struct tcf_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | struct tcf_proto *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | unsigned long cl; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1955 | void *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | int err; |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1957 | int tp_created; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 1958 | bool rtnl_held = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1960 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1961 | return -EPERM; |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | replay: |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1964 | tp_created = 0; |
| 1965 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1966 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, |
| 1967 | rtm_tca_policy, extack); |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1968 | if (err < 0) |
| 1969 | return err; |
| 1970 | |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1971 | t = nlmsg_data(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | protocol = TC_H_MIN(t->tcm_info); |
| 1973 | prio = TC_H_MAJ(t->tcm_info); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1974 | prio_allocate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | parent = t->tcm_parent; |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 1976 | tp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | cl = 0; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 1978 | block = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | |
| 1980 | if (prio == 0) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1981 | /* If no priority is provided by the user, |
| 1982 | * we allocate one. |
| 1983 | */ |
| 1984 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 1985 | prio = TC_H_MAKE(0x80000000U, 0U); |
| 1986 | prio_allocate = true; |
| 1987 | } else { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1988 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | return -ENOENT; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* Find head of filter chain. */ |
| 1994 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 1995 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); |
| 1996 | if (err) |
| 1997 | return err; |
| 1998 | |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 1999 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) { |
| 2000 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); |
| 2001 | err = -EINVAL; |
| 2002 | goto errout; |
| 2003 | } |
| 2004 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2005 | /* Take rtnl mutex if rtnl_held was set to true on previous iteration, |
| 2006 | * block is shared (no qdisc found), qdisc is not unlocked, classifier |
| 2007 | * type is not specified, classifier is not unlocked. |
| 2008 | */ |
| 2009 | if (rtnl_held || |
| 2010 | (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2011 | !tcf_proto_is_unlocked(name)) { |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2012 | rtnl_held = true; |
| 2013 | rtnl_lock(); |
| 2014 | } |
| 2015 | |
| 2016 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); |
| 2017 | if (err) |
| 2018 | goto errout; |
| 2019 | |
| 2020 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, |
| 2021 | extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2022 | if (IS_ERR(block)) { |
| 2023 | err = PTR_ERR(block); |
| 2024 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2025 | } |
Cong Wang | a7df487 | 2020-04-30 20:53:49 -0700 | [diff] [blame] | 2026 | block->classid = parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2027 | |
| 2028 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 2029 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 2030 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2031 | err = -EINVAL; |
| 2032 | goto errout; |
| 2033 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2034 | chain = tcf_chain_get(block, chain_index, true); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2035 | if (!chain) { |
Jiri Pirko | d5ed72a | 2018-08-27 20:58:43 +0200 | [diff] [blame] | 2036 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2037 | err = -ENOMEM; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 2038 | goto errout; |
| 2039 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2041 | mutex_lock(&chain->filter_chain_lock); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 2042 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 2043 | prio, prio_allocate); |
| 2044 | if (IS_ERR(tp)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 2045 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 2046 | err = PTR_ERR(tp); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2047 | goto errout_locked; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | if (tp == NULL) { |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 2051 | struct tcf_proto *tp_new = NULL; |
| 2052 | |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 2053 | if (chain->flushing) { |
| 2054 | err = -EAGAIN; |
| 2055 | goto errout_locked; |
| 2056 | } |
| 2057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | /* Proto-tcf does not exist, create new one */ |
| 2059 | |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2060 | if (tca[TCA_KIND] == NULL || !protocol) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 2061 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2062 | err = -EINVAL; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2063 | goto errout_locked; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2066 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 2067 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2068 | err = -ENOENT; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2069 | goto errout_locked; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2070 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 2072 | if (prio_allocate) |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2073 | prio = tcf_auto_prio(tcf_chain_tp_prev(chain, |
| 2074 | &chain_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2076 | mutex_unlock(&chain->filter_chain_lock); |
Eric Dumazet | 36d79af | 2020-01-21 11:02:20 -0800 | [diff] [blame] | 2077 | tp_new = tcf_proto_create(name, protocol, prio, chain, |
| 2078 | rtnl_held, extack); |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 2079 | if (IS_ERR(tp_new)) { |
| 2080 | err = PTR_ERR(tp_new); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 2081 | goto errout_tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2083 | |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 2084 | tp_created = 1; |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2085 | tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio, |
| 2086 | rtnl_held); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 2087 | if (IS_ERR(tp)) { |
| 2088 | err = PTR_ERR(tp); |
| 2089 | goto errout_tp; |
| 2090 | } |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2091 | } else { |
| 2092 | mutex_unlock(&chain->filter_chain_lock); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 2095 | if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 2096 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 2097 | err = -EINVAL; |
| 2098 | goto errout; |
| 2099 | } |
| 2100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | fh = tp->ops->get(tp, t->tcm_handle); |
| 2102 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 2103 | if (!fh) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2104 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 2105 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2106 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 2108 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2109 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 2110 | tfilter_put(tp, fh); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2111 | NL_SET_ERR_MSG(extack, "Filter already exists"); |
| 2112 | err = -EEXIST; |
| 2113 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2116 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { |
| 2117 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); |
| 2118 | err = -EINVAL; |
| 2119 | goto errout; |
| 2120 | } |
| 2121 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 2122 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
Alexander Aring | 7306db3 | 2018-01-18 11:20:51 -0500 | [diff] [blame] | 2123 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2124 | rtnl_held, extack); |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 2125 | if (err == 0) { |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2126 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2127 | RTM_NEWTFILTER, false, rtnl_held); |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 2128 | tfilter_put(tp, fh); |
Vlad Buslov | 503d81d | 2019-07-21 17:44:12 +0300 | [diff] [blame] | 2129 | /* q pointer is NULL for shared blocks */ |
| 2130 | if (q) |
| 2131 | q->flags &= ~TCQ_F_CAN_BYPASS; |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 2132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | |
| 2134 | errout: |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 2135 | if (err && tp_created) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2136 | tcf_chain_tp_delete_empty(chain, tp, rtnl_held, NULL); |
Vlad Buslov | 726d0612 | 2019-02-11 10:55:42 +0200 | [diff] [blame] | 2137 | errout_tp: |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2138 | if (chain) { |
| 2139 | if (tp && !IS_ERR(tp)) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2140 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2141 | if (!tp_created) |
| 2142 | tcf_chain_put(chain); |
| 2143 | } |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2144 | tcf_block_release(q, block, rtnl_held); |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2145 | |
| 2146 | if (rtnl_held) |
| 2147 | rtnl_unlock(); |
| 2148 | |
| 2149 | if (err == -EAGAIN) { |
| 2150 | /* Take rtnl lock in case EAGAIN is caused by concurrent flush |
| 2151 | * of target chain. |
| 2152 | */ |
| 2153 | rtnl_held = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* Replay the request. */ |
| 2155 | goto replay; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2156 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | return err; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2158 | |
| 2159 | errout_locked: |
| 2160 | mutex_unlock(&chain->filter_chain_lock); |
| 2161 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2164 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 2165 | struct netlink_ext_ack *extack) |
| 2166 | { |
| 2167 | struct net *net = sock_net(skb->sk); |
| 2168 | struct nlattr *tca[TCA_MAX + 1]; |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2169 | char name[IFNAMSIZ]; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2170 | struct tcmsg *t; |
| 2171 | u32 protocol; |
| 2172 | u32 prio; |
| 2173 | u32 parent; |
| 2174 | u32 chain_index; |
| 2175 | struct Qdisc *q = NULL; |
| 2176 | struct tcf_chain_info chain_info; |
| 2177 | struct tcf_chain *chain = NULL; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2178 | struct tcf_block *block = NULL; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2179 | struct tcf_proto *tp = NULL; |
| 2180 | unsigned long cl = 0; |
| 2181 | void *fh = NULL; |
| 2182 | int err; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2183 | bool rtnl_held = false; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2184 | |
| 2185 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 2186 | return -EPERM; |
| 2187 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2188 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, |
| 2189 | rtm_tca_policy, extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2190 | if (err < 0) |
| 2191 | return err; |
| 2192 | |
| 2193 | t = nlmsg_data(n); |
| 2194 | protocol = TC_H_MIN(t->tcm_info); |
| 2195 | prio = TC_H_MAJ(t->tcm_info); |
| 2196 | parent = t->tcm_parent; |
| 2197 | |
| 2198 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { |
| 2199 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); |
| 2200 | return -ENOENT; |
| 2201 | } |
| 2202 | |
| 2203 | /* Find head of filter chain. */ |
| 2204 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2205 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); |
| 2206 | if (err) |
| 2207 | return err; |
| 2208 | |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2209 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) { |
| 2210 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); |
| 2211 | err = -EINVAL; |
| 2212 | goto errout; |
| 2213 | } |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2214 | /* Take rtnl mutex if flushing whole chain, block is shared (no qdisc |
| 2215 | * found), qdisc is not unlocked, classifier type is not specified, |
| 2216 | * classifier is not unlocked. |
| 2217 | */ |
| 2218 | if (!prio || |
| 2219 | (q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2220 | !tcf_proto_is_unlocked(name)) { |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2221 | rtnl_held = true; |
| 2222 | rtnl_lock(); |
| 2223 | } |
| 2224 | |
| 2225 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); |
| 2226 | if (err) |
| 2227 | goto errout; |
| 2228 | |
| 2229 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, |
| 2230 | extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2231 | if (IS_ERR(block)) { |
| 2232 | err = PTR_ERR(block); |
| 2233 | goto errout; |
| 2234 | } |
| 2235 | |
| 2236 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 2237 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 2238 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 2239 | err = -EINVAL; |
| 2240 | goto errout; |
| 2241 | } |
| 2242 | chain = tcf_chain_get(block, chain_index, false); |
| 2243 | if (!chain) { |
Jiri Pirko | 5ca8a25 | 2018-08-03 11:08:47 +0200 | [diff] [blame] | 2244 | /* User requested flush on non-existent chain. Nothing to do, |
| 2245 | * so just return success. |
| 2246 | */ |
| 2247 | if (prio == 0) { |
| 2248 | err = 0; |
| 2249 | goto errout; |
| 2250 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2251 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
Jiri Pirko | b7b4247 | 2018-08-27 20:58:44 +0200 | [diff] [blame] | 2252 | err = -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2253 | goto errout; |
| 2254 | } |
| 2255 | |
| 2256 | if (prio == 0) { |
| 2257 | tfilter_notify_chain(net, skb, block, q, parent, n, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2258 | chain, RTM_DELTFILTER, rtnl_held); |
| 2259 | tcf_chain_flush(chain, rtnl_held); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2260 | err = 0; |
| 2261 | goto errout; |
| 2262 | } |
| 2263 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2264 | mutex_lock(&chain->filter_chain_lock); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2265 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 2266 | prio, false); |
| 2267 | if (!tp || IS_ERR(tp)) { |
| 2268 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 2269 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2270 | goto errout_locked; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2271 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 2272 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 2273 | err = -EINVAL; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2274 | goto errout_locked; |
| 2275 | } else if (t->tcm_handle == 0) { |
John Hurley | 59eb87c | 2019-11-02 14:17:47 +0000 | [diff] [blame] | 2276 | tcf_proto_signal_destroying(chain, tp); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2277 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 2278 | mutex_unlock(&chain->filter_chain_lock); |
| 2279 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2280 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2281 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2282 | RTM_DELTFILTER, false, rtnl_held); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2283 | err = 0; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2284 | goto errout; |
| 2285 | } |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2286 | mutex_unlock(&chain->filter_chain_lock); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2287 | |
| 2288 | fh = tp->ops->get(tp, t->tcm_handle); |
| 2289 | |
| 2290 | if (!fh) { |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2291 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 2292 | err = -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2293 | } else { |
| 2294 | bool last; |
| 2295 | |
| 2296 | err = tfilter_del_notify(net, skb, n, tp, block, |
| 2297 | q, parent, fh, false, &last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2298 | rtnl_held, extack); |
| 2299 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2300 | if (err) |
| 2301 | goto errout; |
Vlad Buslov | 8b64678 | 2019-02-11 10:55:41 +0200 | [diff] [blame] | 2302 | if (last) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2303 | tcf_chain_tp_delete_empty(chain, tp, rtnl_held, extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | errout: |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2307 | if (chain) { |
| 2308 | if (tp && !IS_ERR(tp)) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2309 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2310 | tcf_chain_put(chain); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2311 | } |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2312 | tcf_block_release(q, block, rtnl_held); |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2313 | |
| 2314 | if (rtnl_held) |
| 2315 | rtnl_unlock(); |
| 2316 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2317 | return err; |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2318 | |
| 2319 | errout_locked: |
| 2320 | mutex_unlock(&chain->filter_chain_lock); |
| 2321 | goto errout; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 2325 | struct netlink_ext_ack *extack) |
| 2326 | { |
| 2327 | struct net *net = sock_net(skb->sk); |
| 2328 | struct nlattr *tca[TCA_MAX + 1]; |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2329 | char name[IFNAMSIZ]; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2330 | struct tcmsg *t; |
| 2331 | u32 protocol; |
| 2332 | u32 prio; |
| 2333 | u32 parent; |
| 2334 | u32 chain_index; |
| 2335 | struct Qdisc *q = NULL; |
| 2336 | struct tcf_chain_info chain_info; |
| 2337 | struct tcf_chain *chain = NULL; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2338 | struct tcf_block *block = NULL; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2339 | struct tcf_proto *tp = NULL; |
| 2340 | unsigned long cl = 0; |
| 2341 | void *fh = NULL; |
| 2342 | int err; |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2343 | bool rtnl_held = false; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2344 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2345 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, |
| 2346 | rtm_tca_policy, extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2347 | if (err < 0) |
| 2348 | return err; |
| 2349 | |
| 2350 | t = nlmsg_data(n); |
| 2351 | protocol = TC_H_MIN(t->tcm_info); |
| 2352 | prio = TC_H_MAJ(t->tcm_info); |
| 2353 | parent = t->tcm_parent; |
| 2354 | |
| 2355 | if (prio == 0) { |
| 2356 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| 2357 | return -ENOENT; |
| 2358 | } |
| 2359 | |
| 2360 | /* Find head of filter chain. */ |
| 2361 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2362 | err = __tcf_qdisc_find(net, &q, &parent, t->tcm_ifindex, false, extack); |
| 2363 | if (err) |
| 2364 | return err; |
| 2365 | |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2366 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) { |
| 2367 | NL_SET_ERR_MSG(extack, "Specified TC filter name too long"); |
| 2368 | err = -EINVAL; |
| 2369 | goto errout; |
| 2370 | } |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2371 | /* Take rtnl mutex if block is shared (no qdisc found), qdisc is not |
| 2372 | * unlocked, classifier type is not specified, classifier is not |
| 2373 | * unlocked. |
| 2374 | */ |
| 2375 | if ((q && !(q->ops->cl_ops->flags & QDISC_CLASS_OPS_DOIT_UNLOCKED)) || |
Cong Wang | 6f96c3c | 2019-10-07 13:26:28 -0700 | [diff] [blame] | 2376 | !tcf_proto_is_unlocked(name)) { |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2377 | rtnl_held = true; |
| 2378 | rtnl_lock(); |
| 2379 | } |
| 2380 | |
| 2381 | err = __tcf_qdisc_cl_find(q, parent, &cl, t->tcm_ifindex, extack); |
| 2382 | if (err) |
| 2383 | goto errout; |
| 2384 | |
| 2385 | block = __tcf_block_find(net, q, cl, t->tcm_ifindex, t->tcm_block_index, |
| 2386 | extack); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2387 | if (IS_ERR(block)) { |
| 2388 | err = PTR_ERR(block); |
| 2389 | goto errout; |
| 2390 | } |
| 2391 | |
| 2392 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 2393 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 2394 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 2395 | err = -EINVAL; |
| 2396 | goto errout; |
| 2397 | } |
| 2398 | chain = tcf_chain_get(block, chain_index, false); |
| 2399 | if (!chain) { |
| 2400 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 2401 | err = -EINVAL; |
| 2402 | goto errout; |
| 2403 | } |
| 2404 | |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2405 | mutex_lock(&chain->filter_chain_lock); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2406 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 2407 | prio, false); |
Vlad Buslov | ed76f5e | 2019-02-11 10:55:38 +0200 | [diff] [blame] | 2408 | mutex_unlock(&chain->filter_chain_lock); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2409 | if (!tp || IS_ERR(tp)) { |
| 2410 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 2411 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2412 | goto errout; |
| 2413 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 2414 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 2415 | err = -EINVAL; |
| 2416 | goto errout; |
| 2417 | } |
| 2418 | |
| 2419 | fh = tp->ops->get(tp, t->tcm_handle); |
| 2420 | |
| 2421 | if (!fh) { |
| 2422 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 2423 | err = -ENOENT; |
| 2424 | } else { |
| 2425 | err = tfilter_notify(net, skb, n, tp, block, q, parent, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2426 | fh, RTM_NEWTFILTER, true, rtnl_held); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2427 | if (err < 0) |
| 2428 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); |
| 2429 | } |
| 2430 | |
Vlad Buslov | 7d5509f | 2019-02-11 10:55:44 +0200 | [diff] [blame] | 2431 | tfilter_put(tp, fh); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2432 | errout: |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2433 | if (chain) { |
| 2434 | if (tp && !IS_ERR(tp)) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2435 | tcf_proto_put(tp, rtnl_held, NULL); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2436 | tcf_chain_put(chain); |
Vlad Buslov | 4dbfa76 | 2019-02-11 10:55:39 +0200 | [diff] [blame] | 2437 | } |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2438 | tcf_block_release(q, block, rtnl_held); |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 2439 | |
| 2440 | if (rtnl_held) |
| 2441 | rtnl_unlock(); |
| 2442 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2443 | return err; |
| 2444 | } |
| 2445 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2446 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | struct tcf_walker w; |
| 2448 | struct sk_buff *skb; |
| 2449 | struct netlink_callback *cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2450 | struct tcf_block *block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2451 | struct Qdisc *q; |
| 2452 | u32 parent; |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2453 | bool terse_dump; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | }; |
| 2455 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 2456 | static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2458 | struct tcf_dump_args *a = (void *)arg; |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 2459 | struct net *net = sock_net(a->skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2461 | return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2462 | n, NETLINK_CB(a->cb->skb).portid, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 2463 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2464 | RTM_NEWTFILTER, a->terse_dump, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2467 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 2468 | struct sk_buff *skb, struct netlink_callback *cb, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2469 | long index_start, long *p_index, bool terse) |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2470 | { |
| 2471 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2472 | struct tcf_block *block = chain->block; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2473 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2474 | struct tcf_proto *tp, *tp_prev; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2475 | struct tcf_dump_args arg; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2476 | |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2477 | for (tp = __tcf_get_next_proto(chain, NULL); |
| 2478 | tp; |
| 2479 | tp_prev = tp, |
| 2480 | tp = __tcf_get_next_proto(chain, tp), |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2481 | tcf_proto_put(tp_prev, true, NULL), |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2482 | (*p_index)++) { |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2483 | if (*p_index < index_start) |
| 2484 | continue; |
| 2485 | if (TC_H_MAJ(tcm->tcm_info) && |
| 2486 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 2487 | continue; |
| 2488 | if (TC_H_MIN(tcm->tcm_info) && |
| 2489 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 2490 | continue; |
| 2491 | if (*p_index > index_start) |
| 2492 | memset(&cb->args[1], 0, |
| 2493 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 2494 | if (cb->args[1] == 0) { |
YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 2495 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2496 | NETLINK_CB(cb->skb).portid, |
| 2497 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2498 | RTM_NEWTFILTER, false, true) <= 0) |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2499 | goto errout; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2500 | cb->args[1] = 1; |
| 2501 | } |
| 2502 | if (!tp->ops->walk) |
| 2503 | continue; |
| 2504 | arg.w.fn = tcf_node_dump; |
| 2505 | arg.skb = skb; |
| 2506 | arg.cb = cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2507 | arg.block = block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2508 | arg.q = q; |
| 2509 | arg.parent = parent; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2510 | arg.w.stop = 0; |
| 2511 | arg.w.skip = cb->args[1] - 1; |
| 2512 | arg.w.count = 0; |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2513 | arg.w.cookie = cb->args[2]; |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2514 | arg.terse_dump = terse; |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2515 | tp->ops->walk(tp, &arg.w, true); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2516 | cb->args[2] = arg.w.cookie; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2517 | cb->args[1] = arg.w.count + 1; |
| 2518 | if (arg.w.stop) |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2519 | goto errout; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2520 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2521 | return true; |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2522 | |
| 2523 | errout: |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2524 | tcf_proto_put(tp, true, NULL); |
Vlad Buslov | fe2923a | 2019-02-11 10:55:40 +0200 | [diff] [blame] | 2525 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2526 | } |
| 2527 | |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2528 | static const struct nla_policy tcf_tfilter_dump_policy[TCA_MAX + 1] = { |
| 2529 | [TCA_DUMP_FLAGS] = NLA_POLICY_BITFIELD32(TCA_DUMP_FLAGS_TERSE), |
| 2530 | }; |
| 2531 | |
Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 2532 | /* called with RTNL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 2534 | { |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 2535 | struct tcf_chain *chain, *chain_prev; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2536 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2537 | struct nlattr *tca[TCA_MAX + 1]; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2538 | struct Qdisc *q = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 2539 | struct tcf_block *block; |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 2540 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2541 | bool terse_dump = false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2542 | long index_start; |
| 2543 | long index; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2544 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2545 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 2547 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | return skb->len; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2549 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2550 | err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2551 | tcf_tfilter_dump_policy, cb->extack); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2552 | if (err) |
| 2553 | return err; |
| 2554 | |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2555 | if (tca[TCA_DUMP_FLAGS]) { |
| 2556 | struct nla_bitfield32 flags = |
| 2557 | nla_get_bitfield32(tca[TCA_DUMP_FLAGS]); |
| 2558 | |
| 2559 | terse_dump = flags.value & TCA_DUMP_FLAGS_TERSE; |
| 2560 | } |
| 2561 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2562 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 2563 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2564 | if (!block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 2565 | goto out; |
Jiri Pirko | d680b35 | 2018-01-18 16:14:49 +0100 | [diff] [blame] | 2566 | /* If we work with block index, q is NULL and parent value |
| 2567 | * will never be used in the following code. The check |
| 2568 | * in tcf_fill_node prevents it. However, compiler does not |
| 2569 | * see that far, so set parent to zero to silence the warning |
| 2570 | * about parent being uninitialized. |
| 2571 | */ |
| 2572 | parent = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2573 | } else { |
| 2574 | const struct Qdisc_class_ops *cops; |
| 2575 | struct net_device *dev; |
| 2576 | unsigned long cl = 0; |
| 2577 | |
| 2578 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 2579 | if (!dev) |
| 2580 | return skb->len; |
| 2581 | |
| 2582 | parent = tcm->tcm_parent; |
Cong Wang | a7df487 | 2020-04-30 20:53:49 -0700 | [diff] [blame] | 2583 | if (!parent) |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2584 | q = dev->qdisc; |
Cong Wang | a7df487 | 2020-04-30 20:53:49 -0700 | [diff] [blame] | 2585 | else |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2586 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2587 | if (!q) |
| 2588 | goto out; |
| 2589 | cops = q->ops->cl_ops; |
| 2590 | if (!cops) |
| 2591 | goto out; |
| 2592 | if (!cops->tcf_block) |
| 2593 | goto out; |
| 2594 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 2595 | cl = cops->find(q, tcm->tcm_parent); |
| 2596 | if (cl == 0) |
| 2597 | goto out; |
| 2598 | } |
| 2599 | block = cops->tcf_block(q, cl, NULL); |
| 2600 | if (!block) |
| 2601 | goto out; |
Cong Wang | a7df487 | 2020-04-30 20:53:49 -0700 | [diff] [blame] | 2602 | parent = block->classid; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 2603 | if (tcf_block_shared(block)) |
| 2604 | q = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2607 | index_start = cb->args[0]; |
| 2608 | index = 0; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2609 | |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 2610 | for (chain = __tcf_get_next_chain(block, NULL); |
| 2611 | chain; |
| 2612 | chain_prev = chain, |
| 2613 | chain = __tcf_get_next_chain(block, chain), |
| 2614 | tcf_chain_put(chain_prev)) { |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2615 | if (tca[TCA_CHAIN] && |
| 2616 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 2617 | continue; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 2618 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
Vlad Buslov | f8ab180 | 2020-05-15 14:40:11 +0300 | [diff] [blame] | 2619 | index_start, &index, terse_dump)) { |
Vlad Buslov | bbf7383 | 2019-02-11 10:55:36 +0200 | [diff] [blame] | 2620 | tcf_chain_put(chain); |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 2621 | err = -EMSGSIZE; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2622 | break; |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 2623 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 2624 | } |
| 2625 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 2626 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2627 | tcf_block_refcnt_put(block, true); |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 2628 | cb->args[0] = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | out: |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 2631 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 2632 | if (skb->len == 0 && err) |
| 2633 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | return skb->len; |
| 2635 | } |
| 2636 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2637 | static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops, |
| 2638 | void *tmplt_priv, u32 chain_index, |
| 2639 | struct net *net, struct sk_buff *skb, |
| 2640 | struct tcf_block *block, |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2641 | u32 portid, u32 seq, u16 flags, int event) |
| 2642 | { |
| 2643 | unsigned char *b = skb_tail_pointer(skb); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2644 | const struct tcf_proto_ops *ops; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2645 | struct nlmsghdr *nlh; |
| 2646 | struct tcmsg *tcm; |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2647 | void *priv; |
| 2648 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2649 | ops = tmplt_ops; |
| 2650 | priv = tmplt_priv; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2651 | |
| 2652 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 2653 | if (!nlh) |
| 2654 | goto out_nlmsg_trim; |
| 2655 | tcm = nlmsg_data(nlh); |
| 2656 | tcm->tcm_family = AF_UNSPEC; |
| 2657 | tcm->tcm__pad1 = 0; |
| 2658 | tcm->tcm__pad2 = 0; |
| 2659 | tcm->tcm_handle = 0; |
| 2660 | if (block->q) { |
| 2661 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; |
| 2662 | tcm->tcm_parent = block->q->handle; |
| 2663 | } else { |
| 2664 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 2665 | tcm->tcm_block_index = block->index; |
| 2666 | } |
| 2667 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2668 | if (nla_put_u32(skb, TCA_CHAIN, chain_index)) |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2669 | goto nla_put_failure; |
| 2670 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2671 | if (ops) { |
| 2672 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
| 2673 | goto nla_put_failure; |
| 2674 | if (ops->tmplt_dump(skb, net, priv) < 0) |
| 2675 | goto nla_put_failure; |
| 2676 | } |
| 2677 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2678 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 2679 | return skb->len; |
| 2680 | |
| 2681 | out_nlmsg_trim: |
| 2682 | nla_put_failure: |
| 2683 | nlmsg_trim(skb, b); |
| 2684 | return -EMSGSIZE; |
| 2685 | } |
| 2686 | |
| 2687 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 2688 | u32 seq, u16 flags, int event, bool unicast) |
| 2689 | { |
| 2690 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 2691 | struct tcf_block *block = chain->block; |
| 2692 | struct net *net = block->net; |
| 2693 | struct sk_buff *skb; |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 2694 | int err = 0; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2695 | |
| 2696 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 2697 | if (!skb) |
| 2698 | return -ENOBUFS; |
| 2699 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2700 | if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, |
| 2701 | chain->index, net, skb, block, portid, |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2702 | seq, flags, event) <= 0) { |
| 2703 | kfree_skb(skb); |
| 2704 | return -EINVAL; |
| 2705 | } |
| 2706 | |
| 2707 | if (unicast) |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 2708 | err = netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 2709 | else |
| 2710 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 2711 | flags & NLM_F_ECHO); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2712 | |
Zhike Wang | 5b5f99b | 2019-03-11 03:15:54 -0700 | [diff] [blame] | 2713 | if (err > 0) |
| 2714 | err = 0; |
| 2715 | return err; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2716 | } |
| 2717 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2718 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, |
| 2719 | void *tmplt_priv, u32 chain_index, |
| 2720 | struct tcf_block *block, struct sk_buff *oskb, |
| 2721 | u32 seq, u16 flags, bool unicast) |
| 2722 | { |
| 2723 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 2724 | struct net *net = block->net; |
| 2725 | struct sk_buff *skb; |
| 2726 | |
| 2727 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 2728 | if (!skb) |
| 2729 | return -ENOBUFS; |
| 2730 | |
| 2731 | if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb, |
| 2732 | block, portid, seq, flags, RTM_DELCHAIN) <= 0) { |
| 2733 | kfree_skb(skb); |
| 2734 | return -EINVAL; |
| 2735 | } |
| 2736 | |
| 2737 | if (unicast) |
| 2738 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 2739 | |
| 2740 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); |
| 2741 | } |
| 2742 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2743 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, |
| 2744 | struct nlattr **tca, |
| 2745 | struct netlink_ext_ack *extack) |
| 2746 | { |
| 2747 | const struct tcf_proto_ops *ops; |
Eric Dumazet | 2dd5616 | 2019-12-07 11:34:45 -0800 | [diff] [blame] | 2748 | char name[IFNAMSIZ]; |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2749 | void *tmplt_priv; |
| 2750 | |
| 2751 | /* If kind is not set, user did not specify template. */ |
| 2752 | if (!tca[TCA_KIND]) |
| 2753 | return 0; |
| 2754 | |
Eric Dumazet | 2dd5616 | 2019-12-07 11:34:45 -0800 | [diff] [blame] | 2755 | if (tcf_proto_check_kind(tca[TCA_KIND], name)) { |
| 2756 | NL_SET_ERR_MSG(extack, "Specified TC chain template name too long"); |
| 2757 | return -EINVAL; |
| 2758 | } |
| 2759 | |
| 2760 | ops = tcf_proto_lookup_ops(name, true, extack); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2761 | if (IS_ERR(ops)) |
| 2762 | return PTR_ERR(ops); |
| 2763 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { |
| 2764 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); |
| 2765 | return -EOPNOTSUPP; |
| 2766 | } |
| 2767 | |
| 2768 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack); |
| 2769 | if (IS_ERR(tmplt_priv)) { |
| 2770 | module_put(ops->owner); |
| 2771 | return PTR_ERR(tmplt_priv); |
| 2772 | } |
| 2773 | chain->tmplt_ops = ops; |
| 2774 | chain->tmplt_priv = tmplt_priv; |
| 2775 | return 0; |
| 2776 | } |
| 2777 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2778 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, |
| 2779 | void *tmplt_priv) |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2780 | { |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2781 | /* If template ops are set, no work to do for us. */ |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2782 | if (!tmplt_ops) |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2783 | return; |
| 2784 | |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 2785 | tmplt_ops->tmplt_destroy(tmplt_priv); |
| 2786 | module_put(tmplt_ops->owner); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2789 | /* Add/delete/get a chain */ |
| 2790 | |
| 2791 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, |
| 2792 | struct netlink_ext_ack *extack) |
| 2793 | { |
| 2794 | struct net *net = sock_net(skb->sk); |
| 2795 | struct nlattr *tca[TCA_MAX + 1]; |
| 2796 | struct tcmsg *t; |
| 2797 | u32 parent; |
| 2798 | u32 chain_index; |
| 2799 | struct Qdisc *q = NULL; |
| 2800 | struct tcf_chain *chain = NULL; |
| 2801 | struct tcf_block *block; |
| 2802 | unsigned long cl; |
| 2803 | int err; |
| 2804 | |
| 2805 | if (n->nlmsg_type != RTM_GETCHAIN && |
| 2806 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 2807 | return -EPERM; |
| 2808 | |
| 2809 | replay: |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2810 | err = nlmsg_parse_deprecated(n, sizeof(*t), tca, TCA_MAX, |
| 2811 | rtm_tca_policy, extack); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2812 | if (err < 0) |
| 2813 | return err; |
| 2814 | |
| 2815 | t = nlmsg_data(n); |
| 2816 | parent = t->tcm_parent; |
| 2817 | cl = 0; |
| 2818 | |
| 2819 | block = tcf_block_find(net, &q, &parent, &cl, |
| 2820 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 2821 | if (IS_ERR(block)) |
| 2822 | return PTR_ERR(block); |
| 2823 | |
| 2824 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 2825 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 2826 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2827 | err = -EINVAL; |
| 2828 | goto errout_block; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2829 | } |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2830 | |
| 2831 | mutex_lock(&block->lock); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2832 | chain = tcf_chain_lookup(block, chain_index); |
| 2833 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
| 2834 | if (chain) { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2835 | if (tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2836 | /* The chain exists only because there is |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2837 | * some action referencing it. |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2838 | */ |
| 2839 | tcf_chain_hold(chain); |
| 2840 | } else { |
| 2841 | NL_SET_ERR_MSG(extack, "Filter chain already exists"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2842 | err = -EEXIST; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2843 | goto errout_block_locked; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2844 | } |
| 2845 | } else { |
| 2846 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| 2847 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2848 | err = -ENOENT; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2849 | goto errout_block_locked; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2850 | } |
| 2851 | chain = tcf_chain_create(block, chain_index); |
| 2852 | if (!chain) { |
| 2853 | NL_SET_ERR_MSG(extack, "Failed to create filter chain"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2854 | err = -ENOMEM; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2855 | goto errout_block_locked; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2856 | } |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2857 | } |
| 2858 | } else { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2859 | if (!chain || tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2860 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2861 | err = -EINVAL; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2862 | goto errout_block_locked; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2863 | } |
| 2864 | tcf_chain_hold(chain); |
| 2865 | } |
| 2866 | |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2867 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
| 2868 | /* Modifying chain requires holding parent block lock. In case |
| 2869 | * the chain was successfully added, take a reference to the |
| 2870 | * chain. This ensures that an empty chain does not disappear at |
| 2871 | * the end of this function. |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2872 | */ |
| 2873 | tcf_chain_hold(chain); |
| 2874 | chain->explicitly_created = true; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2875 | } |
| 2876 | mutex_unlock(&block->lock); |
| 2877 | |
| 2878 | switch (n->nlmsg_type) { |
| 2879 | case RTM_NEWCHAIN: |
| 2880 | err = tc_chain_tmplt_add(chain, net, tca, extack); |
| 2881 | if (err) { |
| 2882 | tcf_chain_put_explicitly_created(chain); |
| 2883 | goto errout; |
| 2884 | } |
| 2885 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2886 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 2887 | RTM_NEWCHAIN, false); |
| 2888 | break; |
| 2889 | case RTM_DELCHAIN: |
Cong Wang | f5b9bac | 2018-09-11 14:22:23 -0700 | [diff] [blame] | 2890 | tfilter_notify_chain(net, skb, block, q, parent, n, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2891 | chain, RTM_DELTFILTER, true); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2892 | /* Flush the chain first as the user requested chain removal. */ |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2893 | tcf_chain_flush(chain, true); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2894 | /* In case the chain was successfully deleted, put a reference |
| 2895 | * to the chain previously taken during addition. |
| 2896 | */ |
| 2897 | tcf_chain_put_explicitly_created(chain); |
| 2898 | break; |
| 2899 | case RTM_GETCHAIN: |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2900 | err = tc_chain_notify(chain, skb, n->nlmsg_seq, |
| 2901 | n->nlmsg_seq, n->nlmsg_type, true); |
| 2902 | if (err < 0) |
| 2903 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); |
| 2904 | break; |
| 2905 | default: |
| 2906 | err = -EOPNOTSUPP; |
| 2907 | NL_SET_ERR_MSG(extack, "Unsupported message type"); |
| 2908 | goto errout; |
| 2909 | } |
| 2910 | |
| 2911 | errout: |
| 2912 | tcf_chain_put(chain); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2913 | errout_block: |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2914 | tcf_block_release(q, block, true); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2915 | if (err == -EAGAIN) |
| 2916 | /* Replay the request. */ |
| 2917 | goto replay; |
| 2918 | return err; |
Vlad Buslov | 2cbfab0 | 2019-02-11 10:55:34 +0200 | [diff] [blame] | 2919 | |
| 2920 | errout_block_locked: |
| 2921 | mutex_unlock(&block->lock); |
| 2922 | goto errout_block; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | /* called with RTNL */ |
| 2926 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) |
| 2927 | { |
| 2928 | struct net *net = sock_net(skb->sk); |
| 2929 | struct nlattr *tca[TCA_MAX + 1]; |
| 2930 | struct Qdisc *q = NULL; |
| 2931 | struct tcf_block *block; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2932 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Vlad Buslov | ace4a26 | 2019-02-25 17:45:44 +0200 | [diff] [blame] | 2933 | struct tcf_chain *chain; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2934 | long index_start; |
| 2935 | long index; |
| 2936 | u32 parent; |
| 2937 | int err; |
| 2938 | |
| 2939 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| 2940 | return skb->len; |
| 2941 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2942 | err = nlmsg_parse_deprecated(cb->nlh, sizeof(*tcm), tca, TCA_MAX, |
| 2943 | rtm_tca_policy, cb->extack); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2944 | if (err) |
| 2945 | return err; |
| 2946 | |
| 2947 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 2948 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2949 | if (!block) |
| 2950 | goto out; |
| 2951 | /* If we work with block index, q is NULL and parent value |
| 2952 | * will never be used in the following code. The check |
| 2953 | * in tcf_fill_node prevents it. However, compiler does not |
| 2954 | * see that far, so set parent to zero to silence the warning |
| 2955 | * about parent being uninitialized. |
| 2956 | */ |
| 2957 | parent = 0; |
| 2958 | } else { |
| 2959 | const struct Qdisc_class_ops *cops; |
| 2960 | struct net_device *dev; |
| 2961 | unsigned long cl = 0; |
| 2962 | |
| 2963 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 2964 | if (!dev) |
| 2965 | return skb->len; |
| 2966 | |
| 2967 | parent = tcm->tcm_parent; |
| 2968 | if (!parent) { |
| 2969 | q = dev->qdisc; |
| 2970 | parent = q->handle; |
| 2971 | } else { |
| 2972 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 2973 | } |
| 2974 | if (!q) |
| 2975 | goto out; |
| 2976 | cops = q->ops->cl_ops; |
| 2977 | if (!cops) |
| 2978 | goto out; |
| 2979 | if (!cops->tcf_block) |
| 2980 | goto out; |
| 2981 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 2982 | cl = cops->find(q, tcm->tcm_parent); |
| 2983 | if (cl == 0) |
| 2984 | goto out; |
| 2985 | } |
| 2986 | block = cops->tcf_block(q, cl, NULL); |
| 2987 | if (!block) |
| 2988 | goto out; |
| 2989 | if (tcf_block_shared(block)) |
| 2990 | q = NULL; |
| 2991 | } |
| 2992 | |
| 2993 | index_start = cb->args[0]; |
| 2994 | index = 0; |
| 2995 | |
Vlad Buslov | ace4a26 | 2019-02-25 17:45:44 +0200 | [diff] [blame] | 2996 | mutex_lock(&block->lock); |
| 2997 | list_for_each_entry(chain, &block->chain_list, list) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2998 | if ((tca[TCA_CHAIN] && |
| 2999 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)) |
| 3000 | continue; |
| 3001 | if (index < index_start) { |
| 3002 | index++; |
| 3003 | continue; |
| 3004 | } |
Vlad Buslov | ace4a26 | 2019-02-25 17:45:44 +0200 | [diff] [blame] | 3005 | if (tcf_chain_held_by_acts_only(chain)) |
| 3006 | continue; |
Vlad Buslov | a565482 | 2019-02-11 10:55:37 +0200 | [diff] [blame] | 3007 | err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, |
| 3008 | chain->index, net, skb, block, |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 3009 | NETLINK_CB(cb->skb).portid, |
| 3010 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 3011 | RTM_NEWCHAIN); |
Vlad Buslov | ace4a26 | 2019-02-25 17:45:44 +0200 | [diff] [blame] | 3012 | if (err <= 0) |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 3013 | break; |
| 3014 | index++; |
| 3015 | } |
Vlad Buslov | ace4a26 | 2019-02-25 17:45:44 +0200 | [diff] [blame] | 3016 | mutex_unlock(&block->lock); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 3017 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 3018 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 3019 | tcf_block_refcnt_put(block, true); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 3020 | cb->args[0] = index; |
| 3021 | |
| 3022 | out: |
| 3023 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 3024 | if (skb->len == 0 && err) |
| 3025 | return err; |
| 3026 | return skb->len; |
| 3027 | } |
| 3028 | |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 3029 | void tcf_exts_destroy(struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | { |
| 3031 | #ifdef CONFIG_NET_CLS_ACT |
Eric Dumazet | 3d66b89 | 2019-09-18 12:57:04 -0700 | [diff] [blame] | 3032 | if (exts->actions) { |
| 3033 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
| 3034 | kfree(exts->actions); |
| 3035 | } |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3036 | exts->nr_actions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3037 | #endif |
| 3038 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3039 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 3041 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 3042 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 3043 | bool rtnl_held, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | #ifdef CONFIG_NET_CLS_ACT |
| 3046 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3047 | struct tc_action *act; |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 3048 | size_t attr_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3050 | if (exts->police && tb[exts->police]) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 3051 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 3052 | rate_tlv, "police", ovr, |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 3053 | TCA_ACT_BIND, rtnl_held, |
| 3054 | extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 3055 | if (IS_ERR(act)) |
| 3056 | return PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3058 | act->type = exts->type = TCA_OLD_COMPAT; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3059 | exts->actions[0] = act; |
| 3060 | exts->nr_actions = 1; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3061 | } else if (exts->action && tb[exts->action]) { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 3062 | int err; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3063 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 3064 | err = tcf_action_init(net, tp, tb[exts->action], |
| 3065 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 3066 | exts->actions, &attr_size, |
| 3067 | rtnl_held, extack); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 3068 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3069 | return err; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 3070 | exts->nr_actions = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3071 | } |
| 3072 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3073 | #else |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3074 | if ((exts->action && tb[exts->action]) || |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 3075 | (exts->police && tb[exts->police])) { |
| 3076 | NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | return -EOPNOTSUPP; |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 3078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | #endif |
| 3080 | |
| 3081 | return 0; |
| 3082 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3083 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 3085 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3086 | { |
| 3087 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3088 | struct tcf_exts old = *dst; |
| 3089 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 3090 | *dst = *src; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3091 | tcf_exts_destroy(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | #endif |
| 3093 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3094 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3095 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3096 | #ifdef CONFIG_NET_CLS_ACT |
| 3097 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 3098 | { |
| 3099 | if (exts->nr_actions == 0) |
| 3100 | return NULL; |
| 3101 | else |
| 3102 | return exts->actions[0]; |
| 3103 | } |
| 3104 | #endif |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3105 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3106 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | { |
| 3108 | #ifdef CONFIG_NET_CLS_ACT |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 3109 | struct nlattr *nest; |
| 3110 | |
Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 3111 | if (exts->action && tcf_exts_has_actions(exts)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | /* |
| 3113 | * again for backward compatible mode - we want |
| 3114 | * to work with both old and new modes of entering |
| 3115 | * tc data even if iproute2 was newer - jhs |
| 3116 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3117 | if (exts->type != TCA_OLD_COMPAT) { |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3118 | nest = nla_nest_start_noflag(skb, exts->action); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 3119 | if (nest == NULL) |
| 3120 | goto nla_put_failure; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 3121 | |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 3122 | if (tcf_action_dump(skb, exts->actions, 0, 0, false) |
| 3123 | < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 3124 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 3125 | nla_nest_end(skb, nest); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3126 | } else if (exts->police) { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3127 | struct tc_action *act = tcf_exts_first_act(exts); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3128 | nest = nla_nest_start_noflag(skb, exts->police); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 3129 | if (nest == NULL || !act) |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 3130 | goto nla_put_failure; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3131 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 3132 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 3133 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | } |
| 3135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | return 0; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 3137 | |
| 3138 | nla_put_failure: |
| 3139 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3140 | return -1; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 3141 | #else |
| 3142 | return 0; |
| 3143 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3145 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | |
Vlad Buslov | ca44b73 | 2020-05-15 14:40:12 +0300 | [diff] [blame] | 3147 | int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts) |
| 3148 | { |
| 3149 | #ifdef CONFIG_NET_CLS_ACT |
| 3150 | struct nlattr *nest; |
| 3151 | |
| 3152 | if (!exts->action || !tcf_exts_has_actions(exts)) |
| 3153 | return 0; |
| 3154 | |
| 3155 | nest = nla_nest_start_noflag(skb, exts->action); |
| 3156 | if (!nest) |
| 3157 | goto nla_put_failure; |
| 3158 | |
| 3159 | if (tcf_action_dump(skb, exts->actions, 0, 0, true) < 0) |
| 3160 | goto nla_put_failure; |
| 3161 | nla_nest_end(skb, nest); |
| 3162 | return 0; |
| 3163 | |
| 3164 | nla_put_failure: |
| 3165 | nla_nest_cancel(skb, nest); |
| 3166 | return -1; |
| 3167 | #else |
| 3168 | return 0; |
| 3169 | #endif |
| 3170 | } |
| 3171 | EXPORT_SYMBOL(tcf_exts_terse_dump); |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3172 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 3173 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | { |
| 3175 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3176 | struct tc_action *a = tcf_exts_first_act(exts); |
Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 3177 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 3178 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3179 | #endif |
| 3180 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 3182 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3184 | static void tcf_block_offload_inc(struct tcf_block *block, u32 *flags) |
| 3185 | { |
| 3186 | if (*flags & TCA_CLS_FLAGS_IN_HW) |
| 3187 | return; |
| 3188 | *flags |= TCA_CLS_FLAGS_IN_HW; |
| 3189 | atomic_inc(&block->offloadcnt); |
| 3190 | } |
| 3191 | |
| 3192 | static void tcf_block_offload_dec(struct tcf_block *block, u32 *flags) |
| 3193 | { |
| 3194 | if (!(*flags & TCA_CLS_FLAGS_IN_HW)) |
| 3195 | return; |
| 3196 | *flags &= ~TCA_CLS_FLAGS_IN_HW; |
| 3197 | atomic_dec(&block->offloadcnt); |
| 3198 | } |
| 3199 | |
| 3200 | static void tc_cls_offload_cnt_update(struct tcf_block *block, |
| 3201 | struct tcf_proto *tp, u32 *cnt, |
| 3202 | u32 *flags, u32 diff, bool add) |
| 3203 | { |
| 3204 | lockdep_assert_held(&block->cb_lock); |
| 3205 | |
| 3206 | spin_lock(&tp->lock); |
| 3207 | if (add) { |
| 3208 | if (!*cnt) |
| 3209 | tcf_block_offload_inc(block, flags); |
| 3210 | *cnt += diff; |
| 3211 | } else { |
| 3212 | *cnt -= diff; |
| 3213 | if (!*cnt) |
| 3214 | tcf_block_offload_dec(block, flags); |
| 3215 | } |
| 3216 | spin_unlock(&tp->lock); |
| 3217 | } |
| 3218 | |
| 3219 | static void |
| 3220 | tc_cls_offload_cnt_reset(struct tcf_block *block, struct tcf_proto *tp, |
| 3221 | u32 *cnt, u32 *flags) |
| 3222 | { |
| 3223 | lockdep_assert_held(&block->cb_lock); |
| 3224 | |
| 3225 | spin_lock(&tp->lock); |
| 3226 | tcf_block_offload_dec(block, flags); |
| 3227 | *cnt = 0; |
| 3228 | spin_unlock(&tp->lock); |
| 3229 | } |
| 3230 | |
| 3231 | static int |
| 3232 | __tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 3233 | void *type_data, bool err_stop) |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 3234 | { |
Pablo Neira Ayuso | 955bcb6 | 2019-07-09 22:55:46 +0200 | [diff] [blame] | 3235 | struct flow_block_cb *block_cb; |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 3236 | int ok_count = 0; |
| 3237 | int err; |
| 3238 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3239 | list_for_each_entry(block_cb, &block->flow_block.cb_list, list) { |
| 3240 | err = block_cb->cb(type, type_data, block_cb->cb_priv); |
| 3241 | if (err) { |
| 3242 | if (err_stop) |
| 3243 | return err; |
| 3244 | } else { |
| 3245 | ok_count++; |
| 3246 | } |
| 3247 | } |
| 3248 | return ok_count; |
| 3249 | } |
| 3250 | |
| 3251 | int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 3252 | void *type_data, bool err_stop, bool rtnl_held) |
| 3253 | { |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3254 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3255 | int ok_count; |
| 3256 | |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3257 | retry: |
| 3258 | if (take_rtnl) |
| 3259 | rtnl_lock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3260 | down_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3261 | /* Need to obtain rtnl lock if block is bound to devs that require it. |
| 3262 | * In block bind code cb_lock is obtained while holding rtnl, so we must |
| 3263 | * obtain the locks in same order here. |
| 3264 | */ |
| 3265 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { |
| 3266 | up_read(&block->cb_lock); |
| 3267 | take_rtnl = true; |
| 3268 | goto retry; |
| 3269 | } |
| 3270 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3271 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3272 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3273 | up_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3274 | if (take_rtnl) |
| 3275 | rtnl_unlock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3276 | return ok_count; |
| 3277 | } |
| 3278 | EXPORT_SYMBOL(tc_setup_cb_call); |
| 3279 | |
| 3280 | /* Non-destructive filter add. If filter that wasn't already in hardware is |
| 3281 | * successfully offloaded, increment block offloads counter. On failure, |
| 3282 | * previously offloaded filter is considered to be intact and offloads counter |
| 3283 | * is not decremented. |
| 3284 | */ |
| 3285 | |
| 3286 | int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp, |
| 3287 | enum tc_setup_type type, void *type_data, bool err_stop, |
| 3288 | u32 *flags, unsigned int *in_hw_count, bool rtnl_held) |
| 3289 | { |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3290 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3291 | int ok_count; |
| 3292 | |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3293 | retry: |
| 3294 | if (take_rtnl) |
| 3295 | rtnl_lock(); |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 3296 | down_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3297 | /* Need to obtain rtnl lock if block is bound to devs that require it. |
| 3298 | * In block bind code cb_lock is obtained while holding rtnl, so we must |
| 3299 | * obtain the locks in same order here. |
| 3300 | */ |
| 3301 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { |
| 3302 | up_read(&block->cb_lock); |
| 3303 | take_rtnl = true; |
| 3304 | goto retry; |
| 3305 | } |
| 3306 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 3307 | /* Make sure all netdevs sharing this block are offload-capable. */ |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 3308 | if (block->nooffloaddevcnt && err_stop) { |
| 3309 | ok_count = -EOPNOTSUPP; |
| 3310 | goto err_unlock; |
| 3311 | } |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 3312 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3313 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3314 | if (ok_count < 0) |
| 3315 | goto err_unlock; |
| 3316 | |
| 3317 | if (tp->ops->hw_add) |
| 3318 | tp->ops->hw_add(tp, type_data); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3319 | if (ok_count > 0) |
| 3320 | tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, |
| 3321 | ok_count, true); |
Vlad Buslov | 4f8116c | 2019-08-26 16:44:57 +0300 | [diff] [blame] | 3322 | err_unlock: |
| 3323 | up_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3324 | if (take_rtnl) |
| 3325 | rtnl_unlock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3326 | return ok_count < 0 ? ok_count : 0; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 3327 | } |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3328 | EXPORT_SYMBOL(tc_setup_cb_add); |
| 3329 | |
| 3330 | /* Destructive filter replace. If filter that wasn't already in hardware is |
| 3331 | * successfully offloaded, increment block offload counter. On failure, |
| 3332 | * previously offloaded filter is considered to be destroyed and offload counter |
| 3333 | * is decremented. |
| 3334 | */ |
| 3335 | |
| 3336 | int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp, |
| 3337 | enum tc_setup_type type, void *type_data, bool err_stop, |
| 3338 | u32 *old_flags, unsigned int *old_in_hw_count, |
| 3339 | u32 *new_flags, unsigned int *new_in_hw_count, |
| 3340 | bool rtnl_held) |
| 3341 | { |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3342 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3343 | int ok_count; |
| 3344 | |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3345 | retry: |
| 3346 | if (take_rtnl) |
| 3347 | rtnl_lock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3348 | down_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3349 | /* Need to obtain rtnl lock if block is bound to devs that require it. |
| 3350 | * In block bind code cb_lock is obtained while holding rtnl, so we must |
| 3351 | * obtain the locks in same order here. |
| 3352 | */ |
| 3353 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { |
| 3354 | up_read(&block->cb_lock); |
| 3355 | take_rtnl = true; |
| 3356 | goto retry; |
| 3357 | } |
| 3358 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3359 | /* Make sure all netdevs sharing this block are offload-capable. */ |
| 3360 | if (block->nooffloaddevcnt && err_stop) { |
| 3361 | ok_count = -EOPNOTSUPP; |
| 3362 | goto err_unlock; |
| 3363 | } |
| 3364 | |
| 3365 | tc_cls_offload_cnt_reset(block, tp, old_in_hw_count, old_flags); |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3366 | if (tp->ops->hw_del) |
| 3367 | tp->ops->hw_del(tp, type_data); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3368 | |
| 3369 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3370 | if (ok_count < 0) |
| 3371 | goto err_unlock; |
| 3372 | |
| 3373 | if (tp->ops->hw_add) |
| 3374 | tp->ops->hw_add(tp, type_data); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3375 | if (ok_count > 0) |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3376 | tc_cls_offload_cnt_update(block, tp, new_in_hw_count, |
| 3377 | new_flags, ok_count, true); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3378 | err_unlock: |
| 3379 | up_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3380 | if (take_rtnl) |
| 3381 | rtnl_unlock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3382 | return ok_count < 0 ? ok_count : 0; |
| 3383 | } |
| 3384 | EXPORT_SYMBOL(tc_setup_cb_replace); |
| 3385 | |
| 3386 | /* Destroy filter and decrement block offload counter, if filter was previously |
| 3387 | * offloaded. |
| 3388 | */ |
| 3389 | |
| 3390 | int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp, |
| 3391 | enum tc_setup_type type, void *type_data, bool err_stop, |
| 3392 | u32 *flags, unsigned int *in_hw_count, bool rtnl_held) |
| 3393 | { |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3394 | bool take_rtnl = READ_ONCE(block->lockeddevcnt) && !rtnl_held; |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3395 | int ok_count; |
| 3396 | |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3397 | retry: |
| 3398 | if (take_rtnl) |
| 3399 | rtnl_lock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3400 | down_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3401 | /* Need to obtain rtnl lock if block is bound to devs that require it. |
| 3402 | * In block bind code cb_lock is obtained while holding rtnl, so we must |
| 3403 | * obtain the locks in same order here. |
| 3404 | */ |
| 3405 | if (!rtnl_held && !take_rtnl && block->lockeddevcnt) { |
| 3406 | up_read(&block->cb_lock); |
| 3407 | take_rtnl = true; |
| 3408 | goto retry; |
| 3409 | } |
| 3410 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3411 | ok_count = __tc_setup_cb_call(block, type, type_data, err_stop); |
| 3412 | |
| 3413 | tc_cls_offload_cnt_reset(block, tp, in_hw_count, flags); |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3414 | if (tp->ops->hw_del) |
| 3415 | tp->ops->hw_del(tp, type_data); |
| 3416 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3417 | up_read(&block->cb_lock); |
Vlad Buslov | 11bd634 | 2019-08-26 16:45:02 +0300 | [diff] [blame] | 3418 | if (take_rtnl) |
| 3419 | rtnl_unlock(); |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 3420 | return ok_count < 0 ? ok_count : 0; |
| 3421 | } |
| 3422 | EXPORT_SYMBOL(tc_setup_cb_destroy); |
| 3423 | |
| 3424 | int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp, |
| 3425 | bool add, flow_setup_cb_t *cb, |
| 3426 | enum tc_setup_type type, void *type_data, |
| 3427 | void *cb_priv, u32 *flags, unsigned int *in_hw_count) |
| 3428 | { |
| 3429 | int err = cb(type, type_data, cb_priv); |
| 3430 | |
| 3431 | if (err) { |
| 3432 | if (add && tc_skip_sw(*flags)) |
| 3433 | return err; |
| 3434 | } else { |
| 3435 | tc_cls_offload_cnt_update(block, tp, in_hw_count, flags, 1, |
| 3436 | add); |
| 3437 | } |
| 3438 | |
| 3439 | return 0; |
| 3440 | } |
| 3441 | EXPORT_SYMBOL(tc_setup_cb_reoffload); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 3442 | |
Jiri Pirko | 2008495 | 2020-02-25 11:45:18 +0100 | [diff] [blame] | 3443 | static int tcf_act_get_cookie(struct flow_action_entry *entry, |
| 3444 | const struct tc_action *act) |
| 3445 | { |
| 3446 | struct tc_cookie *cookie; |
| 3447 | int err = 0; |
| 3448 | |
| 3449 | rcu_read_lock(); |
| 3450 | cookie = rcu_dereference(act->act_cookie); |
| 3451 | if (cookie) { |
| 3452 | entry->cookie = flow_action_cookie_create(cookie->data, |
| 3453 | cookie->len, |
| 3454 | GFP_ATOMIC); |
| 3455 | if (!entry->cookie) |
| 3456 | err = -ENOMEM; |
| 3457 | } |
| 3458 | rcu_read_unlock(); |
| 3459 | return err; |
| 3460 | } |
| 3461 | |
| 3462 | static void tcf_act_put_cookie(struct flow_action_entry *entry) |
| 3463 | { |
| 3464 | flow_action_cookie_destroy(entry->cookie); |
| 3465 | } |
| 3466 | |
Vlad Buslov | 5a6ff4b | 2019-08-26 16:45:04 +0300 | [diff] [blame] | 3467 | void tc_cleanup_flow_action(struct flow_action *flow_action) |
| 3468 | { |
| 3469 | struct flow_action_entry *entry; |
| 3470 | int i; |
| 3471 | |
Jiri Pirko | 2008495 | 2020-02-25 11:45:18 +0100 | [diff] [blame] | 3472 | flow_action_for_each(i, entry, flow_action) { |
| 3473 | tcf_act_put_cookie(entry); |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3474 | if (entry->destructor) |
| 3475 | entry->destructor(entry->destructor_priv); |
Jiri Pirko | 2008495 | 2020-02-25 11:45:18 +0100 | [diff] [blame] | 3476 | } |
Vlad Buslov | 5a6ff4b | 2019-08-26 16:45:04 +0300 | [diff] [blame] | 3477 | } |
| 3478 | EXPORT_SYMBOL(tc_cleanup_flow_action); |
| 3479 | |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3480 | static void tcf_mirred_get_dev(struct flow_action_entry *entry, |
| 3481 | const struct tc_action *act) |
| 3482 | { |
Vlad Buslov | 470d506 | 2019-09-13 18:28:41 +0300 | [diff] [blame] | 3483 | #ifdef CONFIG_NET_CLS_ACT |
| 3484 | entry->dev = act->ops->get_dev(act, &entry->destructor); |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3485 | if (!entry->dev) |
| 3486 | return; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3487 | entry->destructor_priv = entry->dev; |
Vlad Buslov | 470d506 | 2019-09-13 18:28:41 +0300 | [diff] [blame] | 3488 | #endif |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3489 | } |
| 3490 | |
| 3491 | static void tcf_tunnel_encap_put_tunnel(void *priv) |
| 3492 | { |
| 3493 | struct ip_tunnel_info *tunnel = priv; |
| 3494 | |
| 3495 | kfree(tunnel); |
| 3496 | } |
| 3497 | |
| 3498 | static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry, |
| 3499 | const struct tc_action *act) |
| 3500 | { |
| 3501 | entry->tunnel = tcf_tunnel_info_copy(act); |
| 3502 | if (!entry->tunnel) |
| 3503 | return -ENOMEM; |
| 3504 | entry->destructor = tcf_tunnel_encap_put_tunnel; |
| 3505 | entry->destructor_priv = entry->tunnel; |
| 3506 | return 0; |
| 3507 | } |
| 3508 | |
Vlad Buslov | 4a5da47 | 2019-09-13 18:28:40 +0300 | [diff] [blame] | 3509 | static void tcf_sample_get_group(struct flow_action_entry *entry, |
| 3510 | const struct tc_action *act) |
| 3511 | { |
| 3512 | #ifdef CONFIG_NET_CLS_ACT |
| 3513 | entry->sample.psample_group = |
| 3514 | act->ops->get_psample_group(act, &entry->destructor); |
| 3515 | entry->destructor_priv = entry->sample.psample_group; |
| 3516 | #endif |
| 3517 | } |
| 3518 | |
Po Liu | d29bdd6 | 2020-05-01 08:53:16 +0800 | [diff] [blame] | 3519 | static void tcf_gate_entry_destructor(void *priv) |
| 3520 | { |
| 3521 | struct action_gate_entry *oe = priv; |
| 3522 | |
| 3523 | kfree(oe); |
| 3524 | } |
| 3525 | |
| 3526 | static int tcf_gate_get_entries(struct flow_action_entry *entry, |
| 3527 | const struct tc_action *act) |
| 3528 | { |
| 3529 | entry->gate.entries = tcf_gate_get_list(act); |
| 3530 | |
| 3531 | if (!entry->gate.entries) |
| 3532 | return -EINVAL; |
| 3533 | |
| 3534 | entry->destructor = tcf_gate_entry_destructor; |
| 3535 | entry->destructor_priv = entry->gate.entries; |
| 3536 | |
| 3537 | return 0; |
| 3538 | } |
| 3539 | |
Pablo Neira Ayuso | 16f8036 | 2020-05-06 20:34:50 +0200 | [diff] [blame] | 3540 | static enum flow_action_hw_stats tc_act_hw_stats(u8 hw_stats) |
| 3541 | { |
| 3542 | if (WARN_ON_ONCE(hw_stats > TCA_ACT_HW_STATS_ANY)) |
| 3543 | return FLOW_ACTION_HW_STATS_DONT_CARE; |
| 3544 | else if (!hw_stats) |
| 3545 | return FLOW_ACTION_HW_STATS_DISABLED; |
| 3546 | |
| 3547 | return hw_stats; |
| 3548 | } |
| 3549 | |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3550 | int tc_setup_flow_action(struct flow_action *flow_action, |
Vlad Buslov | b15e7a6 | 2020-02-17 12:12:12 +0200 | [diff] [blame] | 3551 | const struct tcf_exts *exts) |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3552 | { |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3553 | struct tc_action *act; |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3554 | int i, j, k, err = 0; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3555 | |
Jakub Kicinski | 0dfb2d8 | 2020-03-19 16:26:23 -0700 | [diff] [blame] | 3556 | BUILD_BUG_ON(TCA_ACT_HW_STATS_ANY != FLOW_ACTION_HW_STATS_ANY); |
| 3557 | BUILD_BUG_ON(TCA_ACT_HW_STATS_IMMEDIATE != FLOW_ACTION_HW_STATS_IMMEDIATE); |
| 3558 | BUILD_BUG_ON(TCA_ACT_HW_STATS_DELAYED != FLOW_ACTION_HW_STATS_DELAYED); |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 3559 | |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3560 | if (!exts) |
| 3561 | return 0; |
| 3562 | |
| 3563 | j = 0; |
| 3564 | tcf_exts_for_each_action(i, act, exts) { |
| 3565 | struct flow_action_entry *entry; |
| 3566 | |
| 3567 | entry = &flow_action->entries[j]; |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3568 | spin_lock_bh(&act->tcfa_lock); |
Jiri Pirko | 2008495 | 2020-02-25 11:45:18 +0100 | [diff] [blame] | 3569 | err = tcf_act_get_cookie(entry, act); |
| 3570 | if (err) |
| 3571 | goto err_out_locked; |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 3572 | |
Pablo Neira Ayuso | 16f8036 | 2020-05-06 20:34:50 +0200 | [diff] [blame] | 3573 | entry->hw_stats = tc_act_hw_stats(act->hw_stats); |
Jiri Pirko | 44f8658 | 2020-03-07 12:40:20 +0100 | [diff] [blame] | 3574 | |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3575 | if (is_tcf_gact_ok(act)) { |
| 3576 | entry->id = FLOW_ACTION_ACCEPT; |
| 3577 | } else if (is_tcf_gact_shot(act)) { |
| 3578 | entry->id = FLOW_ACTION_DROP; |
| 3579 | } else if (is_tcf_gact_trap(act)) { |
| 3580 | entry->id = FLOW_ACTION_TRAP; |
| 3581 | } else if (is_tcf_gact_goto_chain(act)) { |
| 3582 | entry->id = FLOW_ACTION_GOTO; |
| 3583 | entry->chain_index = tcf_gact_goto_chain_index(act); |
| 3584 | } else if (is_tcf_mirred_egress_redirect(act)) { |
| 3585 | entry->id = FLOW_ACTION_REDIRECT; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3586 | tcf_mirred_get_dev(entry, act); |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3587 | } else if (is_tcf_mirred_egress_mirror(act)) { |
| 3588 | entry->id = FLOW_ACTION_MIRRED; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3589 | tcf_mirred_get_dev(entry, act); |
John Hurley | 48e584a | 2019-08-04 16:09:06 +0100 | [diff] [blame] | 3590 | } else if (is_tcf_mirred_ingress_redirect(act)) { |
| 3591 | entry->id = FLOW_ACTION_REDIRECT_INGRESS; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3592 | tcf_mirred_get_dev(entry, act); |
John Hurley | 48e584a | 2019-08-04 16:09:06 +0100 | [diff] [blame] | 3593 | } else if (is_tcf_mirred_ingress_mirror(act)) { |
| 3594 | entry->id = FLOW_ACTION_MIRRED_INGRESS; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3595 | tcf_mirred_get_dev(entry, act); |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3596 | } else if (is_tcf_vlan(act)) { |
| 3597 | switch (tcf_vlan_action(act)) { |
| 3598 | case TCA_VLAN_ACT_PUSH: |
| 3599 | entry->id = FLOW_ACTION_VLAN_PUSH; |
| 3600 | entry->vlan.vid = tcf_vlan_push_vid(act); |
| 3601 | entry->vlan.proto = tcf_vlan_push_proto(act); |
| 3602 | entry->vlan.prio = tcf_vlan_push_prio(act); |
| 3603 | break; |
| 3604 | case TCA_VLAN_ACT_POP: |
| 3605 | entry->id = FLOW_ACTION_VLAN_POP; |
| 3606 | break; |
| 3607 | case TCA_VLAN_ACT_MODIFY: |
| 3608 | entry->id = FLOW_ACTION_VLAN_MANGLE; |
| 3609 | entry->vlan.vid = tcf_vlan_push_vid(act); |
| 3610 | entry->vlan.proto = tcf_vlan_push_proto(act); |
| 3611 | entry->vlan.prio = tcf_vlan_push_prio(act); |
| 3612 | break; |
| 3613 | default: |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3614 | err = -EOPNOTSUPP; |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3615 | goto err_out_locked; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3616 | } |
| 3617 | } else if (is_tcf_tunnel_set(act)) { |
| 3618 | entry->id = FLOW_ACTION_TUNNEL_ENCAP; |
Vlad Buslov | 1158958 | 2019-09-13 18:28:39 +0300 | [diff] [blame] | 3619 | err = tcf_tunnel_encap_get_tunnel(entry, act); |
| 3620 | if (err) |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3621 | goto err_out_locked; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3622 | } else if (is_tcf_tunnel_release(act)) { |
| 3623 | entry->id = FLOW_ACTION_TUNNEL_DECAP; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3624 | } else if (is_tcf_pedit(act)) { |
| 3625 | for (k = 0; k < tcf_pedit_nkeys(act); k++) { |
| 3626 | switch (tcf_pedit_cmd(act, k)) { |
| 3627 | case TCA_PEDIT_KEY_EX_CMD_SET: |
| 3628 | entry->id = FLOW_ACTION_MANGLE; |
| 3629 | break; |
| 3630 | case TCA_PEDIT_KEY_EX_CMD_ADD: |
| 3631 | entry->id = FLOW_ACTION_ADD; |
| 3632 | break; |
| 3633 | default: |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3634 | err = -EOPNOTSUPP; |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3635 | goto err_out_locked; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3636 | } |
| 3637 | entry->mangle.htype = tcf_pedit_htype(act, k); |
| 3638 | entry->mangle.mask = tcf_pedit_mask(act, k); |
| 3639 | entry->mangle.val = tcf_pedit_val(act, k); |
| 3640 | entry->mangle.offset = tcf_pedit_offset(act, k); |
Pablo Neira Ayuso | 16f8036 | 2020-05-06 20:34:50 +0200 | [diff] [blame] | 3641 | entry->hw_stats = tc_act_hw_stats(act->hw_stats); |
Petr Machata | 2c4b58d | 2020-03-18 19:42:29 +0200 | [diff] [blame] | 3642 | entry = &flow_action->entries[++j]; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3643 | } |
| 3644 | } else if (is_tcf_csum(act)) { |
| 3645 | entry->id = FLOW_ACTION_CSUM; |
| 3646 | entry->csum_flags = tcf_csum_update_flags(act); |
| 3647 | } else if (is_tcf_skbedit_mark(act)) { |
| 3648 | entry->id = FLOW_ACTION_MARK; |
| 3649 | entry->mark = tcf_skbedit_mark(act); |
Pieter Jansen van Vuuren | a7a7be6 | 2019-05-04 04:46:16 -0700 | [diff] [blame] | 3650 | } else if (is_tcf_sample(act)) { |
| 3651 | entry->id = FLOW_ACTION_SAMPLE; |
Pieter Jansen van Vuuren | a7a7be6 | 2019-05-04 04:46:16 -0700 | [diff] [blame] | 3652 | entry->sample.trunc_size = tcf_sample_trunc_size(act); |
| 3653 | entry->sample.truncate = tcf_sample_truncate(act); |
| 3654 | entry->sample.rate = tcf_sample_rate(act); |
Vlad Buslov | 4a5da47 | 2019-09-13 18:28:40 +0300 | [diff] [blame] | 3655 | tcf_sample_get_group(entry, act); |
Pieter Jansen van Vuuren | 8c8cfc6 | 2019-05-04 04:46:22 -0700 | [diff] [blame] | 3656 | } else if (is_tcf_police(act)) { |
| 3657 | entry->id = FLOW_ACTION_POLICE; |
| 3658 | entry->police.burst = tcf_police_tcfp_burst(act); |
| 3659 | entry->police.rate_bytes_ps = |
| 3660 | tcf_police_rate_bytes_ps(act); |
Po Liu | 19e528d | 2020-06-24 17:36:28 +0800 | [diff] [blame^] | 3661 | entry->police.mtu = tcf_police_tcfp_mtu(act); |
Paul Blakey | b57dc7c | 2019-07-09 10:30:48 +0300 | [diff] [blame] | 3662 | } else if (is_tcf_ct(act)) { |
| 3663 | entry->id = FLOW_ACTION_CT; |
| 3664 | entry->ct.action = tcf_ct_action(act); |
| 3665 | entry->ct.zone = tcf_ct_zone(act); |
Paul Blakey | edd5861 | 2020-03-12 12:23:09 +0200 | [diff] [blame] | 3666 | entry->ct.flow_table = tcf_ct_ft(act); |
John Hurley | 6749d590 | 2019-07-23 15:33:59 +0100 | [diff] [blame] | 3667 | } else if (is_tcf_mpls(act)) { |
| 3668 | switch (tcf_mpls_action(act)) { |
| 3669 | case TCA_MPLS_ACT_PUSH: |
| 3670 | entry->id = FLOW_ACTION_MPLS_PUSH; |
| 3671 | entry->mpls_push.proto = tcf_mpls_proto(act); |
| 3672 | entry->mpls_push.label = tcf_mpls_label(act); |
| 3673 | entry->mpls_push.tc = tcf_mpls_tc(act); |
| 3674 | entry->mpls_push.bos = tcf_mpls_bos(act); |
| 3675 | entry->mpls_push.ttl = tcf_mpls_ttl(act); |
| 3676 | break; |
| 3677 | case TCA_MPLS_ACT_POP: |
| 3678 | entry->id = FLOW_ACTION_MPLS_POP; |
| 3679 | entry->mpls_pop.proto = tcf_mpls_proto(act); |
| 3680 | break; |
| 3681 | case TCA_MPLS_ACT_MODIFY: |
| 3682 | entry->id = FLOW_ACTION_MPLS_MANGLE; |
| 3683 | entry->mpls_mangle.label = tcf_mpls_label(act); |
| 3684 | entry->mpls_mangle.tc = tcf_mpls_tc(act); |
| 3685 | entry->mpls_mangle.bos = tcf_mpls_bos(act); |
| 3686 | entry->mpls_mangle.ttl = tcf_mpls_ttl(act); |
| 3687 | break; |
| 3688 | default: |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3689 | goto err_out_locked; |
John Hurley | 6749d590 | 2019-07-23 15:33:59 +0100 | [diff] [blame] | 3690 | } |
John Hurley | fb1b775 | 2019-08-04 16:09:04 +0100 | [diff] [blame] | 3691 | } else if (is_tcf_skbedit_ptype(act)) { |
| 3692 | entry->id = FLOW_ACTION_PTYPE; |
| 3693 | entry->ptype = tcf_skbedit_ptype(act); |
Petr Machata | 2ce1241 | 2020-03-19 15:47:21 +0200 | [diff] [blame] | 3694 | } else if (is_tcf_skbedit_priority(act)) { |
| 3695 | entry->id = FLOW_ACTION_PRIORITY; |
| 3696 | entry->priority = tcf_skbedit_priority(act); |
Po Liu | d29bdd6 | 2020-05-01 08:53:16 +0800 | [diff] [blame] | 3697 | } else if (is_tcf_gate(act)) { |
| 3698 | entry->id = FLOW_ACTION_GATE; |
| 3699 | entry->gate.index = tcf_gate_index(act); |
| 3700 | entry->gate.prio = tcf_gate_prio(act); |
| 3701 | entry->gate.basetime = tcf_gate_basetime(act); |
| 3702 | entry->gate.cycletime = tcf_gate_cycletime(act); |
| 3703 | entry->gate.cycletimeext = tcf_gate_cycletimeext(act); |
| 3704 | entry->gate.num_entries = tcf_gate_num_entries(act); |
| 3705 | err = tcf_gate_get_entries(entry, act); |
| 3706 | if (err) |
| 3707 | goto err_out; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3708 | } else { |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3709 | err = -EOPNOTSUPP; |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3710 | goto err_out_locked; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3711 | } |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3712 | spin_unlock_bh(&act->tcfa_lock); |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3713 | |
| 3714 | if (!is_tcf_pedit(act)) |
| 3715 | j++; |
| 3716 | } |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3717 | |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3718 | err_out: |
Vlad Buslov | 5a6ff4b | 2019-08-26 16:45:04 +0300 | [diff] [blame] | 3719 | if (err) |
| 3720 | tc_cleanup_flow_action(flow_action); |
| 3721 | |
Vlad Buslov | 9838b20 | 2019-08-26 16:45:03 +0300 | [diff] [blame] | 3722 | return err; |
Vlad Buslov | 7a47281 | 2020-02-17 12:12:09 +0200 | [diff] [blame] | 3723 | err_out_locked: |
| 3724 | spin_unlock_bh(&act->tcfa_lock); |
| 3725 | goto err_out; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 3726 | } |
| 3727 | EXPORT_SYMBOL(tc_setup_flow_action); |
| 3728 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 3729 | unsigned int tcf_exts_num_actions(struct tcf_exts *exts) |
| 3730 | { |
| 3731 | unsigned int num_acts = 0; |
| 3732 | struct tc_action *act; |
| 3733 | int i; |
| 3734 | |
| 3735 | tcf_exts_for_each_action(i, act, exts) { |
| 3736 | if (is_tcf_pedit(act)) |
| 3737 | num_acts += tcf_pedit_nkeys(act); |
| 3738 | else |
| 3739 | num_acts++; |
| 3740 | } |
| 3741 | return num_acts; |
| 3742 | } |
| 3743 | EXPORT_SYMBOL(tcf_exts_num_actions); |
| 3744 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 3745 | static __net_init int tcf_net_init(struct net *net) |
| 3746 | { |
| 3747 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 3748 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 3749 | spin_lock_init(&tn->idr_lock); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 3750 | idr_init(&tn->idr); |
| 3751 | return 0; |
| 3752 | } |
| 3753 | |
| 3754 | static void __net_exit tcf_net_exit(struct net *net) |
| 3755 | { |
| 3756 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 3757 | |
| 3758 | idr_destroy(&tn->idr); |
| 3759 | } |
| 3760 | |
| 3761 | static struct pernet_operations tcf_net_ops = { |
| 3762 | .init = tcf_net_init, |
| 3763 | .exit = tcf_net_exit, |
| 3764 | .id = &tcf_net_id, |
| 3765 | .size = sizeof(struct tcf_net), |
| 3766 | }; |
| 3767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3768 | static int __init tc_filter_init(void) |
| 3769 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 3770 | int err; |
| 3771 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 3772 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 3773 | if (!tc_filter_wq) |
| 3774 | return -ENOMEM; |
| 3775 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 3776 | err = register_pernet_subsys(&tcf_net_ops); |
| 3777 | if (err) |
| 3778 | goto err_register_pernet_subsys; |
| 3779 | |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 3780 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, |
| 3781 | RTNL_FLAG_DOIT_UNLOCKED); |
| 3782 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, |
| 3783 | RTNL_FLAG_DOIT_UNLOCKED); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 3784 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, |
Vlad Buslov | 470502d | 2019-02-11 10:55:48 +0200 | [diff] [blame] | 3785 | tc_dump_tfilter, RTNL_FLAG_DOIT_UNLOCKED); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 3786 | rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); |
| 3787 | rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); |
| 3788 | rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, |
| 3789 | tc_dump_chain, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3791 | return 0; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 3792 | |
| 3793 | err_register_pernet_subsys: |
| 3794 | destroy_workqueue(tc_filter_wq); |
| 3795 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | subsys_initcall(tc_filter_init); |