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); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 245 | if (list_empty(&block->chain_list) && block->refcnt == 0) |
Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 246 | kfree(block); |
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 { |
| 478 | struct idr idr; |
| 479 | }; |
| 480 | |
| 481 | static unsigned int tcf_net_id; |
| 482 | |
| 483 | static int tcf_block_insert(struct tcf_block *block, struct net *net, |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 484 | struct netlink_ext_ack *extack) |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 485 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 486 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 487 | |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 488 | return idr_alloc_u32(&tn->idr, block, &block->index, block->index, |
| 489 | GFP_KERNEL); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 492 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 493 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 494 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 495 | |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 496 | idr_remove(&tn->idr, block->index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | 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] | 500 | u32 block_index, |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 501 | struct netlink_ext_ack *extack) |
| 502 | { |
| 503 | struct tcf_block *block; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 504 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 505 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 506 | if (!block) { |
| 507 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 508 | return ERR_PTR(-ENOMEM); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 509 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 510 | INIT_LIST_HEAD(&block->chain_list); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 511 | INIT_LIST_HEAD(&block->cb_list); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 512 | INIT_LIST_HEAD(&block->owner_list); |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 513 | INIT_LIST_HEAD(&block->chain0.filter_chain_list); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 514 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 515 | block->refcnt = 1; |
| 516 | block->net = net; |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 517 | block->index = block_index; |
| 518 | |
| 519 | /* Don't store q pointer for blocks which are shared */ |
| 520 | if (!tcf_block_shared(block)) |
| 521 | block->q = q; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 522 | return block; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) |
| 526 | { |
| 527 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 528 | |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 529 | return idr_find(&tn->idr, block_index); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 532 | /* Find tcf block. |
| 533 | * Set q, parent, cl when appropriate. |
| 534 | */ |
| 535 | |
| 536 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, |
| 537 | u32 *parent, unsigned long *cl, |
| 538 | int ifindex, u32 block_index, |
| 539 | struct netlink_ext_ack *extack) |
| 540 | { |
| 541 | struct tcf_block *block; |
| 542 | |
| 543 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 544 | block = tcf_block_lookup(net, block_index); |
| 545 | if (!block) { |
| 546 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); |
| 547 | return ERR_PTR(-EINVAL); |
| 548 | } |
| 549 | } else { |
| 550 | const struct Qdisc_class_ops *cops; |
| 551 | struct net_device *dev; |
| 552 | |
| 553 | /* Find link */ |
| 554 | dev = __dev_get_by_index(net, ifindex); |
| 555 | if (!dev) |
| 556 | return ERR_PTR(-ENODEV); |
| 557 | |
| 558 | /* Find qdisc */ |
| 559 | if (!*parent) { |
| 560 | *q = dev->qdisc; |
| 561 | *parent = (*q)->handle; |
| 562 | } else { |
| 563 | *q = qdisc_lookup(dev, TC_H_MAJ(*parent)); |
| 564 | if (!*q) { |
| 565 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 566 | return ERR_PTR(-EINVAL); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* Is it classful? */ |
| 571 | cops = (*q)->ops->cl_ops; |
| 572 | if (!cops) { |
| 573 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); |
| 574 | return ERR_PTR(-EINVAL); |
| 575 | } |
| 576 | |
| 577 | if (!cops->tcf_block) { |
| 578 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); |
| 579 | return ERR_PTR(-EOPNOTSUPP); |
| 580 | } |
| 581 | |
| 582 | /* Do we search for filter, attached to class? */ |
| 583 | if (TC_H_MIN(*parent)) { |
| 584 | *cl = cops->find(*q, *parent); |
| 585 | if (*cl == 0) { |
| 586 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); |
| 587 | return ERR_PTR(-ENOENT); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* And the last stroke */ |
| 592 | block = cops->tcf_block(*q, *cl, extack); |
| 593 | if (!block) |
| 594 | return ERR_PTR(-EINVAL); |
| 595 | if (tcf_block_shared(block)) { |
| 596 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); |
| 597 | return ERR_PTR(-EOPNOTSUPP); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | return block; |
| 602 | } |
| 603 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 604 | struct tcf_block_owner_item { |
| 605 | struct list_head list; |
| 606 | struct Qdisc *q; |
| 607 | enum tcf_block_binder_type binder_type; |
| 608 | }; |
| 609 | |
| 610 | static void |
| 611 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, |
| 612 | struct Qdisc *q, |
| 613 | enum tcf_block_binder_type binder_type) |
| 614 | { |
| 615 | if (block->keep_dst && |
| 616 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS && |
| 617 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
| 618 | netif_keep_dst(qdisc_dev(q)); |
| 619 | } |
| 620 | |
| 621 | void tcf_block_netif_keep_dst(struct tcf_block *block) |
| 622 | { |
| 623 | struct tcf_block_owner_item *item; |
| 624 | |
| 625 | block->keep_dst = true; |
| 626 | list_for_each_entry(item, &block->owner_list, list) |
| 627 | tcf_block_owner_netif_keep_dst(block, item->q, |
| 628 | item->binder_type); |
| 629 | } |
| 630 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); |
| 631 | |
| 632 | static int tcf_block_owner_add(struct tcf_block *block, |
| 633 | struct Qdisc *q, |
| 634 | enum tcf_block_binder_type binder_type) |
| 635 | { |
| 636 | struct tcf_block_owner_item *item; |
| 637 | |
| 638 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 639 | if (!item) |
| 640 | return -ENOMEM; |
| 641 | item->q = q; |
| 642 | item->binder_type = binder_type; |
| 643 | list_add(&item->list, &block->owner_list); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static void tcf_block_owner_del(struct tcf_block *block, |
| 648 | struct Qdisc *q, |
| 649 | enum tcf_block_binder_type binder_type) |
| 650 | { |
| 651 | struct tcf_block_owner_item *item; |
| 652 | |
| 653 | list_for_each_entry(item, &block->owner_list, list) { |
| 654 | if (item->q == q && item->binder_type == binder_type) { |
| 655 | list_del(&item->list); |
| 656 | kfree(item); |
| 657 | return; |
| 658 | } |
| 659 | } |
| 660 | WARN_ON(1); |
| 661 | } |
| 662 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 663 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
| 664 | struct tcf_block_ext_info *ei, |
| 665 | struct netlink_ext_ack *extack) |
| 666 | { |
| 667 | struct net *net = qdisc_net(q); |
| 668 | struct tcf_block *block = NULL; |
| 669 | bool created = false; |
| 670 | int err; |
| 671 | |
| 672 | if (ei->block_index) { |
| 673 | /* block_index not 0 means the shared block is requested */ |
| 674 | block = tcf_block_lookup(net, ei->block_index); |
| 675 | if (block) |
| 676 | block->refcnt++; |
| 677 | } |
| 678 | |
| 679 | if (!block) { |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 680 | block = tcf_block_create(net, q, ei->block_index, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 681 | if (IS_ERR(block)) |
| 682 | return PTR_ERR(block); |
| 683 | created = true; |
Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 684 | if (tcf_block_shared(block)) { |
| 685 | err = tcf_block_insert(block, net, extack); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 686 | if (err) |
| 687 | goto err_block_insert; |
| 688 | } |
| 689 | } |
| 690 | |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 691 | err = tcf_block_owner_add(block, q, ei->binder_type); |
| 692 | if (err) |
| 693 | goto err_block_owner_add; |
| 694 | |
| 695 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); |
| 696 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 697 | err = tcf_chain0_head_change_cb_add(block, ei, extack); |
Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 698 | if (err) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 699 | goto err_chain0_head_change_cb_add; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 700 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 701 | err = tcf_block_offload_bind(block, q, ei, extack); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 702 | if (err) |
| 703 | goto err_block_offload_bind; |
| 704 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 705 | *p_block = block; |
| 706 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 707 | |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 708 | err_block_offload_bind: |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 709 | tcf_chain0_head_change_cb_del(block, ei); |
| 710 | err_chain0_head_change_cb_add: |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 711 | tcf_block_owner_del(block, q, ei->binder_type); |
| 712 | err_block_owner_add: |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 713 | if (created) { |
| 714 | if (tcf_block_shared(block)) |
| 715 | tcf_block_remove(block, net); |
| 716 | err_block_insert: |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 717 | kfree(block); |
| 718 | } else { |
| 719 | block->refcnt--; |
| 720 | } |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 721 | return err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 722 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 723 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 724 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 725 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
| 726 | { |
| 727 | struct tcf_proto __rcu **p_filter_chain = priv; |
| 728 | |
| 729 | rcu_assign_pointer(*p_filter_chain, tp_head); |
| 730 | } |
| 731 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 732 | int tcf_block_get(struct tcf_block **p_block, |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 733 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 734 | struct netlink_ext_ack *extack) |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 735 | { |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 736 | struct tcf_block_ext_info ei = { |
| 737 | .chain_head_change = tcf_chain_head_change_dflt, |
| 738 | .chain_head_change_priv = p_filter_chain, |
| 739 | }; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 740 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 741 | WARN_ON(!p_filter_chain); |
Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 742 | return tcf_block_get_ext(p_block, q, &ei, extack); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 743 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 744 | EXPORT_SYMBOL(tcf_block_get); |
| 745 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 746 | /* 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] | 747 | * actions should be all removed after flushing. |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 748 | */ |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 749 | 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] | 750 | struct tcf_block_ext_info *ei) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 751 | { |
Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 752 | struct tcf_chain *chain, *tmp; |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 753 | |
David S. Miller | c30abd5 | 2017-12-16 22:11:55 -0500 | [diff] [blame] | 754 | if (!block) |
| 755 | return; |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 756 | tcf_chain0_head_change_cb_del(block, ei); |
Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 757 | tcf_block_owner_del(block, q, ei->binder_type); |
Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 758 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 759 | if (block->refcnt == 1) { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 760 | if (tcf_block_shared(block)) |
| 761 | tcf_block_remove(block, block->net); |
| 762 | |
| 763 | /* Hold a refcnt for all chains, so that they don't disappear |
| 764 | * while we are iterating. |
| 765 | */ |
| 766 | list_for_each_entry(chain, &block->chain_list, list) |
| 767 | tcf_chain_hold(chain); |
| 768 | |
| 769 | list_for_each_entry(chain, &block->chain_list, list) |
| 770 | tcf_chain_flush(chain); |
| 771 | } |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 772 | |
Jiri Pirko | 4bb1b11 | 2017-11-02 15:07:01 +0100 | [diff] [blame] | 773 | tcf_block_offload_unbind(block, q, ei); |
| 774 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 775 | if (block->refcnt == 1) { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 776 | /* At this point, all the chains should have refcnt >= 1. */ |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 777 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) { |
| 778 | tcf_chain_put_explicitly_created(chain); |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 779 | tcf_chain_put(chain); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 780 | } |
Jiri Pirko | df45bf8 | 2017-12-08 19:27:27 +0100 | [diff] [blame] | 781 | |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 782 | block->refcnt--; |
| 783 | if (list_empty(&block->chain_list)) |
| 784 | kfree(block); |
Jiri Pirko | 63cc5bc | 2018-08-08 14:04:13 +0200 | [diff] [blame] | 785 | } else { |
| 786 | block->refcnt--; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 787 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 788 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 789 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 790 | |
| 791 | void tcf_block_put(struct tcf_block *block) |
| 792 | { |
| 793 | struct tcf_block_ext_info ei = {0, }; |
| 794 | |
Jiri Pirko | 4853f12 | 2017-12-21 13:13:59 +0100 | [diff] [blame] | 795 | if (!block) |
| 796 | return; |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 797 | tcf_block_put_ext(block, block->q, &ei); |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 798 | } |
David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 799 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 800 | EXPORT_SYMBOL(tcf_block_put); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 801 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 802 | struct tcf_block_cb { |
| 803 | struct list_head list; |
| 804 | tc_setup_cb_t *cb; |
| 805 | void *cb_ident; |
| 806 | void *cb_priv; |
| 807 | unsigned int refcnt; |
| 808 | }; |
| 809 | |
| 810 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) |
| 811 | { |
| 812 | return block_cb->cb_priv; |
| 813 | } |
| 814 | EXPORT_SYMBOL(tcf_block_cb_priv); |
| 815 | |
| 816 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, |
| 817 | tc_setup_cb_t *cb, void *cb_ident) |
| 818 | { struct tcf_block_cb *block_cb; |
| 819 | |
| 820 | list_for_each_entry(block_cb, &block->cb_list, list) |
| 821 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) |
| 822 | return block_cb; |
| 823 | return NULL; |
| 824 | } |
| 825 | EXPORT_SYMBOL(tcf_block_cb_lookup); |
| 826 | |
| 827 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) |
| 828 | { |
| 829 | block_cb->refcnt++; |
| 830 | } |
| 831 | EXPORT_SYMBOL(tcf_block_cb_incref); |
| 832 | |
| 833 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) |
| 834 | { |
| 835 | return --block_cb->refcnt; |
| 836 | } |
| 837 | EXPORT_SYMBOL(tcf_block_cb_decref); |
| 838 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 839 | static int |
| 840 | tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, |
| 841 | void *cb_priv, bool add, bool offload_in_use, |
| 842 | struct netlink_ext_ack *extack) |
| 843 | { |
| 844 | struct tcf_chain *chain; |
| 845 | struct tcf_proto *tp; |
| 846 | int err; |
| 847 | |
| 848 | list_for_each_entry(chain, &block->chain_list, list) { |
| 849 | for (tp = rtnl_dereference(chain->filter_chain); tp; |
| 850 | tp = rtnl_dereference(tp->next)) { |
| 851 | if (tp->ops->reoffload) { |
| 852 | err = tp->ops->reoffload(tp, add, cb, cb_priv, |
| 853 | extack); |
| 854 | if (err && add) |
| 855 | goto err_playback_remove; |
| 856 | } else if (add && offload_in_use) { |
| 857 | err = -EOPNOTSUPP; |
| 858 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); |
| 859 | goto err_playback_remove; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | |
| 866 | err_playback_remove: |
| 867 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
| 868 | extack); |
| 869 | return err; |
| 870 | } |
| 871 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 872 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, |
| 873 | tc_setup_cb_t *cb, void *cb_ident, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 874 | void *cb_priv, |
| 875 | struct netlink_ext_ack *extack) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 876 | { |
| 877 | struct tcf_block_cb *block_cb; |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 878 | int err; |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 879 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 880 | /* Replay any already present rules */ |
| 881 | err = tcf_block_playback_offloads(block, cb, cb_priv, true, |
| 882 | tcf_block_offload_in_use(block), |
| 883 | extack); |
| 884 | if (err) |
| 885 | return ERR_PTR(err); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 886 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 887 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); |
| 888 | if (!block_cb) |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 889 | return ERR_PTR(-ENOMEM); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 890 | block_cb->cb = cb; |
| 891 | block_cb->cb_ident = cb_ident; |
| 892 | block_cb->cb_priv = cb_priv; |
| 893 | list_add(&block_cb->list, &block->cb_list); |
| 894 | return block_cb; |
| 895 | } |
| 896 | EXPORT_SYMBOL(__tcf_block_cb_register); |
| 897 | |
| 898 | int tcf_block_cb_register(struct tcf_block *block, |
| 899 | tc_setup_cb_t *cb, void *cb_ident, |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 900 | void *cb_priv, struct netlink_ext_ack *extack) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 901 | { |
| 902 | struct tcf_block_cb *block_cb; |
| 903 | |
John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 904 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv, |
| 905 | extack); |
Gustavo A. R. Silva | baa2d2b | 2018-07-18 23:14:17 -0500 | [diff] [blame] | 906 | return PTR_ERR_OR_ZERO(block_cb); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 907 | } |
| 908 | EXPORT_SYMBOL(tcf_block_cb_register); |
| 909 | |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 910 | void __tcf_block_cb_unregister(struct tcf_block *block, |
| 911 | struct tcf_block_cb *block_cb) |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 912 | { |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 913 | tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv, |
| 914 | false, tcf_block_offload_in_use(block), |
| 915 | NULL); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 916 | list_del(&block_cb->list); |
| 917 | kfree(block_cb); |
| 918 | } |
| 919 | EXPORT_SYMBOL(__tcf_block_cb_unregister); |
| 920 | |
| 921 | void tcf_block_cb_unregister(struct tcf_block *block, |
| 922 | tc_setup_cb_t *cb, void *cb_ident) |
| 923 | { |
| 924 | struct tcf_block_cb *block_cb; |
| 925 | |
| 926 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); |
| 927 | if (!block_cb) |
| 928 | return; |
John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 929 | __tcf_block_cb_unregister(block, block_cb); |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 930 | } |
| 931 | EXPORT_SYMBOL(tcf_block_cb_unregister); |
| 932 | |
| 933 | static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 934 | void *type_data, bool err_stop) |
| 935 | { |
| 936 | struct tcf_block_cb *block_cb; |
| 937 | int ok_count = 0; |
| 938 | int err; |
| 939 | |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 940 | /* Make sure all netdevs sharing this block are offload-capable. */ |
| 941 | if (block->nooffloaddevcnt && err_stop) |
| 942 | return -EOPNOTSUPP; |
| 943 | |
Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 944 | list_for_each_entry(block_cb, &block->cb_list, list) { |
| 945 | err = block_cb->cb(type, type_data, block_cb->cb_priv); |
| 946 | if (err) { |
| 947 | if (err_stop) |
| 948 | return err; |
| 949 | } else { |
| 950 | ok_count++; |
| 951 | } |
| 952 | } |
| 953 | return ok_count; |
| 954 | } |
| 955 | |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 956 | /* Main classifier routine: scans classifier chain attached |
| 957 | * to this qdisc, (optionally) tests for protocol and asks |
| 958 | * specific classifiers. |
| 959 | */ |
| 960 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 961 | struct tcf_result *res, bool compat_mode) |
| 962 | { |
| 963 | __be16 protocol = tc_skb_protocol(skb); |
| 964 | #ifdef CONFIG_NET_CLS_ACT |
| 965 | const int max_reclassify_loop = 4; |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 966 | const struct tcf_proto *orig_tp = tp; |
| 967 | const struct tcf_proto *first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 968 | int limit = 0; |
| 969 | |
| 970 | reclassify: |
| 971 | #endif |
| 972 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 973 | int err; |
| 974 | |
| 975 | if (tp->protocol != protocol && |
| 976 | tp->protocol != htons(ETH_P_ALL)) |
| 977 | continue; |
| 978 | |
| 979 | err = tp->classify(skb, tp, res); |
| 980 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 981 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 982 | first_tp = orig_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 983 | goto reset; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 984 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 985 | first_tp = res->goto_tp; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 986 | goto reset; |
| 987 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 988 | #endif |
| 989 | if (err >= 0) |
| 990 | return err; |
| 991 | } |
| 992 | |
| 993 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 994 | #ifdef CONFIG_NET_CLS_ACT |
| 995 | reset: |
| 996 | if (unlikely(limit++ >= max_reclassify_loop)) { |
Jiri Pirko | 9d3aaff | 2018-01-17 11:46:47 +0100 | [diff] [blame] | 997 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
| 998 | tp->chain->block->index, |
| 999 | tp->prio & 0xffff, |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1000 | ntohs(tp->protocol)); |
| 1001 | return TC_ACT_SHOT; |
| 1002 | } |
| 1003 | |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1004 | tp = first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1005 | protocol = tc_skb_protocol(skb); |
| 1006 | goto reclassify; |
| 1007 | #endif |
| 1008 | } |
| 1009 | EXPORT_SYMBOL(tcf_classify); |
| 1010 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1011 | struct tcf_chain_info { |
| 1012 | struct tcf_proto __rcu **pprev; |
| 1013 | struct tcf_proto __rcu *next; |
| 1014 | }; |
| 1015 | |
| 1016 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 1017 | { |
| 1018 | return rtnl_dereference(*chain_info->pprev); |
| 1019 | } |
| 1020 | |
| 1021 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 1022 | struct tcf_chain_info *chain_info, |
| 1023 | struct tcf_proto *tp) |
| 1024 | { |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1025 | if (*chain_info->pprev == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1026 | tcf_chain0_head_change(chain, tp); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1027 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 1028 | rcu_assign_pointer(*chain_info->pprev, tp); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1029 | tcf_chain_hold(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 1033 | struct tcf_chain_info *chain_info, |
| 1034 | struct tcf_proto *tp) |
| 1035 | { |
| 1036 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 1037 | |
Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1038 | if (tp == chain->filter_chain) |
Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1039 | tcf_chain0_head_change(chain, next); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1040 | RCU_INIT_POINTER(*chain_info->pprev, next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1041 | tcf_chain_put(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 1045 | struct tcf_chain_info *chain_info, |
| 1046 | u32 protocol, u32 prio, |
| 1047 | bool prio_allocate) |
| 1048 | { |
| 1049 | struct tcf_proto **pprev; |
| 1050 | struct tcf_proto *tp; |
| 1051 | |
| 1052 | /* Check the chain for existence of proto-tcf with this priority */ |
| 1053 | for (pprev = &chain->filter_chain; |
| 1054 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 1055 | if (tp->prio >= prio) { |
| 1056 | if (tp->prio == prio) { |
| 1057 | if (prio_allocate || |
| 1058 | (tp->protocol != protocol && protocol)) |
| 1059 | return ERR_PTR(-EINVAL); |
| 1060 | } else { |
| 1061 | tp = NULL; |
| 1062 | } |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | chain_info->pprev = pprev; |
| 1067 | chain_info->next = tp ? tp->next : NULL; |
| 1068 | return tp; |
| 1069 | } |
| 1070 | |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1071 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1072 | struct tcf_proto *tp, struct tcf_block *block, |
| 1073 | struct Qdisc *q, u32 parent, void *fh, |
| 1074 | u32 portid, u32 seq, u16 flags, int event) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1075 | { |
| 1076 | struct tcmsg *tcm; |
| 1077 | struct nlmsghdr *nlh; |
| 1078 | unsigned char *b = skb_tail_pointer(skb); |
| 1079 | |
| 1080 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1081 | if (!nlh) |
| 1082 | goto out_nlmsg_trim; |
| 1083 | tcm = nlmsg_data(nlh); |
| 1084 | tcm->tcm_family = AF_UNSPEC; |
| 1085 | tcm->tcm__pad1 = 0; |
| 1086 | tcm->tcm__pad2 = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1087 | if (q) { |
| 1088 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 1089 | tcm->tcm_parent = parent; |
| 1090 | } else { |
| 1091 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1092 | tcm->tcm_block_index = block->index; |
| 1093 | } |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1094 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 1095 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 1096 | goto nla_put_failure; |
| 1097 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 1098 | goto nla_put_failure; |
| 1099 | if (!fh) { |
| 1100 | tcm->tcm_handle = 0; |
| 1101 | } else { |
| 1102 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 1103 | goto nla_put_failure; |
| 1104 | } |
| 1105 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1106 | return skb->len; |
| 1107 | |
| 1108 | out_nlmsg_trim: |
| 1109 | nla_put_failure: |
| 1110 | nlmsg_trim(skb, b); |
| 1111 | return -1; |
| 1112 | } |
| 1113 | |
| 1114 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 1115 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1116 | struct tcf_block *block, struct Qdisc *q, |
| 1117 | u32 parent, void *fh, int event, bool unicast) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1118 | { |
| 1119 | struct sk_buff *skb; |
| 1120 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1121 | |
| 1122 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1123 | if (!skb) |
| 1124 | return -ENOBUFS; |
| 1125 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1126 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1127 | n->nlmsg_seq, n->nlmsg_flags, event) <= 0) { |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1128 | kfree_skb(skb); |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
| 1132 | if (unicast) |
| 1133 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1134 | |
| 1135 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1136 | n->nlmsg_flags & NLM_F_ECHO); |
| 1137 | } |
| 1138 | |
| 1139 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 1140 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1141 | struct tcf_block *block, struct Qdisc *q, |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1142 | u32 parent, void *fh, bool unicast, bool *last, |
| 1143 | struct netlink_ext_ack *extack) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1144 | { |
| 1145 | struct sk_buff *skb; |
| 1146 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1147 | int err; |
| 1148 | |
| 1149 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1150 | if (!skb) |
| 1151 | return -ENOBUFS; |
| 1152 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1153 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1154 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1155 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1156 | kfree_skb(skb); |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 1160 | err = tp->ops->delete(tp, fh, last, extack); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1161 | if (err) { |
| 1162 | kfree_skb(skb); |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
| 1166 | if (unicast) |
| 1167 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1168 | |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1169 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1170 | n->nlmsg_flags & NLM_F_ECHO); |
| 1171 | if (err < 0) |
| 1172 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); |
| 1173 | return err; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1177 | struct tcf_block *block, struct Qdisc *q, |
| 1178 | u32 parent, struct nlmsghdr *n, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1179 | struct tcf_chain *chain, int event) |
| 1180 | { |
| 1181 | struct tcf_proto *tp; |
| 1182 | |
| 1183 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1184 | tp; tp = rtnl_dereference(tp->next)) |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1185 | tfilter_notify(net, oskb, n, tp, block, |
YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1186 | q, parent, NULL, event, false); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1189 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1190 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1192 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1193 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | struct tcmsg *t; |
| 1195 | u32 protocol; |
| 1196 | u32 prio; |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1197 | bool prio_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1199 | u32 chain_index; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1200 | struct Qdisc *q = NULL; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1201 | struct tcf_chain_info chain_info; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1202 | struct tcf_chain *chain = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1203 | struct tcf_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | struct tcf_proto *tp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | unsigned long cl; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1206 | void *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | int err; |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1208 | int tp_created; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1210 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1211 | return -EPERM; |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | replay: |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1214 | tp_created = 0; |
| 1215 | |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame^] | 1216 | 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] | 1217 | if (err < 0) |
| 1218 | return err; |
| 1219 | |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1220 | t = nlmsg_data(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | protocol = TC_H_MIN(t->tcm_info); |
| 1222 | prio = TC_H_MAJ(t->tcm_info); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1223 | prio_allocate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | parent = t->tcm_parent; |
| 1225 | cl = 0; |
| 1226 | |
| 1227 | if (prio == 0) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1228 | /* If no priority is provided by the user, |
| 1229 | * we allocate one. |
| 1230 | */ |
| 1231 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 1232 | prio = TC_H_MAKE(0x80000000U, 0U); |
| 1233 | prio_allocate = true; |
| 1234 | } else { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1235 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return -ENOENT; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /* Find head of filter chain. */ |
| 1241 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1242 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1243 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1244 | if (IS_ERR(block)) { |
| 1245 | err = PTR_ERR(block); |
| 1246 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1247 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1248 | |
| 1249 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1250 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1251 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1252 | err = -EINVAL; |
| 1253 | goto errout; |
| 1254 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1255 | chain = tcf_chain_get(block, chain_index, true); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1256 | if (!chain) { |
Jiri Pirko | d5ed72a | 2018-08-27 20:58:43 +0200 | [diff] [blame] | 1257 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1258 | err = -ENOMEM; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1259 | goto errout; |
| 1260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1262 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1263 | prio, prio_allocate); |
| 1264 | if (IS_ERR(tp)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1265 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1266 | err = PTR_ERR(tp); |
| 1267 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | if (tp == NULL) { |
| 1271 | /* Proto-tcf does not exist, create new one */ |
| 1272 | |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1273 | if (tca[TCA_KIND] == NULL || !protocol) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1274 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1275 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1279 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1280 | 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] | 1281 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1285 | if (prio_allocate) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1286 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1288 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1289 | protocol, prio, chain, extack); |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1290 | if (IS_ERR(tp)) { |
| 1291 | err = PTR_ERR(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | goto errout; |
| 1293 | } |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1294 | tp_created = 1; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1295 | } 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] | 1296 | 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] | 1297 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1302 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1303 | if (!fh) { |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1304 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1305 | 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] | 1306 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1308 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1309 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
| 1310 | NL_SET_ERR_MSG(extack, "Filter already exists"); |
| 1311 | err = -EEXIST; |
| 1312 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1315 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { |
| 1316 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); |
| 1317 | err = -EINVAL; |
| 1318 | goto errout; |
| 1319 | } |
| 1320 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 1321 | 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] | 1322 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
| 1323 | extack); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1324 | if (err == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1325 | if (tp_created) |
| 1326 | tcf_chain_tp_insert(chain, &chain_info, tp); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1327 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1328 | RTM_NEWTFILTER, false); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1329 | } else { |
| 1330 | if (tp_created) |
Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 1331 | tcf_proto_destroy(tp, NULL); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1332 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
| 1334 | errout: |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1335 | if (chain) |
| 1336 | tcf_chain_put(chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | if (err == -EAGAIN) |
| 1338 | /* Replay the request. */ |
| 1339 | goto replay; |
| 1340 | return err; |
| 1341 | } |
| 1342 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1343 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1344 | struct netlink_ext_ack *extack) |
| 1345 | { |
| 1346 | struct net *net = sock_net(skb->sk); |
| 1347 | struct nlattr *tca[TCA_MAX + 1]; |
| 1348 | struct tcmsg *t; |
| 1349 | u32 protocol; |
| 1350 | u32 prio; |
| 1351 | u32 parent; |
| 1352 | u32 chain_index; |
| 1353 | struct Qdisc *q = NULL; |
| 1354 | struct tcf_chain_info chain_info; |
| 1355 | struct tcf_chain *chain = NULL; |
| 1356 | struct tcf_block *block; |
| 1357 | struct tcf_proto *tp = NULL; |
| 1358 | unsigned long cl = 0; |
| 1359 | void *fh = NULL; |
| 1360 | int err; |
| 1361 | |
| 1362 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1363 | return -EPERM; |
| 1364 | |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame^] | 1365 | 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] | 1366 | if (err < 0) |
| 1367 | return err; |
| 1368 | |
| 1369 | t = nlmsg_data(n); |
| 1370 | protocol = TC_H_MIN(t->tcm_info); |
| 1371 | prio = TC_H_MAJ(t->tcm_info); |
| 1372 | parent = t->tcm_parent; |
| 1373 | |
| 1374 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { |
| 1375 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); |
| 1376 | return -ENOENT; |
| 1377 | } |
| 1378 | |
| 1379 | /* Find head of filter chain. */ |
| 1380 | |
| 1381 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1382 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1383 | if (IS_ERR(block)) { |
| 1384 | err = PTR_ERR(block); |
| 1385 | goto errout; |
| 1386 | } |
| 1387 | |
| 1388 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1389 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1390 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1391 | err = -EINVAL; |
| 1392 | goto errout; |
| 1393 | } |
| 1394 | chain = tcf_chain_get(block, chain_index, false); |
| 1395 | if (!chain) { |
Jiri Pirko | 5ca8a25 | 2018-08-03 11:08:47 +0200 | [diff] [blame] | 1396 | /* User requested flush on non-existent chain. Nothing to do, |
| 1397 | * so just return success. |
| 1398 | */ |
| 1399 | if (prio == 0) { |
| 1400 | err = 0; |
| 1401 | goto errout; |
| 1402 | } |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1403 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
Jiri Pirko | b7b4247 | 2018-08-27 20:58:44 +0200 | [diff] [blame] | 1404 | err = -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1405 | goto errout; |
| 1406 | } |
| 1407 | |
| 1408 | if (prio == 0) { |
| 1409 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1410 | chain, RTM_DELTFILTER); |
| 1411 | tcf_chain_flush(chain); |
| 1412 | err = 0; |
| 1413 | goto errout; |
| 1414 | } |
| 1415 | |
| 1416 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1417 | prio, false); |
| 1418 | if (!tp || IS_ERR(tp)) { |
| 1419 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1420 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1421 | goto errout; |
| 1422 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1423 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1424 | err = -EINVAL; |
| 1425 | goto errout; |
| 1426 | } |
| 1427 | |
| 1428 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1429 | |
| 1430 | if (!fh) { |
| 1431 | if (t->tcm_handle == 0) { |
| 1432 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1433 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| 1434 | RTM_DELTFILTER, false); |
| 1435 | tcf_proto_destroy(tp, extack); |
| 1436 | err = 0; |
| 1437 | } else { |
| 1438 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1439 | err = -ENOENT; |
| 1440 | } |
| 1441 | } else { |
| 1442 | bool last; |
| 1443 | |
| 1444 | err = tfilter_del_notify(net, skb, n, tp, block, |
| 1445 | q, parent, fh, false, &last, |
| 1446 | extack); |
| 1447 | if (err) |
| 1448 | goto errout; |
| 1449 | if (last) { |
| 1450 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1451 | tcf_proto_destroy(tp, extack); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | errout: |
| 1456 | if (chain) |
| 1457 | tcf_chain_put(chain); |
| 1458 | return err; |
| 1459 | } |
| 1460 | |
| 1461 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1462 | struct netlink_ext_ack *extack) |
| 1463 | { |
| 1464 | struct net *net = sock_net(skb->sk); |
| 1465 | struct nlattr *tca[TCA_MAX + 1]; |
| 1466 | struct tcmsg *t; |
| 1467 | u32 protocol; |
| 1468 | u32 prio; |
| 1469 | u32 parent; |
| 1470 | u32 chain_index; |
| 1471 | struct Qdisc *q = NULL; |
| 1472 | struct tcf_chain_info chain_info; |
| 1473 | struct tcf_chain *chain = NULL; |
| 1474 | struct tcf_block *block; |
| 1475 | struct tcf_proto *tp = NULL; |
| 1476 | unsigned long cl = 0; |
| 1477 | void *fh = NULL; |
| 1478 | int err; |
| 1479 | |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame^] | 1480 | 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] | 1481 | if (err < 0) |
| 1482 | return err; |
| 1483 | |
| 1484 | t = nlmsg_data(n); |
| 1485 | protocol = TC_H_MIN(t->tcm_info); |
| 1486 | prio = TC_H_MAJ(t->tcm_info); |
| 1487 | parent = t->tcm_parent; |
| 1488 | |
| 1489 | if (prio == 0) { |
| 1490 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| 1491 | return -ENOENT; |
| 1492 | } |
| 1493 | |
| 1494 | /* Find head of filter chain. */ |
| 1495 | |
| 1496 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1497 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1498 | if (IS_ERR(block)) { |
| 1499 | err = PTR_ERR(block); |
| 1500 | goto errout; |
| 1501 | } |
| 1502 | |
| 1503 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1504 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1505 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1506 | err = -EINVAL; |
| 1507 | goto errout; |
| 1508 | } |
| 1509 | chain = tcf_chain_get(block, chain_index, false); |
| 1510 | if (!chain) { |
| 1511 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1512 | err = -EINVAL; |
| 1513 | goto errout; |
| 1514 | } |
| 1515 | |
| 1516 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1517 | prio, false); |
| 1518 | if (!tp || IS_ERR(tp)) { |
| 1519 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1520 | err = tp ? PTR_ERR(tp) : -ENOENT; |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1521 | goto errout; |
| 1522 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1523 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1524 | err = -EINVAL; |
| 1525 | goto errout; |
| 1526 | } |
| 1527 | |
| 1528 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1529 | |
| 1530 | if (!fh) { |
| 1531 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1532 | err = -ENOENT; |
| 1533 | } else { |
| 1534 | err = tfilter_notify(net, skb, n, tp, block, q, parent, |
| 1535 | fh, RTM_NEWTFILTER, true); |
| 1536 | if (err < 0) |
| 1537 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); |
| 1538 | } |
| 1539 | |
| 1540 | errout: |
| 1541 | if (chain) |
| 1542 | tcf_chain_put(chain); |
| 1543 | return err; |
| 1544 | } |
| 1545 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1546 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | struct tcf_walker w; |
| 1548 | struct sk_buff *skb; |
| 1549 | struct netlink_callback *cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1550 | struct tcf_block *block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1551 | struct Qdisc *q; |
| 1552 | u32 parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | }; |
| 1554 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1555 | 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] | 1556 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1557 | struct tcf_dump_args *a = (void *)arg; |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 1558 | struct net *net = sock_net(a->skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1560 | 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] | 1561 | n, NETLINK_CB(a->cb->skb).portid, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1562 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1563 | RTM_NEWTFILTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1566 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 1567 | struct sk_buff *skb, struct netlink_callback *cb, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1568 | long index_start, long *p_index) |
| 1569 | { |
| 1570 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1571 | struct tcf_block *block = chain->block; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1572 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 1573 | struct tcf_dump_args arg; |
| 1574 | struct tcf_proto *tp; |
| 1575 | |
| 1576 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1577 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 1578 | if (*p_index < index_start) |
| 1579 | continue; |
| 1580 | if (TC_H_MAJ(tcm->tcm_info) && |
| 1581 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 1582 | continue; |
| 1583 | if (TC_H_MIN(tcm->tcm_info) && |
| 1584 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 1585 | continue; |
| 1586 | if (*p_index > index_start) |
| 1587 | memset(&cb->args[1], 0, |
| 1588 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 1589 | if (cb->args[1] == 0) { |
YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1590 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1591 | NETLINK_CB(cb->skb).portid, |
| 1592 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1593 | RTM_NEWTFILTER) <= 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1594 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1595 | |
| 1596 | cb->args[1] = 1; |
| 1597 | } |
| 1598 | if (!tp->ops->walk) |
| 1599 | continue; |
| 1600 | arg.w.fn = tcf_node_dump; |
| 1601 | arg.skb = skb; |
| 1602 | arg.cb = cb; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1603 | arg.block = block; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1604 | arg.q = q; |
| 1605 | arg.parent = parent; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1606 | arg.w.stop = 0; |
| 1607 | arg.w.skip = cb->args[1] - 1; |
| 1608 | arg.w.count = 0; |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1609 | arg.w.cookie = cb->args[2]; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1610 | tp->ops->walk(tp, &arg.w); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1611 | cb->args[2] = arg.w.cookie; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1612 | cb->args[1] = arg.w.count + 1; |
| 1613 | if (arg.w.stop) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1614 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1615 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1616 | return true; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1617 | } |
| 1618 | |
Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 1619 | /* called with RTNL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 1621 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1622 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1623 | struct nlattr *tca[TCA_MAX + 1]; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1624 | struct Qdisc *q = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1625 | struct tcf_block *block; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1626 | struct tcf_chain *chain; |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1627 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1628 | long index_start; |
| 1629 | long index; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1630 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1631 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1633 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | return skb->len; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1635 | |
| 1636 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 1637 | if (err) |
| 1638 | return err; |
| 1639 | |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1640 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 1641 | block = tcf_block_lookup(net, tcm->tcm_block_index); |
| 1642 | if (!block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 1643 | goto out; |
Jiri Pirko | d680b35 | 2018-01-18 16:14:49 +0100 | [diff] [blame] | 1644 | /* If we work with block index, q is NULL and parent value |
| 1645 | * will never be used in the following code. The check |
| 1646 | * in tcf_fill_node prevents it. However, compiler does not |
| 1647 | * see that far, so set parent to zero to silence the warning |
| 1648 | * about parent being uninitialized. |
| 1649 | */ |
| 1650 | parent = 0; |
Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1651 | } else { |
| 1652 | const struct Qdisc_class_ops *cops; |
| 1653 | struct net_device *dev; |
| 1654 | unsigned long cl = 0; |
| 1655 | |
| 1656 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 1657 | if (!dev) |
| 1658 | return skb->len; |
| 1659 | |
| 1660 | parent = tcm->tcm_parent; |
| 1661 | if (!parent) { |
| 1662 | q = dev->qdisc; |
| 1663 | parent = q->handle; |
| 1664 | } else { |
| 1665 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 1666 | } |
| 1667 | if (!q) |
| 1668 | goto out; |
| 1669 | cops = q->ops->cl_ops; |
| 1670 | if (!cops) |
| 1671 | goto out; |
| 1672 | if (!cops->tcf_block) |
| 1673 | goto out; |
| 1674 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 1675 | cl = cops->find(q, tcm->tcm_parent); |
| 1676 | if (cl == 0) |
| 1677 | goto out; |
| 1678 | } |
| 1679 | block = cops->tcf_block(q, cl, NULL); |
| 1680 | if (!block) |
| 1681 | goto out; |
| 1682 | if (tcf_block_shared(block)) |
| 1683 | q = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1686 | index_start = cb->args[0]; |
| 1687 | index = 0; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1688 | |
| 1689 | list_for_each_entry(chain, &block->chain_list, list) { |
| 1690 | if (tca[TCA_CHAIN] && |
| 1691 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 1692 | continue; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1693 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1694 | index_start, &index)) { |
| 1695 | err = -EMSGSIZE; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1696 | break; |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1697 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1698 | } |
| 1699 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1700 | cb->args[0] = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | out: |
Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1703 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 1704 | if (skb->len == 0 && err) |
| 1705 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | return skb->len; |
| 1707 | } |
| 1708 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1709 | static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net, |
| 1710 | struct sk_buff *skb, struct tcf_block *block, |
| 1711 | u32 portid, u32 seq, u16 flags, int event) |
| 1712 | { |
| 1713 | unsigned char *b = skb_tail_pointer(skb); |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1714 | const struct tcf_proto_ops *ops; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1715 | struct nlmsghdr *nlh; |
| 1716 | struct tcmsg *tcm; |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1717 | void *priv; |
| 1718 | |
| 1719 | ops = chain->tmplt_ops; |
| 1720 | priv = chain->tmplt_priv; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1721 | |
| 1722 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1723 | if (!nlh) |
| 1724 | goto out_nlmsg_trim; |
| 1725 | tcm = nlmsg_data(nlh); |
| 1726 | tcm->tcm_family = AF_UNSPEC; |
| 1727 | tcm->tcm__pad1 = 0; |
| 1728 | tcm->tcm__pad2 = 0; |
| 1729 | tcm->tcm_handle = 0; |
| 1730 | if (block->q) { |
| 1731 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; |
| 1732 | tcm->tcm_parent = block->q->handle; |
| 1733 | } else { |
| 1734 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1735 | tcm->tcm_block_index = block->index; |
| 1736 | } |
| 1737 | |
| 1738 | if (nla_put_u32(skb, TCA_CHAIN, chain->index)) |
| 1739 | goto nla_put_failure; |
| 1740 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1741 | if (ops) { |
| 1742 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
| 1743 | goto nla_put_failure; |
| 1744 | if (ops->tmplt_dump(skb, net, priv) < 0) |
| 1745 | goto nla_put_failure; |
| 1746 | } |
| 1747 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1748 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1749 | return skb->len; |
| 1750 | |
| 1751 | out_nlmsg_trim: |
| 1752 | nla_put_failure: |
| 1753 | nlmsg_trim(skb, b); |
| 1754 | return -EMSGSIZE; |
| 1755 | } |
| 1756 | |
| 1757 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 1758 | u32 seq, u16 flags, int event, bool unicast) |
| 1759 | { |
| 1760 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1761 | struct tcf_block *block = chain->block; |
| 1762 | struct net *net = block->net; |
| 1763 | struct sk_buff *skb; |
| 1764 | |
| 1765 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1766 | if (!skb) |
| 1767 | return -ENOBUFS; |
| 1768 | |
| 1769 | if (tc_chain_fill_node(chain, net, skb, block, portid, |
| 1770 | seq, flags, event) <= 0) { |
| 1771 | kfree_skb(skb); |
| 1772 | return -EINVAL; |
| 1773 | } |
| 1774 | |
| 1775 | if (unicast) |
| 1776 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1777 | |
| 1778 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); |
| 1779 | } |
| 1780 | |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1781 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, |
| 1782 | struct nlattr **tca, |
| 1783 | struct netlink_ext_ack *extack) |
| 1784 | { |
| 1785 | const struct tcf_proto_ops *ops; |
| 1786 | void *tmplt_priv; |
| 1787 | |
| 1788 | /* If kind is not set, user did not specify template. */ |
| 1789 | if (!tca[TCA_KIND]) |
| 1790 | return 0; |
| 1791 | |
| 1792 | ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack); |
| 1793 | if (IS_ERR(ops)) |
| 1794 | return PTR_ERR(ops); |
| 1795 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { |
| 1796 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); |
| 1797 | return -EOPNOTSUPP; |
| 1798 | } |
| 1799 | |
| 1800 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack); |
| 1801 | if (IS_ERR(tmplt_priv)) { |
| 1802 | module_put(ops->owner); |
| 1803 | return PTR_ERR(tmplt_priv); |
| 1804 | } |
| 1805 | chain->tmplt_ops = ops; |
| 1806 | chain->tmplt_priv = tmplt_priv; |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
| 1810 | static void tc_chain_tmplt_del(struct tcf_chain *chain) |
| 1811 | { |
| 1812 | const struct tcf_proto_ops *ops = chain->tmplt_ops; |
| 1813 | |
| 1814 | /* If template ops are set, no work to do for us. */ |
| 1815 | if (!ops) |
| 1816 | return; |
| 1817 | |
| 1818 | ops->tmplt_destroy(chain->tmplt_priv); |
| 1819 | module_put(ops->owner); |
| 1820 | } |
| 1821 | |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1822 | /* Add/delete/get a chain */ |
| 1823 | |
| 1824 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, |
| 1825 | struct netlink_ext_ack *extack) |
| 1826 | { |
| 1827 | struct net *net = sock_net(skb->sk); |
| 1828 | struct nlattr *tca[TCA_MAX + 1]; |
| 1829 | struct tcmsg *t; |
| 1830 | u32 parent; |
| 1831 | u32 chain_index; |
| 1832 | struct Qdisc *q = NULL; |
| 1833 | struct tcf_chain *chain = NULL; |
| 1834 | struct tcf_block *block; |
| 1835 | unsigned long cl; |
| 1836 | int err; |
| 1837 | |
| 1838 | if (n->nlmsg_type != RTM_GETCHAIN && |
| 1839 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1840 | return -EPERM; |
| 1841 | |
| 1842 | replay: |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame^] | 1843 | 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] | 1844 | if (err < 0) |
| 1845 | return err; |
| 1846 | |
| 1847 | t = nlmsg_data(n); |
| 1848 | parent = t->tcm_parent; |
| 1849 | cl = 0; |
| 1850 | |
| 1851 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1852 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1853 | if (IS_ERR(block)) |
| 1854 | return PTR_ERR(block); |
| 1855 | |
| 1856 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1857 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1858 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1859 | return -EINVAL; |
| 1860 | } |
| 1861 | chain = tcf_chain_lookup(block, chain_index); |
| 1862 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
| 1863 | if (chain) { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1864 | if (tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1865 | /* The chain exists only because there is |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1866 | * some action referencing it. |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1867 | */ |
| 1868 | tcf_chain_hold(chain); |
| 1869 | } else { |
| 1870 | NL_SET_ERR_MSG(extack, "Filter chain already exists"); |
| 1871 | return -EEXIST; |
| 1872 | } |
| 1873 | } else { |
| 1874 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| 1875 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); |
| 1876 | return -ENOENT; |
| 1877 | } |
| 1878 | chain = tcf_chain_create(block, chain_index); |
| 1879 | if (!chain) { |
| 1880 | NL_SET_ERR_MSG(extack, "Failed to create filter chain"); |
| 1881 | return -ENOMEM; |
| 1882 | } |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1883 | } |
| 1884 | } else { |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1885 | if (!chain || tcf_chain_held_by_acts_only(chain)) { |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1886 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1887 | return -EINVAL; |
| 1888 | } |
| 1889 | tcf_chain_hold(chain); |
| 1890 | } |
| 1891 | |
| 1892 | switch (n->nlmsg_type) { |
| 1893 | case RTM_NEWCHAIN: |
Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1894 | err = tc_chain_tmplt_add(chain, net, tca, extack); |
| 1895 | if (err) |
| 1896 | goto errout; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1897 | /* In case the chain was successfully added, take a reference |
| 1898 | * to the chain. This ensures that an empty chain |
| 1899 | * does not disappear at the end of this function. |
| 1900 | */ |
| 1901 | tcf_chain_hold(chain); |
| 1902 | chain->explicitly_created = true; |
| 1903 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 1904 | RTM_NEWCHAIN, false); |
| 1905 | break; |
| 1906 | case RTM_DELCHAIN: |
Cong Wang | f5b9bac | 2018-09-11 14:22:23 -0700 | [diff] [blame] | 1907 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1908 | chain, RTM_DELTFILTER); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1909 | /* Flush the chain first as the user requested chain removal. */ |
| 1910 | tcf_chain_flush(chain); |
| 1911 | /* In case the chain was successfully deleted, put a reference |
| 1912 | * to the chain previously taken during addition. |
| 1913 | */ |
| 1914 | tcf_chain_put_explicitly_created(chain); |
Jiri Pirko | c921d7d | 2018-07-26 18:27:58 +0200 | [diff] [blame] | 1915 | chain->explicitly_created = false; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1916 | break; |
| 1917 | case RTM_GETCHAIN: |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1918 | err = tc_chain_notify(chain, skb, n->nlmsg_seq, |
| 1919 | n->nlmsg_seq, n->nlmsg_type, true); |
| 1920 | if (err < 0) |
| 1921 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); |
| 1922 | break; |
| 1923 | default: |
| 1924 | err = -EOPNOTSUPP; |
| 1925 | NL_SET_ERR_MSG(extack, "Unsupported message type"); |
| 1926 | goto errout; |
| 1927 | } |
| 1928 | |
| 1929 | errout: |
| 1930 | tcf_chain_put(chain); |
| 1931 | if (err == -EAGAIN) |
| 1932 | /* Replay the request. */ |
| 1933 | goto replay; |
| 1934 | return err; |
| 1935 | } |
| 1936 | |
| 1937 | /* called with RTNL */ |
| 1938 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) |
| 1939 | { |
| 1940 | struct net *net = sock_net(skb->sk); |
| 1941 | struct nlattr *tca[TCA_MAX + 1]; |
| 1942 | struct Qdisc *q = NULL; |
| 1943 | struct tcf_block *block; |
| 1944 | struct tcf_chain *chain; |
| 1945 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 1946 | long index_start; |
| 1947 | long index; |
| 1948 | u32 parent; |
| 1949 | int err; |
| 1950 | |
| 1951 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| 1952 | return skb->len; |
| 1953 | |
Davide Caratti | e331473 | 2018-10-10 22:00:58 +0200 | [diff] [blame^] | 1954 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy, |
| 1955 | NULL); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1956 | if (err) |
| 1957 | return err; |
| 1958 | |
| 1959 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 1960 | block = tcf_block_lookup(net, tcm->tcm_block_index); |
| 1961 | if (!block) |
| 1962 | goto out; |
| 1963 | /* If we work with block index, q is NULL and parent value |
| 1964 | * will never be used in the following code. The check |
| 1965 | * in tcf_fill_node prevents it. However, compiler does not |
| 1966 | * see that far, so set parent to zero to silence the warning |
| 1967 | * about parent being uninitialized. |
| 1968 | */ |
| 1969 | parent = 0; |
| 1970 | } else { |
| 1971 | const struct Qdisc_class_ops *cops; |
| 1972 | struct net_device *dev; |
| 1973 | unsigned long cl = 0; |
| 1974 | |
| 1975 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 1976 | if (!dev) |
| 1977 | return skb->len; |
| 1978 | |
| 1979 | parent = tcm->tcm_parent; |
| 1980 | if (!parent) { |
| 1981 | q = dev->qdisc; |
| 1982 | parent = q->handle; |
| 1983 | } else { |
| 1984 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 1985 | } |
| 1986 | if (!q) |
| 1987 | goto out; |
| 1988 | cops = q->ops->cl_ops; |
| 1989 | if (!cops) |
| 1990 | goto out; |
| 1991 | if (!cops->tcf_block) |
| 1992 | goto out; |
| 1993 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 1994 | cl = cops->find(q, tcm->tcm_parent); |
| 1995 | if (cl == 0) |
| 1996 | goto out; |
| 1997 | } |
| 1998 | block = cops->tcf_block(q, cl, NULL); |
| 1999 | if (!block) |
| 2000 | goto out; |
| 2001 | if (tcf_block_shared(block)) |
| 2002 | q = NULL; |
| 2003 | } |
| 2004 | |
| 2005 | index_start = cb->args[0]; |
| 2006 | index = 0; |
| 2007 | |
| 2008 | list_for_each_entry(chain, &block->chain_list, list) { |
| 2009 | if ((tca[TCA_CHAIN] && |
| 2010 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)) |
| 2011 | continue; |
| 2012 | if (index < index_start) { |
| 2013 | index++; |
| 2014 | continue; |
| 2015 | } |
Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2016 | if (tcf_chain_held_by_acts_only(chain)) |
Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2017 | continue; |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2018 | err = tc_chain_fill_node(chain, net, skb, block, |
| 2019 | NETLINK_CB(cb->skb).portid, |
| 2020 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 2021 | RTM_NEWCHAIN); |
| 2022 | if (err <= 0) |
| 2023 | break; |
| 2024 | index++; |
| 2025 | } |
| 2026 | |
| 2027 | cb->args[0] = index; |
| 2028 | |
| 2029 | out: |
| 2030 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 2031 | if (skb->len == 0 && err) |
| 2032 | return err; |
| 2033 | return skb->len; |
| 2034 | } |
| 2035 | |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 2036 | void tcf_exts_destroy(struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | { |
| 2038 | #ifdef CONFIG_NET_CLS_ACT |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2039 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2040 | kfree(exts->actions); |
| 2041 | exts->nr_actions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | #endif |
| 2043 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2044 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 2046 | 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] | 2047 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
| 2048 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | #ifdef CONFIG_NET_CLS_ACT |
| 2051 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | struct tc_action *act; |
Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 2053 | size_t attr_size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2055 | if (exts->police && tb[exts->police]) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2056 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 2057 | rate_tlv, "police", ovr, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2058 | TCA_ACT_BIND, true, extack); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 2059 | if (IS_ERR(act)) |
| 2060 | return PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2062 | act->type = exts->type = TCA_OLD_COMPAT; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2063 | exts->actions[0] = act; |
| 2064 | exts->nr_actions = 1; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2065 | } else if (exts->action && tb[exts->action]) { |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2066 | int err; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2067 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2068 | err = tcf_action_init(net, tp, tb[exts->action], |
| 2069 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2070 | exts->actions, &attr_size, true, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2071 | extack); |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2072 | if (err < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2073 | return err; |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2074 | exts->nr_actions = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | } |
Cong Wang | e4b95c4 | 2017-11-06 13:47:19 -0800 | [diff] [blame] | 2076 | exts->net = net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | #else |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2079 | if ((exts->action && tb[exts->action]) || |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2080 | (exts->police && tb[exts->police])) { |
| 2081 | 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] | 2082 | return -EOPNOTSUPP; |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2083 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | #endif |
| 2085 | |
| 2086 | return 0; |
| 2087 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2088 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2090 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | { |
| 2092 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2093 | struct tcf_exts old = *dst; |
| 2094 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2095 | *dst = *src; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2096 | tcf_exts_destroy(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | #endif |
| 2098 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2099 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2101 | #ifdef CONFIG_NET_CLS_ACT |
| 2102 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 2103 | { |
| 2104 | if (exts->nr_actions == 0) |
| 2105 | return NULL; |
| 2106 | else |
| 2107 | return exts->actions[0]; |
| 2108 | } |
| 2109 | #endif |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2110 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2111 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
| 2113 | #ifdef CONFIG_NET_CLS_ACT |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2114 | struct nlattr *nest; |
| 2115 | |
Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 2116 | if (exts->action && tcf_exts_has_actions(exts)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | /* |
| 2118 | * again for backward compatible mode - we want |
| 2119 | * to work with both old and new modes of entering |
| 2120 | * tc data even if iproute2 was newer - jhs |
| 2121 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2122 | if (exts->type != TCA_OLD_COMPAT) { |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2123 | nest = nla_nest_start(skb, exts->action); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2124 | if (nest == NULL) |
| 2125 | goto nla_put_failure; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2126 | |
Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2127 | if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2128 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2129 | nla_nest_end(skb, nest); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2130 | } else if (exts->police) { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2131 | struct tc_action *act = tcf_exts_first_act(exts); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2132 | nest = nla_nest_start(skb, exts->police); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 2133 | if (nest == NULL || !act) |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2134 | goto nla_put_failure; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2135 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2136 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2137 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
| 2139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | return 0; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2141 | |
| 2142 | nla_put_failure: |
| 2143 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | return -1; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2145 | #else |
| 2146 | return 0; |
| 2147 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2149 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2151 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2152 | 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] | 2153 | { |
| 2154 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2155 | struct tc_action *a = tcf_exts_first_act(exts); |
Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 2156 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2157 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | #endif |
| 2159 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2161 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2163 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
| 2164 | enum tc_setup_type type, |
| 2165 | void *type_data, bool err_stop) |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2166 | { |
| 2167 | int ok_count = 0; |
| 2168 | #ifdef CONFIG_NET_CLS_ACT |
| 2169 | const struct tc_action *a; |
| 2170 | struct net_device *dev; |
Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2171 | int i, ret; |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2172 | |
| 2173 | if (!tcf_exts_has_actions(exts)) |
| 2174 | return 0; |
| 2175 | |
Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2176 | for (i = 0; i < exts->nr_actions; i++) { |
| 2177 | a = exts->actions[i]; |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2178 | if (!a->ops->get_dev) |
| 2179 | continue; |
| 2180 | dev = a->ops->get_dev(a); |
Jiri Pirko | 7612fb0 | 2017-11-01 11:47:40 +0100 | [diff] [blame] | 2181 | if (!dev) |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2182 | continue; |
| 2183 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); |
Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame] | 2184 | a->ops->put_dev(dev); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2185 | if (ret < 0) |
| 2186 | return ret; |
| 2187 | ok_count += ret; |
| 2188 | } |
| 2189 | #endif |
| 2190 | return ok_count; |
| 2191 | } |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2192 | |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2193 | int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, |
| 2194 | enum tc_setup_type type, void *type_data, bool err_stop) |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2195 | { |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2196 | int ok_count; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2197 | int ret; |
| 2198 | |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2199 | ret = tcf_block_cb_call(block, type, type_data, err_stop); |
| 2200 | if (ret < 0) |
| 2201 | return ret; |
| 2202 | ok_count = ret; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2203 | |
Or Gerlitz | f8f4bef | 2018-05-23 19:24:48 +0300 | [diff] [blame] | 2204 | if (!exts || ok_count) |
David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2205 | return ok_count; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2206 | ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); |
| 2207 | if (ret < 0) |
| 2208 | return ret; |
| 2209 | ok_count += ret; |
| 2210 | |
| 2211 | return ok_count; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2212 | } |
| 2213 | EXPORT_SYMBOL(tc_setup_cb_call); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2214 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2215 | static __net_init int tcf_net_init(struct net *net) |
| 2216 | { |
| 2217 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2218 | |
| 2219 | idr_init(&tn->idr); |
| 2220 | return 0; |
| 2221 | } |
| 2222 | |
| 2223 | static void __net_exit tcf_net_exit(struct net *net) |
| 2224 | { |
| 2225 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2226 | |
| 2227 | idr_destroy(&tn->idr); |
| 2228 | } |
| 2229 | |
| 2230 | static struct pernet_operations tcf_net_ops = { |
| 2231 | .init = tcf_net_init, |
| 2232 | .exit = tcf_net_exit, |
| 2233 | .id = &tcf_net_id, |
| 2234 | .size = sizeof(struct tcf_net), |
| 2235 | }; |
| 2236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | static int __init tc_filter_init(void) |
| 2238 | { |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2239 | int err; |
| 2240 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 2241 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 2242 | if (!tc_filter_wq) |
| 2243 | return -ENOMEM; |
| 2244 | |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2245 | err = register_pernet_subsys(&tcf_net_ops); |
| 2246 | if (err) |
| 2247 | goto err_register_pernet_subsys; |
| 2248 | |
Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2249 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0); |
| 2250 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0); |
| 2251 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 2252 | tc_dump_tfilter, 0); |
Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2253 | rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); |
| 2254 | rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); |
| 2255 | rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, |
| 2256 | tc_dump_chain, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | return 0; |
Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2259 | |
| 2260 | err_register_pernet_subsys: |
| 2261 | destroy_workqueue(tc_filter_wq); |
| 2262 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | subsys_initcall(tc_filter_init); |