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