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