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> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
| 29 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 30 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <net/pkt_sched.h> |
| 32 | #include <net/pkt_cls.h> |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* The list of all installed classifier types */ |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 35 | static LIST_HEAD(tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Protects list of registered TC modules. It is pure SMP lock. */ |
| 38 | static DEFINE_RWLOCK(cls_mod_lock); |
| 39 | |
| 40 | /* Find classifier type by string name */ |
| 41 | |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 42 | 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] | 43 | { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 44 | const struct tcf_proto_ops *t, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | if (kind) { |
| 47 | read_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 48 | list_for_each_entry(t, &tcf_proto_base, head) { |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 49 | if (strcmp(kind, t->kind) == 0) { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 50 | if (try_module_get(t->owner)) |
| 51 | res = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | } |
| 55 | read_unlock(&cls_mod_lock); |
| 56 | } |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 57 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 60 | static const struct tcf_proto_ops * |
| 61 | tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack) |
| 62 | { |
| 63 | const struct tcf_proto_ops *ops; |
| 64 | |
| 65 | ops = __tcf_proto_lookup_ops(kind); |
| 66 | if (ops) |
| 67 | return ops; |
| 68 | #ifdef CONFIG_MODULES |
| 69 | rtnl_unlock(); |
| 70 | request_module("cls_%s", kind); |
| 71 | rtnl_lock(); |
| 72 | ops = __tcf_proto_lookup_ops(kind); |
| 73 | /* We dropped the RTNL semaphore in order to perform |
| 74 | * the module load. So, even if we succeeded in loading |
| 75 | * the module we have to replay the request. We indicate |
| 76 | * this using -EAGAIN. |
| 77 | */ |
| 78 | if (ops) { |
| 79 | module_put(ops->owner); |
| 80 | return ERR_PTR(-EAGAIN); |
| 81 | } |
| 82 | #endif |
| 83 | NL_SET_ERR_MSG(extack, "TC classifier not found"); |
| 84 | return ERR_PTR(-ENOENT); |
| 85 | } |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* Register(unregister) new classifier type */ |
| 88 | |
| 89 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 90 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 91 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int rc = -EEXIST; |
| 93 | |
| 94 | write_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 95 | list_for_each_entry(t, &tcf_proto_base, head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!strcmp(ops->kind, t->kind)) |
| 97 | goto out; |
| 98 | |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 99 | list_add_tail(&ops->head, &tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | rc = 0; |
| 101 | out: |
| 102 | write_unlock(&cls_mod_lock); |
| 103 | return rc; |
| 104 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 105 | EXPORT_SYMBOL(register_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 107 | static struct workqueue_struct *tc_filter_wq; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 110 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 111 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int rc = -ENOENT; |
| 113 | |
Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 114 | /* Wait for outstanding call_rcu()s, if any, from a |
| 115 | * tcf_proto_ops's destroy() handler. |
| 116 | */ |
| 117 | rcu_barrier(); |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 118 | flush_workqueue(tc_filter_wq); |
Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | write_lock(&cls_mod_lock); |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 121 | list_for_each_entry(t, &tcf_proto_base, head) { |
| 122 | if (t == ops) { |
| 123 | list_del(&t->head); |
| 124 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | break; |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | write_unlock(&cls_mod_lock); |
| 129 | return rc; |
| 130 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 131 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 133 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 134 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 135 | INIT_RCU_WORK(rwork, func); |
| 136 | return queue_rcu_work(tc_filter_wq, rwork); |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL(tcf_queue_work); |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* Select new prio value from the range, managed by kernel. */ |
| 141 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 142 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 144 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | if (tp) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 147 | first = tp->prio - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Jiri Pirko | 7961973 | 2017-05-17 11:07:58 +0200 | [diff] [blame] | 149 | return TC_H_MAJ(first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 152 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 153 | u32 prio, struct tcf_chain *chain, |
| 154 | struct netlink_ext_ack *extack) |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 155 | { |
| 156 | struct tcf_proto *tp; |
| 157 | int err; |
| 158 | |
| 159 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); |
| 160 | if (!tp) |
| 161 | return ERR_PTR(-ENOBUFS); |
| 162 | |
Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 163 | tp->ops = tcf_proto_lookup_ops(kind, extack); |
| 164 | if (IS_ERR(tp->ops)) { |
| 165 | err = PTR_ERR(tp->ops); |
Jiri Pirko | d68d75f | 2018-05-11 17:45:32 +0200 | [diff] [blame] | 166 | goto errout; |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 167 | } |
| 168 | tp->classify = tp->ops->classify; |
| 169 | tp->protocol = protocol; |
| 170 | tp->prio = prio; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 171 | tp->chain = chain; |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 172 | |
| 173 | err = tp->ops->init(tp); |
| 174 | if (err) { |
| 175 | module_put(tp->ops->owner); |
| 176 | goto errout; |
| 177 | } |
| 178 | return tp; |
| 179 | |
| 180 | errout: |
| 181 | kfree(tp); |
| 182 | return ERR_PTR(err); |
| 183 | } |
| 184 | |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 185 | static void tcf_proto_destroy(struct tcf_proto *tp, |
| 186 | struct netlink_ext_ack *extack) |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 187 | { |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 188 | tp->ops->destroy(tp, extack); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 189 | module_put(tp->ops->owner); |
| 190 | kfree_rcu(tp, rcu); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 193 | struct tcf_filter_chain_list_item { |
| 194 | struct list_head list; |
| 195 | tcf_chain_head_change_t *chain_head_change; |
| 196 | void *chain_head_change_priv; |
| 197 | }; |
| 198 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 199 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
| 200 | u32 chain_index) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 201 | { |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 202 | struct tcf_chain *chain; |
| 203 | |
| 204 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| 205 | if (!chain) |
| 206 | return NULL; |
| 207 | list_add_tail(&chain->list, &block->chain_list); |
| 208 | chain->block = block; |
| 209 | chain->index = chain_index; |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 210 | chain->refcnt = 1; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 211 | if (!chain->index) |
| 212 | block->chain0.chain = chain; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 213 | return chain; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 216 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, |
| 217 | struct tcf_proto *tp_head) |
| 218 | { |
| 219 | if (item->chain_head_change) |
| 220 | item->chain_head_change(tp_head, item->chain_head_change_priv); |
| 221 | } |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 222 | |
| 223 | static void tcf_chain0_head_change(struct tcf_chain *chain, |
| 224 | struct tcf_proto *tp_head) |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 225 | { |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 226 | struct tcf_filter_chain_list_item *item; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 227 | struct tcf_block *block = chain->block; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 228 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 229 | if (chain->index) |
| 230 | return; |
| 231 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 232 | tcf_chain_head_change_item(item, tp_head); |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 235 | static void tcf_chain_destroy(struct tcf_chain *chain) |
| 236 | { |
Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 237 | struct tcf_block *block = chain->block; |
| 238 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 239 | list_del(&chain->list); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 240 | if (!chain->index) |
| 241 | block->chain0.chain = NULL; |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 242 | kfree(chain); |
Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame] | 243 | if (list_empty(&block->chain_list) && !refcount_read(&block->refcnt)) |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 244 | kfree_rcu(block, rcu); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 245 | } |
Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 246 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 247 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 248 | { |
| 249 | ++chain->refcnt; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 252 | static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 253 | { |
| 254 | /* In case all the references are action references, this |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 255 | * chain should not be shown to the user. |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 256 | */ |
| 257 | return chain->refcnt == chain->action_refcnt; |
| 258 | } |
| 259 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 260 | static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, |
| 261 | u32 chain_index) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 262 | { |
| 263 | struct tcf_chain *chain; |
| 264 | |
| 265 | list_for_each_entry(chain, &block->chain_list, list) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 266 | if (chain->index == chain_index) |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 267 | return chain; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 268 | } |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 273 | u32 seq, u16 flags, int event, bool unicast); |
| 274 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 275 | static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, |
| 276 | u32 chain_index, bool create, |
| 277 | bool by_act) |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 278 | { |
| 279 | struct tcf_chain *chain = tcf_chain_lookup(block, chain_index); |
| 280 | |
| 281 | if (chain) { |
| 282 | tcf_chain_hold(chain); |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 283 | } else { |
| 284 | if (!create) |
| 285 | return NULL; |
| 286 | chain = tcf_chain_create(block, chain_index); |
| 287 | if (!chain) |
| 288 | return NULL; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 289 | } |
Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 290 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 291 | if (by_act) |
| 292 | ++chain->action_refcnt; |
| 293 | |
| 294 | /* Send notification only in case we got the first |
| 295 | * non-action reference. Until then, the chain acts only as |
| 296 | * a placeholder for actions pointing to it and user ought |
| 297 | * not know about them. |
| 298 | */ |
| 299 | if (chain->refcnt - chain->action_refcnt == 1 && !by_act) |
| 300 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 301 | RTM_NEWCHAIN, false); |
| 302 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 303 | return chain; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 304 | } |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 305 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 306 | static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 307 | bool create) |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 308 | { |
| 309 | return __tcf_chain_get(block, chain_index, create, false); |
| 310 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 311 | |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 312 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) |
| 313 | { |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 314 | return __tcf_chain_get(block, chain_index, true, true); |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 315 | } |
| 316 | EXPORT_SYMBOL(tcf_chain_get_by_act); |
| 317 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 318 | static void tc_chain_tmplt_del(struct tcf_chain *chain); |
| 319 | |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 320 | static void __tcf_chain_put(struct tcf_chain *chain, bool by_act) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 321 | { |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 322 | if (by_act) |
| 323 | chain->action_refcnt--; |
| 324 | chain->refcnt--; |
| 325 | |
| 326 | /* The last dropped non-action reference will trigger notification. */ |
| 327 | if (chain->refcnt - chain->action_refcnt == 0 && !by_act) |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 328 | tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false); |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 329 | |
| 330 | if (chain->refcnt == 0) { |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 331 | tc_chain_tmplt_del(chain); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 332 | tcf_chain_destroy(chain); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 333 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 334 | } |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 335 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 336 | static void tcf_chain_put(struct tcf_chain *chain) |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 337 | { |
| 338 | __tcf_chain_put(chain, false); |
| 339 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 340 | |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 341 | void tcf_chain_put_by_act(struct tcf_chain *chain) |
| 342 | { |
Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 343 | __tcf_chain_put(chain, true); |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 344 | } |
| 345 | EXPORT_SYMBOL(tcf_chain_put_by_act); |
| 346 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 347 | static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) |
| 348 | { |
| 349 | if (chain->explicitly_created) |
| 350 | tcf_chain_put(chain); |
| 351 | } |
| 352 | |
Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 353 | static void tcf_chain_flush(struct tcf_chain *chain) |
| 354 | { |
| 355 | struct tcf_proto *tp = rtnl_dereference(chain->filter_chain); |
| 356 | |
| 357 | tcf_chain0_head_change(chain, NULL); |
| 358 | while (tp) { |
| 359 | RCU_INIT_POINTER(chain->filter_chain, tp->next); |
| 360 | tcf_proto_destroy(tp, NULL); |
| 361 | tp = rtnl_dereference(chain->filter_chain); |
| 362 | tcf_chain_put(chain); |
| 363 | } |
| 364 | } |
| 365 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 366 | static bool tcf_block_offload_in_use(struct tcf_block *block) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 367 | { |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 368 | return block->offloadcnt; |
| 369 | } |
| 370 | |
| 371 | static int tcf_block_offload_cmd(struct tcf_block *block, |
| 372 | struct net_device *dev, |
| 373 | struct tcf_block_ext_info *ei, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 374 | enum tc_block_command command, |
| 375 | struct netlink_ext_ack *extack) |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 376 | { |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 377 | struct tc_block_offload bo = {}; |
| 378 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 379 | bo.command = command; |
| 380 | bo.binder_type = ei->binder_type; |
| 381 | bo.block = block; |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 382 | bo.extack = extack; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 383 | return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 386 | 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] | 387 | struct tcf_block_ext_info *ei, |
| 388 | struct netlink_ext_ack *extack) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 389 | { |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 390 | struct net_device *dev = q->dev_queue->dev; |
| 391 | int err; |
| 392 | |
| 393 | if (!dev->netdev_ops->ndo_setup_tc) |
| 394 | goto no_offload_dev_inc; |
| 395 | |
| 396 | /* If tc offload feature is disabled and the block we try to bind |
| 397 | * to already has some offloaded filters, forbid to bind. |
| 398 | */ |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 399 | if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) { |
| 400 | 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] | 401 | return -EOPNOTSUPP; |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 402 | } |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 403 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 404 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 405 | if (err == -EOPNOTSUPP) |
| 406 | goto no_offload_dev_inc; |
| 407 | return err; |
| 408 | |
| 409 | no_offload_dev_inc: |
| 410 | if (tcf_block_offload_in_use(block)) |
| 411 | return -EOPNOTSUPP; |
| 412 | block->nooffloaddevcnt++; |
| 413 | return 0; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, |
| 417 | struct tcf_block_ext_info *ei) |
| 418 | { |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 419 | struct net_device *dev = q->dev_queue->dev; |
| 420 | int err; |
| 421 | |
| 422 | if (!dev->netdev_ops->ndo_setup_tc) |
| 423 | goto no_offload_dev_dec; |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 424 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 425 | if (err == -EOPNOTSUPP) |
| 426 | goto no_offload_dev_dec; |
| 427 | return; |
| 428 | |
| 429 | no_offload_dev_dec: |
| 430 | WARN_ON(block->nooffloaddevcnt-- == 0); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 431 | } |
| 432 | |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 433 | static int |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 434 | tcf_chain0_head_change_cb_add(struct tcf_block *block, |
| 435 | struct tcf_block_ext_info *ei, |
| 436 | struct netlink_ext_ack *extack) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 437 | { |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 438 | struct tcf_chain *chain0 = block->chain0.chain; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 439 | struct tcf_filter_chain_list_item *item; |
| 440 | |
| 441 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 442 | if (!item) { |
| 443 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); |
| 444 | return -ENOMEM; |
| 445 | } |
| 446 | item->chain_head_change = ei->chain_head_change; |
| 447 | item->chain_head_change_priv = ei->chain_head_change_priv; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 448 | if (chain0 && chain0->filter_chain) |
| 449 | tcf_chain_head_change_item(item, chain0->filter_chain); |
| 450 | list_add(&item->list, &block->chain0.filter_chain_list); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 455 | tcf_chain0_head_change_cb_del(struct tcf_block *block, |
| 456 | struct tcf_block_ext_info *ei) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 457 | { |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 458 | struct tcf_chain *chain0 = block->chain0.chain; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 459 | struct tcf_filter_chain_list_item *item; |
| 460 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 461 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) { |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 462 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) || |
| 463 | (item->chain_head_change == ei->chain_head_change && |
| 464 | item->chain_head_change_priv == ei->chain_head_change_priv)) { |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 465 | if (chain0) |
| 466 | tcf_chain_head_change_item(item, NULL); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 467 | list_del(&item->list); |
| 468 | kfree(item); |
| 469 | return; |
| 470 | } |
| 471 | } |
| 472 | WARN_ON(1); |
| 473 | } |
| 474 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 475 | struct tcf_net { |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 476 | spinlock_t idr_lock; /* Protects idr */ |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 477 | struct idr idr; |
| 478 | }; |
| 479 | |
| 480 | static unsigned int tcf_net_id; |
| 481 | |
| 482 | static int tcf_block_insert(struct tcf_block *block, struct net *net, |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 483 | struct netlink_ext_ack *extack) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 484 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 485 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 486 | int err; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 487 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 488 | idr_preload(GFP_KERNEL); |
| 489 | spin_lock(&tn->idr_lock); |
| 490 | err = idr_alloc_u32(&tn->idr, block, &block->index, block->index, |
| 491 | GFP_NOWAIT); |
| 492 | spin_unlock(&tn->idr_lock); |
| 493 | idr_preload_end(); |
| 494 | |
| 495 | return err; |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 496 | } |
| 497 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 498 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 499 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 500 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 501 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 502 | spin_lock(&tn->idr_lock); |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 503 | idr_remove(&tn->idr, block->index); |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 504 | spin_unlock(&tn->idr_lock); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | 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] | 508 | u32 block_index, |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 509 | struct netlink_ext_ack *extack) |
| 510 | { |
| 511 | struct tcf_block *block; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 512 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 513 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 514 | if (!block) { |
| 515 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 516 | return ERR_PTR(-ENOMEM); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 517 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 518 | INIT_LIST_HEAD(&block->chain_list); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 519 | INIT_LIST_HEAD(&block->cb_list); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 520 | INIT_LIST_HEAD(&block->owner_list); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 521 | INIT_LIST_HEAD(&block->chain0.filter_chain_list); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 522 | |
Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame] | 523 | refcount_set(&block->refcnt, 1); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 524 | block->net = net; |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 525 | block->index = block_index; |
| 526 | |
| 527 | /* Don't store q pointer for blocks which are shared */ |
| 528 | if (!tcf_block_shared(block)) |
| 529 | block->q = q; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 530 | return block; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) |
| 534 | { |
| 535 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 536 | |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 537 | return idr_find(&tn->idr, block_index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 540 | static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index) |
| 541 | { |
| 542 | struct tcf_block *block; |
| 543 | |
| 544 | rcu_read_lock(); |
| 545 | block = tcf_block_lookup(net, block_index); |
| 546 | if (block && !refcount_inc_not_zero(&block->refcnt)) |
| 547 | block = NULL; |
| 548 | rcu_read_unlock(); |
| 549 | |
| 550 | return block; |
| 551 | } |
| 552 | |
Vlad Buslov | f002343 | 2018-09-24 19:22:55 +0300 | [diff] [blame] | 553 | static void tcf_block_flush_all_chains(struct tcf_block *block) |
| 554 | { |
| 555 | struct tcf_chain *chain; |
| 556 | |
| 557 | /* Hold a refcnt for all chains, so that they don't disappear |
| 558 | * while we are iterating. |
| 559 | */ |
| 560 | list_for_each_entry(chain, &block->chain_list, list) |
| 561 | tcf_chain_hold(chain); |
| 562 | |
| 563 | list_for_each_entry(chain, &block->chain_list, list) |
| 564 | tcf_chain_flush(chain); |
| 565 | } |
| 566 | |
| 567 | static void tcf_block_put_all_chains(struct tcf_block *block) |
| 568 | { |
| 569 | struct tcf_chain *chain, *tmp; |
| 570 | |
| 571 | /* At this point, all the chains should have refcnt >= 1. */ |
| 572 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) { |
| 573 | tcf_chain_put_explicitly_created(chain); |
| 574 | tcf_chain_put(chain); |
| 575 | } |
| 576 | } |
| 577 | |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 578 | static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q, |
| 579 | struct tcf_block_ext_info *ei) |
| 580 | { |
| 581 | if (refcount_dec_and_test(&block->refcnt)) { |
| 582 | /* Flushing/putting all chains will cause the block to be |
| 583 | * deallocated when last chain is freed. However, if chain_list |
| 584 | * is empty, block has to be manually deallocated. After block |
| 585 | * reference counter reached 0, it is no longer possible to |
| 586 | * increment it or add new chains to block. |
| 587 | */ |
| 588 | bool free_block = list_empty(&block->chain_list); |
| 589 | |
| 590 | if (tcf_block_shared(block)) |
| 591 | tcf_block_remove(block, block->net); |
| 592 | if (!free_block) |
| 593 | tcf_block_flush_all_chains(block); |
| 594 | |
| 595 | if (q) |
| 596 | tcf_block_offload_unbind(block, q, ei); |
| 597 | |
| 598 | if (free_block) |
| 599 | kfree_rcu(block, rcu); |
| 600 | else |
| 601 | tcf_block_put_all_chains(block); |
| 602 | } else if (q) { |
| 603 | tcf_block_offload_unbind(block, q, ei); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | static void tcf_block_refcnt_put(struct tcf_block *block) |
| 608 | { |
| 609 | __tcf_block_put(block, NULL, NULL); |
| 610 | } |
| 611 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 612 | /* Find tcf block. |
| 613 | * Set q, parent, cl when appropriate. |
| 614 | */ |
| 615 | |
| 616 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, |
| 617 | u32 *parent, unsigned long *cl, |
| 618 | int ifindex, u32 block_index, |
| 619 | struct netlink_ext_ack *extack) |
| 620 | { |
| 621 | struct tcf_block *block; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 622 | int err = 0; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 623 | |
| 624 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 625 | block = tcf_block_refcnt_get(net, block_index); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 626 | if (!block) { |
| 627 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); |
| 628 | return ERR_PTR(-EINVAL); |
| 629 | } |
| 630 | } else { |
| 631 | const struct Qdisc_class_ops *cops; |
| 632 | struct net_device *dev; |
| 633 | |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 634 | rcu_read_lock(); |
| 635 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 636 | /* Find link */ |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 637 | dev = dev_get_by_index_rcu(net, ifindex); |
| 638 | if (!dev) { |
| 639 | rcu_read_unlock(); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 640 | return ERR_PTR(-ENODEV); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 641 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 642 | |
| 643 | /* Find qdisc */ |
| 644 | if (!*parent) { |
| 645 | *q = dev->qdisc; |
| 646 | *parent = (*q)->handle; |
| 647 | } else { |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 648 | *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 649 | if (!*q) { |
| 650 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 651 | err = -EINVAL; |
| 652 | goto errout_rcu; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 656 | *q = qdisc_refcount_inc_nz(*q); |
| 657 | if (!*q) { |
| 658 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 659 | err = -EINVAL; |
| 660 | goto errout_rcu; |
| 661 | } |
| 662 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 663 | /* Is it classful? */ |
| 664 | cops = (*q)->ops->cl_ops; |
| 665 | if (!cops) { |
| 666 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 667 | err = -EINVAL; |
| 668 | goto errout_rcu; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | if (!cops->tcf_block) { |
| 672 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 673 | err = -EOPNOTSUPP; |
| 674 | goto errout_rcu; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 675 | } |
| 676 | |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 677 | /* At this point we know that qdisc is not noop_qdisc, |
| 678 | * which means that qdisc holds a reference to net_device |
| 679 | * and we hold a reference to qdisc, so it is safe to release |
| 680 | * rcu read lock. |
| 681 | */ |
| 682 | rcu_read_unlock(); |
| 683 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 684 | /* Do we search for filter, attached to class? */ |
| 685 | if (TC_H_MIN(*parent)) { |
| 686 | *cl = cops->find(*q, *parent); |
| 687 | if (*cl == 0) { |
| 688 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 689 | err = -ENOENT; |
| 690 | goto errout_qdisc; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | |
| 694 | /* And the last stroke */ |
| 695 | block = cops->tcf_block(*q, *cl, extack); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 696 | if (!block) { |
| 697 | err = -EINVAL; |
| 698 | goto errout_qdisc; |
| 699 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 700 | if (tcf_block_shared(block)) { |
| 701 | 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] | 702 | err = -EOPNOTSUPP; |
| 703 | goto errout_qdisc; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 704 | } |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 705 | |
| 706 | /* Always take reference to block in order to support execution |
| 707 | * of rules update path of cls API without rtnl lock. Caller |
| 708 | * must release block when it is finished using it. 'if' block |
| 709 | * of this conditional obtain reference to block by calling |
| 710 | * tcf_block_refcnt_get(). |
| 711 | */ |
| 712 | refcount_inc(&block->refcnt); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | return block; |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 716 | |
| 717 | errout_rcu: |
| 718 | rcu_read_unlock(); |
| 719 | errout_qdisc: |
Cong Wang | 460b360 | 2018-09-27 13:42:19 -0700 | [diff] [blame] | 720 | if (*q) { |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 721 | qdisc_put(*q); |
Cong Wang | 460b360 | 2018-09-27 13:42:19 -0700 | [diff] [blame] | 722 | *q = NULL; |
| 723 | } |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 724 | return ERR_PTR(err); |
| 725 | } |
| 726 | |
| 727 | static void tcf_block_release(struct Qdisc *q, struct tcf_block *block) |
| 728 | { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 729 | if (!IS_ERR_OR_NULL(block)) |
| 730 | tcf_block_refcnt_put(block); |
| 731 | |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 732 | if (q) |
| 733 | qdisc_put(q); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 734 | } |
| 735 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 736 | struct tcf_block_owner_item { |
| 737 | struct list_head list; |
| 738 | struct Qdisc *q; |
| 739 | enum tcf_block_binder_type binder_type; |
| 740 | }; |
| 741 | |
| 742 | static void |
| 743 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, |
| 744 | struct Qdisc *q, |
| 745 | enum tcf_block_binder_type binder_type) |
| 746 | { |
| 747 | if (block->keep_dst && |
| 748 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS && |
| 749 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
| 750 | netif_keep_dst(qdisc_dev(q)); |
| 751 | } |
| 752 | |
| 753 | void tcf_block_netif_keep_dst(struct tcf_block *block) |
| 754 | { |
| 755 | struct tcf_block_owner_item *item; |
| 756 | |
| 757 | block->keep_dst = true; |
| 758 | list_for_each_entry(item, &block->owner_list, list) |
| 759 | tcf_block_owner_netif_keep_dst(block, item->q, |
| 760 | item->binder_type); |
| 761 | } |
| 762 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); |
| 763 | |
| 764 | static int tcf_block_owner_add(struct tcf_block *block, |
| 765 | struct Qdisc *q, |
| 766 | enum tcf_block_binder_type binder_type) |
| 767 | { |
| 768 | struct tcf_block_owner_item *item; |
| 769 | |
| 770 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 771 | if (!item) |
| 772 | return -ENOMEM; |
| 773 | item->q = q; |
| 774 | item->binder_type = binder_type; |
| 775 | list_add(&item->list, &block->owner_list); |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static void tcf_block_owner_del(struct tcf_block *block, |
| 780 | struct Qdisc *q, |
| 781 | enum tcf_block_binder_type binder_type) |
| 782 | { |
| 783 | struct tcf_block_owner_item *item; |
| 784 | |
| 785 | list_for_each_entry(item, &block->owner_list, list) { |
| 786 | if (item->q == q && item->binder_type == binder_type) { |
| 787 | list_del(&item->list); |
| 788 | kfree(item); |
| 789 | return; |
| 790 | } |
| 791 | } |
| 792 | WARN_ON(1); |
| 793 | } |
| 794 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 795 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
| 796 | struct tcf_block_ext_info *ei, |
| 797 | struct netlink_ext_ack *extack) |
| 798 | { |
| 799 | struct net *net = qdisc_net(q); |
| 800 | struct tcf_block *block = NULL; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 801 | int err; |
| 802 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 803 | if (ei->block_index) |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 804 | /* block_index not 0 means the shared block is requested */ |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 805 | block = tcf_block_refcnt_get(net, ei->block_index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 806 | |
| 807 | if (!block) { |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 808 | block = tcf_block_create(net, q, ei->block_index, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 809 | if (IS_ERR(block)) |
| 810 | return PTR_ERR(block); |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 811 | if (tcf_block_shared(block)) { |
| 812 | err = tcf_block_insert(block, net, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 813 | if (err) |
| 814 | goto err_block_insert; |
| 815 | } |
| 816 | } |
| 817 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 818 | err = tcf_block_owner_add(block, q, ei->binder_type); |
| 819 | if (err) |
| 820 | goto err_block_owner_add; |
| 821 | |
| 822 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); |
| 823 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 824 | err = tcf_chain0_head_change_cb_add(block, ei, extack); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 825 | if (err) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 826 | goto err_chain0_head_change_cb_add; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 827 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 828 | err = tcf_block_offload_bind(block, q, ei, extack); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 829 | if (err) |
| 830 | goto err_block_offload_bind; |
| 831 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 832 | *p_block = block; |
| 833 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 834 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 835 | err_block_offload_bind: |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 836 | tcf_chain0_head_change_cb_del(block, ei); |
| 837 | err_chain0_head_change_cb_add: |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 838 | tcf_block_owner_del(block, q, ei->binder_type); |
| 839 | err_block_owner_add: |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 840 | err_block_insert: |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 841 | tcf_block_refcnt_put(block); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 842 | return err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 843 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 844 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 845 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 846 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
| 847 | { |
| 848 | struct tcf_proto __rcu **p_filter_chain = priv; |
| 849 | |
| 850 | rcu_assign_pointer(*p_filter_chain, tp_head); |
| 851 | } |
| 852 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 853 | int tcf_block_get(struct tcf_block **p_block, |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 854 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 855 | struct netlink_ext_ack *extack) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 856 | { |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 857 | struct tcf_block_ext_info ei = { |
| 858 | .chain_head_change = tcf_chain_head_change_dflt, |
| 859 | .chain_head_change_priv = p_filter_chain, |
| 860 | }; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 861 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 862 | WARN_ON(!p_filter_chain); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 863 | return tcf_block_get_ext(p_block, q, &ei, extack); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 864 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 865 | EXPORT_SYMBOL(tcf_block_get); |
| 866 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 867 | /* 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] | 868 | * actions should be all removed after flushing. |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 869 | */ |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 870 | 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] | 871 | struct tcf_block_ext_info *ei) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 872 | { |
David S. Miller | c30abd5 | 2017-12-16 22:11:55 -0500 | [diff] [blame] | 873 | if (!block) |
| 874 | return; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 875 | tcf_chain0_head_change_cb_del(block, ei); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 876 | tcf_block_owner_del(block, q, ei->binder_type); |
Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 877 | |
Vlad Buslov | 0607e43 | 2018-09-24 19:22:57 +0300 | [diff] [blame] | 878 | __tcf_block_put(block, q, ei); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 879 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 880 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 881 | |
| 882 | void tcf_block_put(struct tcf_block *block) |
| 883 | { |
| 884 | struct tcf_block_ext_info ei = {0, }; |
| 885 | |
Jiri Pirko | 4853f12 | 2017-12-21 13:13:59 +0100 | [diff] [blame] | 886 | if (!block) |
| 887 | return; |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 888 | tcf_block_put_ext(block, block->q, &ei); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 889 | } |
David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 890 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 891 | EXPORT_SYMBOL(tcf_block_put); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 892 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 893 | struct tcf_block_cb { |
| 894 | struct list_head list; |
| 895 | tc_setup_cb_t *cb; |
| 896 | void *cb_ident; |
| 897 | void *cb_priv; |
| 898 | unsigned int refcnt; |
| 899 | }; |
| 900 | |
| 901 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) |
| 902 | { |
| 903 | return block_cb->cb_priv; |
| 904 | } |
| 905 | EXPORT_SYMBOL(tcf_block_cb_priv); |
| 906 | |
| 907 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, |
| 908 | tc_setup_cb_t *cb, void *cb_ident) |
| 909 | { struct tcf_block_cb *block_cb; |
| 910 | |
| 911 | list_for_each_entry(block_cb, &block->cb_list, list) |
| 912 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) |
| 913 | return block_cb; |
| 914 | return NULL; |
| 915 | } |
| 916 | EXPORT_SYMBOL(tcf_block_cb_lookup); |
| 917 | |
| 918 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) |
| 919 | { |
| 920 | block_cb->refcnt++; |
| 921 | } |
| 922 | EXPORT_SYMBOL(tcf_block_cb_incref); |
| 923 | |
| 924 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) |
| 925 | { |
| 926 | return --block_cb->refcnt; |
| 927 | } |
| 928 | EXPORT_SYMBOL(tcf_block_cb_decref); |
| 929 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 930 | static int |
| 931 | tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, |
| 932 | void *cb_priv, bool add, bool offload_in_use, |
| 933 | struct netlink_ext_ack *extack) |
| 934 | { |
| 935 | struct tcf_chain *chain; |
| 936 | struct tcf_proto *tp; |
| 937 | int err; |
| 938 | |
| 939 | list_for_each_entry(chain, &block->chain_list, list) { |
| 940 | for (tp = rtnl_dereference(chain->filter_chain); tp; |
| 941 | tp = rtnl_dereference(tp->next)) { |
| 942 | if (tp->ops->reoffload) { |
| 943 | err = tp->ops->reoffload(tp, add, cb, cb_priv, |
| 944 | extack); |
| 945 | if (err && add) |
| 946 | goto err_playback_remove; |
| 947 | } else if (add && offload_in_use) { |
| 948 | err = -EOPNOTSUPP; |
| 949 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); |
| 950 | goto err_playback_remove; |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | |
| 957 | err_playback_remove: |
| 958 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
| 959 | extack); |
| 960 | return err; |
| 961 | } |
| 962 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 963 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, |
| 964 | tc_setup_cb_t *cb, void *cb_ident, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 965 | void *cb_priv, |
| 966 | struct netlink_ext_ack *extack) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 967 | { |
| 968 | struct tcf_block_cb *block_cb; |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 969 | int err; |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 970 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 971 | /* Replay any already present rules */ |
| 972 | err = tcf_block_playback_offloads(block, cb, cb_priv, true, |
| 973 | tcf_block_offload_in_use(block), |
| 974 | extack); |
| 975 | if (err) |
| 976 | return ERR_PTR(err); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 977 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 978 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); |
| 979 | if (!block_cb) |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 980 | return ERR_PTR(-ENOMEM); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 981 | block_cb->cb = cb; |
| 982 | block_cb->cb_ident = cb_ident; |
| 983 | block_cb->cb_priv = cb_priv; |
| 984 | list_add(&block_cb->list, &block->cb_list); |
| 985 | return block_cb; |
| 986 | } |
| 987 | EXPORT_SYMBOL(__tcf_block_cb_register); |
| 988 | |
| 989 | int tcf_block_cb_register(struct tcf_block *block, |
| 990 | tc_setup_cb_t *cb, void *cb_ident, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 991 | void *cb_priv, struct netlink_ext_ack *extack) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 992 | { |
| 993 | struct tcf_block_cb *block_cb; |
| 994 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 995 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv, |
| 996 | extack); |
Gustavo A. R. Silva | baa2d2b | 2018-07-18 23:14:17 -0500 | [diff] [blame] | 997 | return PTR_ERR_OR_ZERO(block_cb); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 998 | } |
| 999 | EXPORT_SYMBOL(tcf_block_cb_register); |
| 1000 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1001 | void __tcf_block_cb_unregister(struct tcf_block *block, |
| 1002 | struct tcf_block_cb *block_cb) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 1003 | { |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1004 | tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv, |
| 1005 | false, tcf_block_offload_in_use(block), |
| 1006 | NULL); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 1007 | list_del(&block_cb->list); |
| 1008 | kfree(block_cb); |
| 1009 | } |
| 1010 | EXPORT_SYMBOL(__tcf_block_cb_unregister); |
| 1011 | |
| 1012 | void tcf_block_cb_unregister(struct tcf_block *block, |
| 1013 | tc_setup_cb_t *cb, void *cb_ident) |
| 1014 | { |
| 1015 | struct tcf_block_cb *block_cb; |
| 1016 | |
| 1017 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); |
| 1018 | if (!block_cb) |
| 1019 | return; |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 1020 | __tcf_block_cb_unregister(block, block_cb); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 1021 | } |
| 1022 | EXPORT_SYMBOL(tcf_block_cb_unregister); |
| 1023 | |
| 1024 | static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 1025 | void *type_data, bool err_stop) |
| 1026 | { |
| 1027 | struct tcf_block_cb *block_cb; |
| 1028 | int ok_count = 0; |
| 1029 | int err; |
| 1030 | |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 1031 | /* Make sure all netdevs sharing this block are offload-capable. */ |
| 1032 | if (block->nooffloaddevcnt && err_stop) |
| 1033 | return -EOPNOTSUPP; |
| 1034 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 1035 | list_for_each_entry(block_cb, &block->cb_list, list) { |
| 1036 | err = block_cb->cb(type, type_data, block_cb->cb_priv); |
| 1037 | if (err) { |
| 1038 | if (err_stop) |
| 1039 | return err; |
| 1040 | } else { |
| 1041 | ok_count++; |
| 1042 | } |
| 1043 | } |
| 1044 | return ok_count; |
| 1045 | } |
| 1046 | |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1047 | /* Main classifier routine: scans classifier chain attached |
| 1048 | * to this qdisc, (optionally) tests for protocol and asks |
| 1049 | * specific classifiers. |
| 1050 | */ |
| 1051 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 1052 | struct tcf_result *res, bool compat_mode) |
| 1053 | { |
| 1054 | __be16 protocol = tc_skb_protocol(skb); |
| 1055 | #ifdef CONFIG_NET_CLS_ACT |
| 1056 | const int max_reclassify_loop = 4; |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1057 | const struct tcf_proto *orig_tp = tp; |
| 1058 | const struct tcf_proto *first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1059 | int limit = 0; |
| 1060 | |
| 1061 | reclassify: |
| 1062 | #endif |
| 1063 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 1064 | int err; |
| 1065 | |
| 1066 | if (tp->protocol != protocol && |
| 1067 | tp->protocol != htons(ETH_P_ALL)) |
| 1068 | continue; |
| 1069 | |
| 1070 | err = tp->classify(skb, tp, res); |
| 1071 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1072 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1073 | first_tp = orig_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1074 | goto reset; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1075 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1076 | first_tp = res->goto_tp; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1077 | goto reset; |
| 1078 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1079 | #endif |
| 1080 | if (err >= 0) |
| 1081 | return err; |
| 1082 | } |
| 1083 | |
| 1084 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 1085 | #ifdef CONFIG_NET_CLS_ACT |
| 1086 | reset: |
| 1087 | if (unlikely(limit++ >= max_reclassify_loop)) { |
Jiri Pirko | 9d3aaff | 2018-01-17 11:46:47 +0100 | [diff] [blame] | 1088 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
| 1089 | tp->chain->block->index, |
| 1090 | tp->prio & 0xffff, |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1091 | ntohs(tp->protocol)); |
| 1092 | return TC_ACT_SHOT; |
| 1093 | } |
| 1094 | |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1095 | tp = first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1096 | protocol = tc_skb_protocol(skb); |
| 1097 | goto reclassify; |
| 1098 | #endif |
| 1099 | } |
| 1100 | EXPORT_SYMBOL(tcf_classify); |
| 1101 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1102 | struct tcf_chain_info { |
| 1103 | struct tcf_proto __rcu **pprev; |
| 1104 | struct tcf_proto __rcu *next; |
| 1105 | }; |
| 1106 | |
| 1107 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 1108 | { |
| 1109 | return rtnl_dereference(*chain_info->pprev); |
| 1110 | } |
| 1111 | |
| 1112 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 1113 | struct tcf_chain_info *chain_info, |
| 1114 | struct tcf_proto *tp) |
| 1115 | { |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1116 | if (*chain_info->pprev == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1117 | tcf_chain0_head_change(chain, tp); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1118 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 1119 | rcu_assign_pointer(*chain_info->pprev, tp); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1120 | tcf_chain_hold(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 1124 | struct tcf_chain_info *chain_info, |
| 1125 | struct tcf_proto *tp) |
| 1126 | { |
| 1127 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 1128 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1129 | if (tp == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1130 | tcf_chain0_head_change(chain, next); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1131 | RCU_INIT_POINTER(*chain_info->pprev, next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1132 | tcf_chain_put(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 1136 | struct tcf_chain_info *chain_info, |
| 1137 | u32 protocol, u32 prio, |
| 1138 | bool prio_allocate) |
| 1139 | { |
| 1140 | struct tcf_proto **pprev; |
| 1141 | struct tcf_proto *tp; |
| 1142 | |
| 1143 | /* Check the chain for existence of proto-tcf with this priority */ |
| 1144 | for (pprev = &chain->filter_chain; |
| 1145 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 1146 | if (tp->prio >= prio) { |
| 1147 | if (tp->prio == prio) { |
| 1148 | if (prio_allocate || |
| 1149 | (tp->protocol != protocol && protocol)) |
| 1150 | return ERR_PTR(-EINVAL); |
| 1151 | } else { |
| 1152 | tp = NULL; |
| 1153 | } |
| 1154 | break; |
| 1155 | } |
| 1156 | } |
| 1157 | chain_info->pprev = pprev; |
| 1158 | chain_info->next = tp ? tp->next : NULL; |
| 1159 | return tp; |
| 1160 | } |
| 1161 | |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1162 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1163 | struct tcf_proto *tp, struct tcf_block *block, |
| 1164 | struct Qdisc *q, u32 parent, void *fh, |
| 1165 | u32 portid, u32 seq, u16 flags, int event) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1166 | { |
| 1167 | struct tcmsg *tcm; |
| 1168 | struct nlmsghdr *nlh; |
| 1169 | unsigned char *b = skb_tail_pointer(skb); |
| 1170 | |
| 1171 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1172 | if (!nlh) |
| 1173 | goto out_nlmsg_trim; |
| 1174 | tcm = nlmsg_data(nlh); |
| 1175 | tcm->tcm_family = AF_UNSPEC; |
| 1176 | tcm->tcm__pad1 = 0; |
| 1177 | tcm->tcm__pad2 = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1178 | if (q) { |
| 1179 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 1180 | tcm->tcm_parent = parent; |
| 1181 | } else { |
| 1182 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1183 | tcm->tcm_block_index = block->index; |
| 1184 | } |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1185 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 1186 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 1187 | goto nla_put_failure; |
| 1188 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 1189 | goto nla_put_failure; |
| 1190 | if (!fh) { |
| 1191 | tcm->tcm_handle = 0; |
| 1192 | } else { |
| 1193 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 1194 | goto nla_put_failure; |
| 1195 | } |
| 1196 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1197 | return skb->len; |
| 1198 | |
| 1199 | out_nlmsg_trim: |
| 1200 | nla_put_failure: |
| 1201 | nlmsg_trim(skb, b); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 1206 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1207 | struct tcf_block *block, struct Qdisc *q, |
| 1208 | u32 parent, void *fh, int event, bool unicast) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1209 | { |
| 1210 | struct sk_buff *skb; |
| 1211 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1212 | |
| 1213 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1214 | if (!skb) |
| 1215 | return -ENOBUFS; |
| 1216 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1217 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1218 | n->nlmsg_seq, n->nlmsg_flags, event) <= 0) { |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1219 | kfree_skb(skb); |
| 1220 | return -EINVAL; |
| 1221 | } |
| 1222 | |
| 1223 | if (unicast) |
| 1224 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1225 | |
| 1226 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1227 | n->nlmsg_flags & NLM_F_ECHO); |
| 1228 | } |
| 1229 | |
| 1230 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 1231 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1232 | struct tcf_block *block, struct Qdisc *q, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1233 | u32 parent, void *fh, bool unicast, bool *last, |
| 1234 | struct netlink_ext_ack *extack) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1235 | { |
| 1236 | struct sk_buff *skb; |
| 1237 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1238 | int err; |
| 1239 | |
| 1240 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1241 | if (!skb) |
| 1242 | return -ENOBUFS; |
| 1243 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1244 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1245 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1246 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1247 | kfree_skb(skb); |
| 1248 | return -EINVAL; |
| 1249 | } |
| 1250 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 1251 | err = tp->ops->delete(tp, fh, last, extack); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1252 | if (err) { |
| 1253 | kfree_skb(skb); |
| 1254 | return err; |
| 1255 | } |
| 1256 | |
| 1257 | if (unicast) |
| 1258 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1259 | |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1260 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1261 | n->nlmsg_flags & NLM_F_ECHO); |
| 1262 | if (err < 0) |
| 1263 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); |
| 1264 | return err; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1268 | struct tcf_block *block, struct Qdisc *q, |
| 1269 | u32 parent, struct nlmsghdr *n, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1270 | struct tcf_chain *chain, int event) |
| 1271 | { |
| 1272 | struct tcf_proto *tp; |
| 1273 | |
| 1274 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1275 | tp; tp = rtnl_dereference(tp->next)) |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1276 | tfilter_notify(net, oskb, n, tp, block, |
YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1277 | q, parent, NULL, event, false); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1280 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1281 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1283 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1284 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | struct tcmsg *t; |
| 1286 | u32 protocol; |
| 1287 | u32 prio; |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1288 | bool prio_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1290 | u32 chain_index; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1291 | struct Qdisc *q = NULL; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1292 | struct tcf_chain_info chain_info; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1293 | struct tcf_chain *chain = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1294 | struct tcf_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | struct tcf_proto *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | unsigned long cl; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1297 | void *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | int err; |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1299 | int tp_created; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1301 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1302 | return -EPERM; |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | replay: |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1305 | tp_created = 0; |
| 1306 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1307 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1308 | if (err < 0) |
| 1309 | return err; |
| 1310 | |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1311 | t = nlmsg_data(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | protocol = TC_H_MIN(t->tcm_info); |
| 1313 | prio = TC_H_MAJ(t->tcm_info); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1314 | prio_allocate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | parent = t->tcm_parent; |
| 1316 | cl = 0; |
| 1317 | |
| 1318 | if (prio == 0) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1319 | /* If no priority is provided by the user, |
| 1320 | * we allocate one. |
| 1321 | */ |
| 1322 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 1323 | prio = TC_H_MAKE(0x80000000U, 0U); |
| 1324 | prio_allocate = true; |
| 1325 | } else { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1326 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | return -ENOENT; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1328 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /* Find head of filter chain. */ |
| 1332 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1333 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1334 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1335 | if (IS_ERR(block)) { |
| 1336 | err = PTR_ERR(block); |
| 1337 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1338 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1339 | |
| 1340 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1341 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1342 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1343 | err = -EINVAL; |
| 1344 | goto errout; |
| 1345 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1346 | chain = tcf_chain_get(block, chain_index, true); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1347 | if (!chain) { |
Jiri Pirko | d5ed72a | 2018-08-27 20:58:43 +0200 | [diff] [blame] | 1348 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1349 | err = -ENOMEM; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1350 | goto errout; |
| 1351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1353 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1354 | prio, prio_allocate); |
| 1355 | if (IS_ERR(tp)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1356 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1357 | err = PTR_ERR(tp); |
| 1358 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | if (tp == NULL) { |
| 1362 | /* Proto-tcf does not exist, create new one */ |
| 1363 | |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1364 | if (tca[TCA_KIND] == NULL || !protocol) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1365 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1366 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1368 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1370 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1371 | 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] | 1372 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1376 | if (prio_allocate) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1377 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1379 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1380 | protocol, prio, chain, extack); |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1381 | if (IS_ERR(tp)) { |
| 1382 | err = PTR_ERR(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | goto errout; |
| 1384 | } |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1385 | tp_created = 1; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1386 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1387 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1388 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
| 1392 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1393 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1394 | if (!fh) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1395 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1396 | 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] | 1397 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1399 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1400 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
| 1401 | NL_SET_ERR_MSG(extack, "Filter already exists"); |
| 1402 | err = -EEXIST; |
| 1403 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1406 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { |
| 1407 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); |
| 1408 | err = -EINVAL; |
| 1409 | goto errout; |
| 1410 | } |
| 1411 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 1412 | 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] | 1413 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
| 1414 | extack); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1415 | if (err == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1416 | if (tp_created) |
| 1417 | tcf_chain_tp_insert(chain, &chain_info, tp); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1418 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1419 | RTM_NEWTFILTER, false); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1420 | } else { |
| 1421 | if (tp_created) |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 1422 | tcf_proto_destroy(tp, NULL); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | |
| 1425 | errout: |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1426 | if (chain) |
| 1427 | tcf_chain_put(chain); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1428 | tcf_block_release(q, block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (err == -EAGAIN) |
| 1430 | /* Replay the request. */ |
| 1431 | goto replay; |
| 1432 | return err; |
| 1433 | } |
| 1434 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1435 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1436 | struct netlink_ext_ack *extack) |
| 1437 | { |
| 1438 | struct net *net = sock_net(skb->sk); |
| 1439 | struct nlattr *tca[TCA_MAX + 1]; |
| 1440 | struct tcmsg *t; |
| 1441 | u32 protocol; |
| 1442 | u32 prio; |
| 1443 | u32 parent; |
| 1444 | u32 chain_index; |
| 1445 | struct Qdisc *q = NULL; |
| 1446 | struct tcf_chain_info chain_info; |
| 1447 | struct tcf_chain *chain = NULL; |
| 1448 | struct tcf_block *block; |
| 1449 | struct tcf_proto *tp = NULL; |
| 1450 | unsigned long cl = 0; |
| 1451 | void *fh = NULL; |
| 1452 | int err; |
| 1453 | |
| 1454 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1455 | return -EPERM; |
| 1456 | |
| 1457 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1458 | if (err < 0) |
| 1459 | return err; |
| 1460 | |
| 1461 | t = nlmsg_data(n); |
| 1462 | protocol = TC_H_MIN(t->tcm_info); |
| 1463 | prio = TC_H_MAJ(t->tcm_info); |
| 1464 | parent = t->tcm_parent; |
| 1465 | |
| 1466 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { |
| 1467 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); |
| 1468 | return -ENOENT; |
| 1469 | } |
| 1470 | |
| 1471 | /* Find head of filter chain. */ |
| 1472 | |
| 1473 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1474 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1475 | if (IS_ERR(block)) { |
| 1476 | err = PTR_ERR(block); |
| 1477 | goto errout; |
| 1478 | } |
| 1479 | |
| 1480 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1481 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1482 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1483 | err = -EINVAL; |
| 1484 | goto errout; |
| 1485 | } |
| 1486 | chain = tcf_chain_get(block, chain_index, false); |
| 1487 | if (!chain) { |
Jiri Pirko | 5ca8a25 | 2018-08-03 11:08:47 +0200 | [diff] [blame] | 1488 | /* User requested flush on non-existent chain. Nothing to do, |
| 1489 | * so just return success. |
| 1490 | */ |
| 1491 | if (prio == 0) { |
| 1492 | err = 0; |
| 1493 | goto errout; |
| 1494 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1495 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
Jiri Pirko | b7b4247 | 2018-08-27 20:58:44 +0200 | [diff] [blame] | 1496 | err = -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1497 | goto errout; |
| 1498 | } |
| 1499 | |
| 1500 | if (prio == 0) { |
| 1501 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1502 | chain, RTM_DELTFILTER); |
| 1503 | tcf_chain_flush(chain); |
| 1504 | err = 0; |
| 1505 | goto errout; |
| 1506 | } |
| 1507 | |
| 1508 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1509 | prio, false); |
| 1510 | if (!tp || IS_ERR(tp)) { |
| 1511 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1512 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1513 | goto errout; |
| 1514 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1515 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1516 | err = -EINVAL; |
| 1517 | goto errout; |
| 1518 | } |
| 1519 | |
| 1520 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1521 | |
| 1522 | if (!fh) { |
| 1523 | if (t->tcm_handle == 0) { |
| 1524 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1525 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| 1526 | RTM_DELTFILTER, false); |
| 1527 | tcf_proto_destroy(tp, extack); |
| 1528 | err = 0; |
| 1529 | } else { |
| 1530 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1531 | err = -ENOENT; |
| 1532 | } |
| 1533 | } else { |
| 1534 | bool last; |
| 1535 | |
| 1536 | err = tfilter_del_notify(net, skb, n, tp, block, |
| 1537 | q, parent, fh, false, &last, |
| 1538 | extack); |
| 1539 | if (err) |
| 1540 | goto errout; |
| 1541 | if (last) { |
| 1542 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1543 | tcf_proto_destroy(tp, extack); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | errout: |
| 1548 | if (chain) |
| 1549 | tcf_chain_put(chain); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1550 | tcf_block_release(q, block); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1551 | return err; |
| 1552 | } |
| 1553 | |
| 1554 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1555 | struct netlink_ext_ack *extack) |
| 1556 | { |
| 1557 | struct net *net = sock_net(skb->sk); |
| 1558 | struct nlattr *tca[TCA_MAX + 1]; |
| 1559 | struct tcmsg *t; |
| 1560 | u32 protocol; |
| 1561 | u32 prio; |
| 1562 | u32 parent; |
| 1563 | u32 chain_index; |
| 1564 | struct Qdisc *q = NULL; |
| 1565 | struct tcf_chain_info chain_info; |
| 1566 | struct tcf_chain *chain = NULL; |
| 1567 | struct tcf_block *block; |
| 1568 | struct tcf_proto *tp = NULL; |
| 1569 | unsigned long cl = 0; |
| 1570 | void *fh = NULL; |
| 1571 | int err; |
| 1572 | |
| 1573 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1574 | if (err < 0) |
| 1575 | return err; |
| 1576 | |
| 1577 | t = nlmsg_data(n); |
| 1578 | protocol = TC_H_MIN(t->tcm_info); |
| 1579 | prio = TC_H_MAJ(t->tcm_info); |
| 1580 | parent = t->tcm_parent; |
| 1581 | |
| 1582 | if (prio == 0) { |
| 1583 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| 1584 | return -ENOENT; |
| 1585 | } |
| 1586 | |
| 1587 | /* Find head of filter chain. */ |
| 1588 | |
| 1589 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1590 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1591 | if (IS_ERR(block)) { |
| 1592 | err = PTR_ERR(block); |
| 1593 | goto errout; |
| 1594 | } |
| 1595 | |
| 1596 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1597 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1598 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1599 | err = -EINVAL; |
| 1600 | goto errout; |
| 1601 | } |
| 1602 | chain = tcf_chain_get(block, chain_index, false); |
| 1603 | if (!chain) { |
| 1604 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1605 | err = -EINVAL; |
| 1606 | goto errout; |
| 1607 | } |
| 1608 | |
| 1609 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1610 | prio, false); |
| 1611 | if (!tp || IS_ERR(tp)) { |
| 1612 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1613 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1614 | goto errout; |
| 1615 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1616 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1617 | err = -EINVAL; |
| 1618 | goto errout; |
| 1619 | } |
| 1620 | |
| 1621 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1622 | |
| 1623 | if (!fh) { |
| 1624 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1625 | err = -ENOENT; |
| 1626 | } else { |
| 1627 | err = tfilter_notify(net, skb, n, tp, block, q, parent, |
| 1628 | fh, RTM_NEWTFILTER, true); |
| 1629 | if (err < 0) |
| 1630 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); |
| 1631 | } |
| 1632 | |
| 1633 | errout: |
| 1634 | if (chain) |
| 1635 | tcf_chain_put(chain); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1636 | tcf_block_release(q, block); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1637 | return err; |
| 1638 | } |
| 1639 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1640 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | struct tcf_walker w; |
| 1642 | struct sk_buff *skb; |
| 1643 | struct netlink_callback *cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1644 | struct tcf_block *block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1645 | struct Qdisc *q; |
| 1646 | u32 parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | }; |
| 1648 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1649 | 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] | 1650 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1651 | struct tcf_dump_args *a = (void *)arg; |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 1652 | struct net *net = sock_net(a->skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1654 | 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] | 1655 | n, NETLINK_CB(a->cb->skb).portid, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1656 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1657 | RTM_NEWTFILTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | } |
| 1659 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1660 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 1661 | struct sk_buff *skb, struct netlink_callback *cb, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1662 | long index_start, long *p_index) |
| 1663 | { |
| 1664 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1665 | struct tcf_block *block = chain->block; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1666 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 1667 | struct tcf_dump_args arg; |
| 1668 | struct tcf_proto *tp; |
| 1669 | |
| 1670 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1671 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 1672 | if (*p_index < index_start) |
| 1673 | continue; |
| 1674 | if (TC_H_MAJ(tcm->tcm_info) && |
| 1675 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 1676 | continue; |
| 1677 | if (TC_H_MIN(tcm->tcm_info) && |
| 1678 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 1679 | continue; |
| 1680 | if (*p_index > index_start) |
| 1681 | memset(&cb->args[1], 0, |
| 1682 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 1683 | if (cb->args[1] == 0) { |
YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1684 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1685 | NETLINK_CB(cb->skb).portid, |
| 1686 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1687 | RTM_NEWTFILTER) <= 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1688 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1689 | |
| 1690 | cb->args[1] = 1; |
| 1691 | } |
| 1692 | if (!tp->ops->walk) |
| 1693 | continue; |
| 1694 | arg.w.fn = tcf_node_dump; |
| 1695 | arg.skb = skb; |
| 1696 | arg.cb = cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1697 | arg.block = block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1698 | arg.q = q; |
| 1699 | arg.parent = parent; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1700 | arg.w.stop = 0; |
| 1701 | arg.w.skip = cb->args[1] - 1; |
| 1702 | arg.w.count = 0; |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1703 | arg.w.cookie = cb->args[2]; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1704 | tp->ops->walk(tp, &arg.w); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1705 | cb->args[2] = arg.w.cookie; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1706 | cb->args[1] = arg.w.count + 1; |
| 1707 | if (arg.w.stop) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1708 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1709 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1710 | return true; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1711 | } |
| 1712 | |
Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 1713 | /* called with RTNL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 1715 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1716 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1717 | struct nlattr *tca[TCA_MAX + 1]; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1718 | struct Qdisc *q = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1719 | struct tcf_block *block; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1720 | struct tcf_chain *chain; |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1721 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1722 | long index_start; |
| 1723 | long index; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1724 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1725 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1727 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | return skb->len; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1729 | |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame^] | 1730 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, |
| 1731 | cb->extack); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1732 | if (err) |
| 1733 | return err; |
| 1734 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1735 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1736 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1737 | if (!block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 1738 | goto out; |
Jiri Pirko | d680b35 | 2018-01-18 16:14:49 +0100 | [diff] [blame] | 1739 | /* If we work with block index, q is NULL and parent value |
| 1740 | * will never be used in the following code. The check |
| 1741 | * in tcf_fill_node prevents it. However, compiler does not |
| 1742 | * see that far, so set parent to zero to silence the warning |
| 1743 | * about parent being uninitialized. |
| 1744 | */ |
| 1745 | parent = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1746 | } else { |
| 1747 | const struct Qdisc_class_ops *cops; |
| 1748 | struct net_device *dev; |
| 1749 | unsigned long cl = 0; |
| 1750 | |
| 1751 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 1752 | if (!dev) |
| 1753 | return skb->len; |
| 1754 | |
| 1755 | parent = tcm->tcm_parent; |
| 1756 | if (!parent) { |
| 1757 | q = dev->qdisc; |
| 1758 | parent = q->handle; |
| 1759 | } else { |
| 1760 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 1761 | } |
| 1762 | if (!q) |
| 1763 | goto out; |
| 1764 | cops = q->ops->cl_ops; |
| 1765 | if (!cops) |
| 1766 | goto out; |
| 1767 | if (!cops->tcf_block) |
| 1768 | goto out; |
| 1769 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 1770 | cl = cops->find(q, tcm->tcm_parent); |
| 1771 | if (cl == 0) |
| 1772 | goto out; |
| 1773 | } |
| 1774 | block = cops->tcf_block(q, cl, NULL); |
| 1775 | if (!block) |
| 1776 | goto out; |
| 1777 | if (tcf_block_shared(block)) |
| 1778 | q = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1781 | index_start = cb->args[0]; |
| 1782 | index = 0; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1783 | |
| 1784 | list_for_each_entry(chain, &block->chain_list, list) { |
| 1785 | if (tca[TCA_CHAIN] && |
| 1786 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 1787 | continue; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1788 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1789 | index_start, &index)) { |
| 1790 | err = -EMSGSIZE; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1791 | break; |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1792 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1793 | } |
| 1794 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 1795 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
| 1796 | tcf_block_refcnt_put(block); |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1797 | cb->args[0] = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | out: |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1800 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 1801 | if (skb->len == 0 && err) |
| 1802 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | return skb->len; |
| 1804 | } |
| 1805 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1806 | static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net, |
| 1807 | struct sk_buff *skb, struct tcf_block *block, |
| 1808 | u32 portid, u32 seq, u16 flags, int event) |
| 1809 | { |
| 1810 | unsigned char *b = skb_tail_pointer(skb); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1811 | const struct tcf_proto_ops *ops; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1812 | struct nlmsghdr *nlh; |
| 1813 | struct tcmsg *tcm; |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1814 | void *priv; |
| 1815 | |
| 1816 | ops = chain->tmplt_ops; |
| 1817 | priv = chain->tmplt_priv; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1818 | |
| 1819 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1820 | if (!nlh) |
| 1821 | goto out_nlmsg_trim; |
| 1822 | tcm = nlmsg_data(nlh); |
| 1823 | tcm->tcm_family = AF_UNSPEC; |
| 1824 | tcm->tcm__pad1 = 0; |
| 1825 | tcm->tcm__pad2 = 0; |
| 1826 | tcm->tcm_handle = 0; |
| 1827 | if (block->q) { |
| 1828 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; |
| 1829 | tcm->tcm_parent = block->q->handle; |
| 1830 | } else { |
| 1831 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1832 | tcm->tcm_block_index = block->index; |
| 1833 | } |
| 1834 | |
| 1835 | if (nla_put_u32(skb, TCA_CHAIN, chain->index)) |
| 1836 | goto nla_put_failure; |
| 1837 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1838 | if (ops) { |
| 1839 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
| 1840 | goto nla_put_failure; |
| 1841 | if (ops->tmplt_dump(skb, net, priv) < 0) |
| 1842 | goto nla_put_failure; |
| 1843 | } |
| 1844 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1845 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1846 | return skb->len; |
| 1847 | |
| 1848 | out_nlmsg_trim: |
| 1849 | nla_put_failure: |
| 1850 | nlmsg_trim(skb, b); |
| 1851 | return -EMSGSIZE; |
| 1852 | } |
| 1853 | |
| 1854 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 1855 | u32 seq, u16 flags, int event, bool unicast) |
| 1856 | { |
| 1857 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1858 | struct tcf_block *block = chain->block; |
| 1859 | struct net *net = block->net; |
| 1860 | struct sk_buff *skb; |
| 1861 | |
| 1862 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1863 | if (!skb) |
| 1864 | return -ENOBUFS; |
| 1865 | |
| 1866 | if (tc_chain_fill_node(chain, net, skb, block, portid, |
| 1867 | seq, flags, event) <= 0) { |
| 1868 | kfree_skb(skb); |
| 1869 | return -EINVAL; |
| 1870 | } |
| 1871 | |
| 1872 | if (unicast) |
| 1873 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1874 | |
| 1875 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); |
| 1876 | } |
| 1877 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1878 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, |
| 1879 | struct nlattr **tca, |
| 1880 | struct netlink_ext_ack *extack) |
| 1881 | { |
| 1882 | const struct tcf_proto_ops *ops; |
| 1883 | void *tmplt_priv; |
| 1884 | |
| 1885 | /* If kind is not set, user did not specify template. */ |
| 1886 | if (!tca[TCA_KIND]) |
| 1887 | return 0; |
| 1888 | |
| 1889 | ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack); |
| 1890 | if (IS_ERR(ops)) |
| 1891 | return PTR_ERR(ops); |
| 1892 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { |
| 1893 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); |
| 1894 | return -EOPNOTSUPP; |
| 1895 | } |
| 1896 | |
| 1897 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack); |
| 1898 | if (IS_ERR(tmplt_priv)) { |
| 1899 | module_put(ops->owner); |
| 1900 | return PTR_ERR(tmplt_priv); |
| 1901 | } |
| 1902 | chain->tmplt_ops = ops; |
| 1903 | chain->tmplt_priv = tmplt_priv; |
| 1904 | return 0; |
| 1905 | } |
| 1906 | |
| 1907 | static void tc_chain_tmplt_del(struct tcf_chain *chain) |
| 1908 | { |
| 1909 | const struct tcf_proto_ops *ops = chain->tmplt_ops; |
| 1910 | |
| 1911 | /* If template ops are set, no work to do for us. */ |
| 1912 | if (!ops) |
| 1913 | return; |
| 1914 | |
| 1915 | ops->tmplt_destroy(chain->tmplt_priv); |
| 1916 | module_put(ops->owner); |
| 1917 | } |
| 1918 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1919 | /* Add/delete/get a chain */ |
| 1920 | |
| 1921 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, |
| 1922 | struct netlink_ext_ack *extack) |
| 1923 | { |
| 1924 | struct net *net = sock_net(skb->sk); |
| 1925 | struct nlattr *tca[TCA_MAX + 1]; |
| 1926 | struct tcmsg *t; |
| 1927 | u32 parent; |
| 1928 | u32 chain_index; |
| 1929 | struct Qdisc *q = NULL; |
| 1930 | struct tcf_chain *chain = NULL; |
| 1931 | struct tcf_block *block; |
| 1932 | unsigned long cl; |
| 1933 | int err; |
| 1934 | |
| 1935 | if (n->nlmsg_type != RTM_GETCHAIN && |
| 1936 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1937 | return -EPERM; |
| 1938 | |
| 1939 | replay: |
| 1940 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1941 | if (err < 0) |
| 1942 | return err; |
| 1943 | |
| 1944 | t = nlmsg_data(n); |
| 1945 | parent = t->tcm_parent; |
| 1946 | cl = 0; |
| 1947 | |
| 1948 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1949 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1950 | if (IS_ERR(block)) |
| 1951 | return PTR_ERR(block); |
| 1952 | |
| 1953 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1954 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1955 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1956 | err = -EINVAL; |
| 1957 | goto errout_block; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1958 | } |
| 1959 | chain = tcf_chain_lookup(block, chain_index); |
| 1960 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
| 1961 | if (chain) { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1962 | if (tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1963 | /* The chain exists only because there is |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1964 | * some action referencing it. |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1965 | */ |
| 1966 | tcf_chain_hold(chain); |
| 1967 | } else { |
| 1968 | NL_SET_ERR_MSG(extack, "Filter chain already exists"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1969 | err = -EEXIST; |
| 1970 | goto errout_block; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1971 | } |
| 1972 | } else { |
| 1973 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| 1974 | 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] | 1975 | err = -ENOENT; |
| 1976 | goto errout_block; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1977 | } |
| 1978 | chain = tcf_chain_create(block, chain_index); |
| 1979 | if (!chain) { |
| 1980 | NL_SET_ERR_MSG(extack, "Failed to create filter chain"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1981 | err = -ENOMEM; |
| 1982 | goto errout_block; |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1983 | } |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1984 | } |
| 1985 | } else { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1986 | if (!chain || tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1987 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1988 | err = -EINVAL; |
| 1989 | goto errout_block; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1990 | } |
| 1991 | tcf_chain_hold(chain); |
| 1992 | } |
| 1993 | |
| 1994 | switch (n->nlmsg_type) { |
| 1995 | case RTM_NEWCHAIN: |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1996 | err = tc_chain_tmplt_add(chain, net, tca, extack); |
| 1997 | if (err) |
| 1998 | goto errout; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1999 | /* In case the chain was successfully added, take a reference |
| 2000 | * to the chain. This ensures that an empty chain |
| 2001 | * does not disappear at the end of this function. |
| 2002 | */ |
| 2003 | tcf_chain_hold(chain); |
| 2004 | chain->explicitly_created = true; |
| 2005 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 2006 | RTM_NEWCHAIN, false); |
| 2007 | break; |
| 2008 | case RTM_DELCHAIN: |
Cong Wang | f5b9bac | 2018-09-11 14:22:23 -0700 | [diff] [blame] | 2009 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 2010 | chain, RTM_DELTFILTER); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2011 | /* Flush the chain first as the user requested chain removal. */ |
| 2012 | tcf_chain_flush(chain); |
| 2013 | /* In case the chain was successfully deleted, put a reference |
| 2014 | * to the chain previously taken during addition. |
| 2015 | */ |
| 2016 | tcf_chain_put_explicitly_created(chain); |
Jiri Pirko | c921d7d | 2018-07-26 18:27:58 +0200 | [diff] [blame] | 2017 | chain->explicitly_created = false; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2018 | break; |
| 2019 | case RTM_GETCHAIN: |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2020 | err = tc_chain_notify(chain, skb, n->nlmsg_seq, |
| 2021 | n->nlmsg_seq, n->nlmsg_type, true); |
| 2022 | if (err < 0) |
| 2023 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); |
| 2024 | break; |
| 2025 | default: |
| 2026 | err = -EOPNOTSUPP; |
| 2027 | NL_SET_ERR_MSG(extack, "Unsupported message type"); |
| 2028 | goto errout; |
| 2029 | } |
| 2030 | |
| 2031 | errout: |
| 2032 | tcf_chain_put(chain); |
Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 2033 | errout_block: |
| 2034 | tcf_block_release(q, block); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2035 | if (err == -EAGAIN) |
| 2036 | /* Replay the request. */ |
| 2037 | goto replay; |
| 2038 | return err; |
| 2039 | } |
| 2040 | |
| 2041 | /* called with RTNL */ |
| 2042 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) |
| 2043 | { |
| 2044 | struct net *net = sock_net(skb->sk); |
| 2045 | struct nlattr *tca[TCA_MAX + 1]; |
| 2046 | struct Qdisc *q = NULL; |
| 2047 | struct tcf_block *block; |
| 2048 | struct tcf_chain *chain; |
| 2049 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 2050 | long index_start; |
| 2051 | long index; |
| 2052 | u32 parent; |
| 2053 | int err; |
| 2054 | |
| 2055 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| 2056 | return skb->len; |
| 2057 | |
David Ahern | dac9c97 | 2018-10-07 20:16:24 -0700 | [diff] [blame^] | 2058 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, |
| 2059 | cb->extack); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2060 | if (err) |
| 2061 | return err; |
| 2062 | |
| 2063 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 2064 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2065 | if (!block) |
| 2066 | goto out; |
| 2067 | /* If we work with block index, q is NULL and parent value |
| 2068 | * will never be used in the following code. The check |
| 2069 | * in tcf_fill_node prevents it. However, compiler does not |
| 2070 | * see that far, so set parent to zero to silence the warning |
| 2071 | * about parent being uninitialized. |
| 2072 | */ |
| 2073 | parent = 0; |
| 2074 | } else { |
| 2075 | const struct Qdisc_class_ops *cops; |
| 2076 | struct net_device *dev; |
| 2077 | unsigned long cl = 0; |
| 2078 | |
| 2079 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 2080 | if (!dev) |
| 2081 | return skb->len; |
| 2082 | |
| 2083 | parent = tcm->tcm_parent; |
| 2084 | if (!parent) { |
| 2085 | q = dev->qdisc; |
| 2086 | parent = q->handle; |
| 2087 | } else { |
| 2088 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 2089 | } |
| 2090 | if (!q) |
| 2091 | goto out; |
| 2092 | cops = q->ops->cl_ops; |
| 2093 | if (!cops) |
| 2094 | goto out; |
| 2095 | if (!cops->tcf_block) |
| 2096 | goto out; |
| 2097 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 2098 | cl = cops->find(q, tcm->tcm_parent); |
| 2099 | if (cl == 0) |
| 2100 | goto out; |
| 2101 | } |
| 2102 | block = cops->tcf_block(q, cl, NULL); |
| 2103 | if (!block) |
| 2104 | goto out; |
| 2105 | if (tcf_block_shared(block)) |
| 2106 | q = NULL; |
| 2107 | } |
| 2108 | |
| 2109 | index_start = cb->args[0]; |
| 2110 | index = 0; |
| 2111 | |
| 2112 | list_for_each_entry(chain, &block->chain_list, list) { |
| 2113 | if ((tca[TCA_CHAIN] && |
| 2114 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)) |
| 2115 | continue; |
| 2116 | if (index < index_start) { |
| 2117 | index++; |
| 2118 | continue; |
| 2119 | } |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2120 | if (tcf_chain_held_by_acts_only(chain)) |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2121 | continue; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2122 | err = tc_chain_fill_node(chain, net, skb, block, |
| 2123 | NETLINK_CB(cb->skb).portid, |
| 2124 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 2125 | RTM_NEWCHAIN); |
| 2126 | if (err <= 0) |
| 2127 | break; |
| 2128 | index++; |
| 2129 | } |
| 2130 | |
Vlad Buslov | 787ce6d | 2018-09-24 19:22:58 +0300 | [diff] [blame] | 2131 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
| 2132 | tcf_block_refcnt_put(block); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2133 | cb->args[0] = index; |
| 2134 | |
| 2135 | out: |
| 2136 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 2137 | if (skb->len == 0 && err) |
| 2138 | return err; |
| 2139 | return skb->len; |
| 2140 | } |
| 2141 | |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 2142 | void tcf_exts_destroy(struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | { |
| 2144 | #ifdef CONFIG_NET_CLS_ACT |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2145 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2146 | kfree(exts->actions); |
| 2147 | exts->nr_actions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | #endif |
| 2149 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2150 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2151 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 2152 | 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] | 2153 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
| 2154 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | #ifdef CONFIG_NET_CLS_ACT |
| 2157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | struct tc_action *act; |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 2159 | size_t attr_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2161 | if (exts->police && tb[exts->police]) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2162 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 2163 | rate_tlv, "police", ovr, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2164 | TCA_ACT_BIND, true, extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 2165 | if (IS_ERR(act)) |
| 2166 | return PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2168 | act->type = exts->type = TCA_OLD_COMPAT; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2169 | exts->actions[0] = act; |
| 2170 | exts->nr_actions = 1; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2171 | } else if (exts->action && tb[exts->action]) { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2172 | int err; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2173 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2174 | err = tcf_action_init(net, tp, tb[exts->action], |
| 2175 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2176 | exts->actions, &attr_size, true, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2177 | extack); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2178 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2179 | return err; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2180 | exts->nr_actions = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2181 | } |
Cong Wang | e4b95c4 | 2017-11-06 13:47:19 -0800 | [diff] [blame] | 2182 | exts->net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | #else |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2185 | if ((exts->action && tb[exts->action]) || |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2186 | (exts->police && tb[exts->police])) { |
| 2187 | 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] | 2188 | return -EOPNOTSUPP; |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2189 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | #endif |
| 2191 | |
| 2192 | return 0; |
| 2193 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2194 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2196 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | { |
| 2198 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2199 | struct tcf_exts old = *dst; |
| 2200 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2201 | *dst = *src; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2202 | tcf_exts_destroy(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | #endif |
| 2204 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2205 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2206 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2207 | #ifdef CONFIG_NET_CLS_ACT |
| 2208 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 2209 | { |
| 2210 | if (exts->nr_actions == 0) |
| 2211 | return NULL; |
| 2212 | else |
| 2213 | return exts->actions[0]; |
| 2214 | } |
| 2215 | #endif |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2216 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2217 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | { |
| 2219 | #ifdef CONFIG_NET_CLS_ACT |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2220 | struct nlattr *nest; |
| 2221 | |
Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 2222 | if (exts->action && tcf_exts_has_actions(exts)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | /* |
| 2224 | * again for backward compatible mode - we want |
| 2225 | * to work with both old and new modes of entering |
| 2226 | * tc data even if iproute2 was newer - jhs |
| 2227 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2228 | if (exts->type != TCA_OLD_COMPAT) { |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2229 | nest = nla_nest_start(skb, exts->action); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2230 | if (nest == NULL) |
| 2231 | goto nla_put_failure; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2232 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2233 | if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2234 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2235 | nla_nest_end(skb, nest); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2236 | } else if (exts->police) { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2237 | struct tc_action *act = tcf_exts_first_act(exts); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2238 | nest = nla_nest_start(skb, exts->police); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 2239 | if (nest == NULL || !act) |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2240 | goto nla_put_failure; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2241 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2242 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2243 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | } |
| 2245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | return 0; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2247 | |
| 2248 | nla_put_failure: |
| 2249 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | return -1; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2251 | #else |
| 2252 | return 0; |
| 2253 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2255 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2257 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2258 | 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] | 2259 | { |
| 2260 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2261 | struct tc_action *a = tcf_exts_first_act(exts); |
Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 2262 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2263 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | #endif |
| 2265 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2266 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2267 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2269 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
| 2270 | enum tc_setup_type type, |
| 2271 | void *type_data, bool err_stop) |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2272 | { |
| 2273 | int ok_count = 0; |
| 2274 | #ifdef CONFIG_NET_CLS_ACT |
| 2275 | const struct tc_action *a; |
| 2276 | struct net_device *dev; |
Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2277 | int i, ret; |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2278 | |
| 2279 | if (!tcf_exts_has_actions(exts)) |
| 2280 | return 0; |
| 2281 | |
Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2282 | for (i = 0; i < exts->nr_actions; i++) { |
| 2283 | a = exts->actions[i]; |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2284 | if (!a->ops->get_dev) |
| 2285 | continue; |
| 2286 | dev = a->ops->get_dev(a); |
Jiri Pirko | 7612fb0 | 2017-11-01 11:47:40 +0100 | [diff] [blame] | 2287 | if (!dev) |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2288 | continue; |
| 2289 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame] | 2290 | a->ops->put_dev(dev); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2291 | if (ret < 0) |
| 2292 | return ret; |
| 2293 | ok_count += ret; |
| 2294 | } |
| 2295 | #endif |
| 2296 | return ok_count; |
| 2297 | } |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2298 | |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2299 | int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, |
| 2300 | enum tc_setup_type type, void *type_data, bool err_stop) |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2301 | { |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2302 | int ok_count; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2303 | int ret; |
| 2304 | |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2305 | ret = tcf_block_cb_call(block, type, type_data, err_stop); |
| 2306 | if (ret < 0) |
| 2307 | return ret; |
| 2308 | ok_count = ret; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2309 | |
Or Gerlitz | f8f4bef | 2018-05-23 19:24:48 +0300 | [diff] [blame] | 2310 | if (!exts || ok_count) |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2311 | return ok_count; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2312 | ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); |
| 2313 | if (ret < 0) |
| 2314 | return ret; |
| 2315 | ok_count += ret; |
| 2316 | |
| 2317 | return ok_count; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2318 | } |
| 2319 | EXPORT_SYMBOL(tc_setup_cb_call); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2320 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2321 | static __net_init int tcf_net_init(struct net *net) |
| 2322 | { |
| 2323 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2324 | |
Vlad Buslov | ab28162 | 2018-09-24 19:22:56 +0300 | [diff] [blame] | 2325 | spin_lock_init(&tn->idr_lock); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2326 | idr_init(&tn->idr); |
| 2327 | return 0; |
| 2328 | } |
| 2329 | |
| 2330 | static void __net_exit tcf_net_exit(struct net *net) |
| 2331 | { |
| 2332 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2333 | |
| 2334 | idr_destroy(&tn->idr); |
| 2335 | } |
| 2336 | |
| 2337 | static struct pernet_operations tcf_net_ops = { |
| 2338 | .init = tcf_net_init, |
| 2339 | .exit = tcf_net_exit, |
| 2340 | .id = &tcf_net_id, |
| 2341 | .size = sizeof(struct tcf_net), |
| 2342 | }; |
| 2343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | static int __init tc_filter_init(void) |
| 2345 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2346 | int err; |
| 2347 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 2348 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 2349 | if (!tc_filter_wq) |
| 2350 | return -ENOMEM; |
| 2351 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2352 | err = register_pernet_subsys(&tcf_net_ops); |
| 2353 | if (err) |
| 2354 | goto err_register_pernet_subsys; |
| 2355 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2356 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0); |
| 2357 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0); |
| 2358 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 2359 | tc_dump_tfilter, 0); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2360 | rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); |
| 2361 | rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); |
| 2362 | rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, |
| 2363 | tc_dump_chain, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | return 0; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2366 | |
| 2367 | err_register_pernet_subsys: |
| 2368 | destroy_workqueue(tc_filter_wq); |
| 2369 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | subsys_initcall(tc_filter_init); |