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