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> |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 26 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/slab.h> |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 28 | #include <net/net_namespace.h> |
| 29 | #include <net/sock.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 30 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <net/pkt_sched.h> |
| 32 | #include <net/pkt_cls.h> |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* The list of all installed classifier types */ |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 35 | static LIST_HEAD(tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* Protects list of registered TC modules. It is pure SMP lock. */ |
| 38 | static DEFINE_RWLOCK(cls_mod_lock); |
| 39 | |
| 40 | /* Find classifier type by string name */ |
| 41 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 42 | static const struct tcf_proto_ops *tcf_proto_lookup_ops(const char *kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 44 | const struct tcf_proto_ops *t, *res = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | if (kind) { |
| 47 | read_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 48 | list_for_each_entry(t, &tcf_proto_base, head) { |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 49 | if (strcmp(kind, t->kind) == 0) { |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 50 | if (try_module_get(t->owner)) |
| 51 | res = t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | break; |
| 53 | } |
| 54 | } |
| 55 | read_unlock(&cls_mod_lock); |
| 56 | } |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 57 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* Register(unregister) new classifier type */ |
| 61 | |
| 62 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 63 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 64 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int rc = -EEXIST; |
| 66 | |
| 67 | write_lock(&cls_mod_lock); |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 68 | list_for_each_entry(t, &tcf_proto_base, head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (!strcmp(ops->kind, t->kind)) |
| 70 | goto out; |
| 71 | |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 72 | list_add_tail(&ops->head, &tcf_proto_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | rc = 0; |
| 74 | out: |
| 75 | write_unlock(&cls_mod_lock); |
| 76 | return rc; |
| 77 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 78 | EXPORT_SYMBOL(register_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 81 | { |
WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 82 | struct tcf_proto_ops *t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | int rc = -ENOENT; |
| 84 | |
Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 85 | /* Wait for outstanding call_rcu()s, if any, from a |
| 86 | * tcf_proto_ops's destroy() handler. |
| 87 | */ |
| 88 | rcu_barrier(); |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | write_lock(&cls_mod_lock); |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 91 | list_for_each_entry(t, &tcf_proto_base, head) { |
| 92 | if (t == ops) { |
| 93 | list_del(&t->head); |
| 94 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | break; |
Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 96 | } |
| 97 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | write_unlock(&cls_mod_lock); |
| 99 | return rc; |
| 100 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 101 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* Select new prio value from the range, managed by kernel. */ |
| 104 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 105 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 107 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | if (tp) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 110 | first = tp->prio - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Jiri Pirko | 7961973 | 2017-05-17 11:07:58 +0200 | [diff] [blame] | 112 | return TC_H_MAJ(first); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 115 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 116 | u32 prio, u32 parent, struct Qdisc *q, |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 117 | struct tcf_chain *chain) |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 118 | { |
| 119 | struct tcf_proto *tp; |
| 120 | int err; |
| 121 | |
| 122 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); |
| 123 | if (!tp) |
| 124 | return ERR_PTR(-ENOBUFS); |
| 125 | |
| 126 | err = -ENOENT; |
| 127 | tp->ops = tcf_proto_lookup_ops(kind); |
| 128 | if (!tp->ops) { |
| 129 | #ifdef CONFIG_MODULES |
| 130 | rtnl_unlock(); |
| 131 | request_module("cls_%s", kind); |
| 132 | rtnl_lock(); |
| 133 | tp->ops = tcf_proto_lookup_ops(kind); |
| 134 | /* We dropped the RTNL semaphore in order to perform |
| 135 | * the module load. So, even if we succeeded in loading |
| 136 | * the module we have to replay the request. We indicate |
| 137 | * this using -EAGAIN. |
| 138 | */ |
| 139 | if (tp->ops) { |
| 140 | module_put(tp->ops->owner); |
| 141 | err = -EAGAIN; |
| 142 | } else { |
| 143 | err = -ENOENT; |
| 144 | } |
| 145 | goto errout; |
| 146 | #endif |
| 147 | } |
| 148 | tp->classify = tp->ops->classify; |
| 149 | tp->protocol = protocol; |
| 150 | tp->prio = prio; |
| 151 | tp->classid = parent; |
| 152 | tp->q = q; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 153 | tp->chain = chain; |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 154 | |
| 155 | err = tp->ops->init(tp); |
| 156 | if (err) { |
| 157 | module_put(tp->ops->owner); |
| 158 | goto errout; |
| 159 | } |
| 160 | return tp; |
| 161 | |
| 162 | errout: |
| 163 | kfree(tp); |
| 164 | return ERR_PTR(err); |
| 165 | } |
| 166 | |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 167 | static void tcf_proto_destroy(struct tcf_proto *tp) |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 168 | { |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 169 | tp->ops->destroy(tp); |
| 170 | module_put(tp->ops->owner); |
| 171 | kfree_rcu(tp, rcu); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 174 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
| 175 | u32 chain_index) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 176 | { |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 177 | struct tcf_chain *chain; |
| 178 | |
| 179 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| 180 | if (!chain) |
| 181 | return NULL; |
| 182 | list_add_tail(&chain->list, &block->chain_list); |
| 183 | chain->block = block; |
| 184 | chain->index = chain_index; |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 185 | chain->refcnt = 1; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 186 | return chain; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 189 | static void tcf_chain_flush(struct tcf_chain *chain) |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 190 | { |
| 191 | struct tcf_proto *tp; |
| 192 | |
Jiri Pirko | acc8b31 | 2017-08-18 10:10:43 +0200 | [diff] [blame] | 193 | if (chain->p_filter_chain) |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 194 | RCU_INIT_POINTER(*chain->p_filter_chain, NULL); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 195 | while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) { |
| 196 | RCU_INIT_POINTER(chain->filter_chain, tp->next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 197 | tcf_chain_put(chain); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 198 | tcf_proto_destroy(tp); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 199 | } |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static void tcf_chain_destroy(struct tcf_chain *chain) |
| 203 | { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 204 | list_del(&chain->list); |
| 205 | kfree(chain); |
| 206 | } |
Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 207 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 208 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 209 | { |
| 210 | ++chain->refcnt; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 211 | } |
| 212 | |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 213 | struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 214 | bool create) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 215 | { |
| 216 | struct tcf_chain *chain; |
| 217 | |
| 218 | list_for_each_entry(chain, &block->chain_list, list) { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 219 | if (chain->index == chain_index) { |
| 220 | tcf_chain_hold(chain); |
| 221 | return chain; |
| 222 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 223 | } |
Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 224 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 225 | return create ? tcf_chain_create(block, chain_index) : NULL; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 226 | } |
| 227 | EXPORT_SYMBOL(tcf_chain_get); |
| 228 | |
| 229 | void tcf_chain_put(struct tcf_chain *chain) |
| 230 | { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 231 | if (--chain->refcnt == 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 232 | tcf_chain_destroy(chain); |
| 233 | } |
| 234 | EXPORT_SYMBOL(tcf_chain_put); |
| 235 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 236 | static void |
| 237 | tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain, |
| 238 | struct tcf_proto __rcu **p_filter_chain) |
| 239 | { |
| 240 | chain->p_filter_chain = p_filter_chain; |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 241 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 242 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 243 | static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q, |
| 244 | struct tcf_block_ext_info *ei, |
| 245 | enum tc_block_command command) |
| 246 | { |
| 247 | struct net_device *dev = q->dev_queue->dev; |
| 248 | struct tc_block_offload bo = {}; |
| 249 | |
| 250 | if (!tc_can_offload(dev)) |
| 251 | return; |
| 252 | bo.command = command; |
| 253 | bo.binder_type = ei->binder_type; |
| 254 | bo.block = block; |
| 255 | dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
| 256 | } |
| 257 | |
| 258 | static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, |
| 259 | struct tcf_block_ext_info *ei) |
| 260 | { |
| 261 | tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND); |
| 262 | } |
| 263 | |
| 264 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, |
| 265 | struct tcf_block_ext_info *ei) |
| 266 | { |
| 267 | tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND); |
| 268 | } |
| 269 | |
| 270 | int tcf_block_get_ext(struct tcf_block **p_block, |
| 271 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 272 | struct tcf_block_ext_info *ei) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 273 | { |
| 274 | struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 275 | struct tcf_chain *chain; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 276 | int err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 277 | |
| 278 | if (!block) |
| 279 | return -ENOMEM; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 280 | INIT_LIST_HEAD(&block->chain_list); |
| 281 | /* Create chain 0 by default, it has to be always present. */ |
| 282 | chain = tcf_chain_create(block, 0); |
| 283 | if (!chain) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 284 | err = -ENOMEM; |
| 285 | goto err_chain_create; |
| 286 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 287 | tcf_chain_filter_chain_ptr_set(chain, p_filter_chain); |
Jiri Pirko | 855319b | 2017-10-13 14:00:58 +0200 | [diff] [blame] | 288 | block->net = qdisc_net(q); |
Jiri Pirko | 69d78ef | 2017-10-13 14:00:57 +0200 | [diff] [blame] | 289 | block->q = q; |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 290 | tcf_block_offload_bind(block, q, ei); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 291 | *p_block = block; |
| 292 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 293 | |
| 294 | err_chain_create: |
| 295 | kfree(block); |
| 296 | return err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 297 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 298 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 299 | |
| 300 | int tcf_block_get(struct tcf_block **p_block, |
| 301 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q) |
| 302 | { |
| 303 | struct tcf_block_ext_info ei = {0, }; |
| 304 | |
| 305 | return tcf_block_get_ext(p_block, p_filter_chain, q, &ei); |
| 306 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 307 | EXPORT_SYMBOL(tcf_block_get); |
| 308 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 309 | void tcf_block_put_ext(struct tcf_block *block, |
| 310 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 311 | struct tcf_block_ext_info *ei) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 312 | { |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 313 | struct tcf_chain *chain, *tmp; |
| 314 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 315 | if (!block) |
| 316 | return; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 317 | |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 318 | tcf_block_offload_unbind(block, q, ei); |
| 319 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 320 | /* XXX: Standalone actions are not allowed to jump to any chain, and |
| 321 | * bound actions should be all removed after flushing. However, |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 322 | * filters are destroyed in RCU callbacks, we have to hold the chains |
| 323 | * first, otherwise we would always race with RCU callbacks on this list |
| 324 | * without proper locking. |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 325 | */ |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 326 | |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 327 | /* Wait for existing RCU callbacks to cool down. */ |
| 328 | rcu_barrier(); |
| 329 | |
| 330 | /* Hold a refcnt for all chains, except 0, in case they are gone. */ |
| 331 | list_for_each_entry(chain, &block->chain_list, list) |
| 332 | if (chain->index) |
| 333 | tcf_chain_hold(chain); |
| 334 | |
| 335 | /* No race on the list, because no chain could be destroyed. */ |
| 336 | list_for_each_entry(chain, &block->chain_list, list) |
| 337 | tcf_chain_flush(chain); |
| 338 | |
| 339 | /* Wait for RCU callbacks to release the reference count. */ |
| 340 | rcu_barrier(); |
| 341 | |
| 342 | /* At this point, all the chains should have refcnt == 1. */ |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 343 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
| 344 | tcf_chain_put(chain); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 345 | kfree(block); |
| 346 | } |
Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame^] | 347 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 348 | |
| 349 | void tcf_block_put(struct tcf_block *block) |
| 350 | { |
| 351 | struct tcf_block_ext_info ei = {0, }; |
| 352 | |
| 353 | tcf_block_put_ext(block, NULL, block->q, &ei); |
| 354 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 355 | EXPORT_SYMBOL(tcf_block_put); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 356 | |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 357 | /* Main classifier routine: scans classifier chain attached |
| 358 | * to this qdisc, (optionally) tests for protocol and asks |
| 359 | * specific classifiers. |
| 360 | */ |
| 361 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 362 | struct tcf_result *res, bool compat_mode) |
| 363 | { |
| 364 | __be16 protocol = tc_skb_protocol(skb); |
| 365 | #ifdef CONFIG_NET_CLS_ACT |
| 366 | const int max_reclassify_loop = 4; |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 367 | const struct tcf_proto *orig_tp = tp; |
| 368 | const struct tcf_proto *first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 369 | int limit = 0; |
| 370 | |
| 371 | reclassify: |
| 372 | #endif |
| 373 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 374 | int err; |
| 375 | |
| 376 | if (tp->protocol != protocol && |
| 377 | tp->protocol != htons(ETH_P_ALL)) |
| 378 | continue; |
| 379 | |
| 380 | err = tp->classify(skb, tp, res); |
| 381 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 382 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 383 | first_tp = orig_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 384 | goto reset; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 385 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 386 | first_tp = res->goto_tp; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 387 | goto reset; |
| 388 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 389 | #endif |
| 390 | if (err >= 0) |
| 391 | return err; |
| 392 | } |
| 393 | |
| 394 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 395 | #ifdef CONFIG_NET_CLS_ACT |
| 396 | reset: |
| 397 | if (unlikely(limit++ >= max_reclassify_loop)) { |
| 398 | net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n", |
| 399 | tp->q->ops->id, tp->prio & 0xffff, |
| 400 | ntohs(tp->protocol)); |
| 401 | return TC_ACT_SHOT; |
| 402 | } |
| 403 | |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 404 | tp = first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 405 | protocol = tc_skb_protocol(skb); |
| 406 | goto reclassify; |
| 407 | #endif |
| 408 | } |
| 409 | EXPORT_SYMBOL(tcf_classify); |
| 410 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 411 | struct tcf_chain_info { |
| 412 | struct tcf_proto __rcu **pprev; |
| 413 | struct tcf_proto __rcu *next; |
| 414 | }; |
| 415 | |
| 416 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 417 | { |
| 418 | return rtnl_dereference(*chain_info->pprev); |
| 419 | } |
| 420 | |
| 421 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 422 | struct tcf_chain_info *chain_info, |
| 423 | struct tcf_proto *tp) |
| 424 | { |
| 425 | if (chain->p_filter_chain && |
| 426 | *chain_info->pprev == chain->filter_chain) |
Jiri Pirko | 31efcc2 | 2017-05-20 15:01:31 +0200 | [diff] [blame] | 427 | rcu_assign_pointer(*chain->p_filter_chain, tp); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 428 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 429 | rcu_assign_pointer(*chain_info->pprev, tp); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 430 | tcf_chain_hold(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 434 | struct tcf_chain_info *chain_info, |
| 435 | struct tcf_proto *tp) |
| 436 | { |
| 437 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 438 | |
| 439 | if (chain->p_filter_chain && tp == chain->filter_chain) |
Jiri Pirko | 31efcc2 | 2017-05-20 15:01:31 +0200 | [diff] [blame] | 440 | RCU_INIT_POINTER(*chain->p_filter_chain, next); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 441 | RCU_INIT_POINTER(*chain_info->pprev, next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 442 | tcf_chain_put(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 446 | struct tcf_chain_info *chain_info, |
| 447 | u32 protocol, u32 prio, |
| 448 | bool prio_allocate) |
| 449 | { |
| 450 | struct tcf_proto **pprev; |
| 451 | struct tcf_proto *tp; |
| 452 | |
| 453 | /* Check the chain for existence of proto-tcf with this priority */ |
| 454 | for (pprev = &chain->filter_chain; |
| 455 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 456 | if (tp->prio >= prio) { |
| 457 | if (tp->prio == prio) { |
| 458 | if (prio_allocate || |
| 459 | (tp->protocol != protocol && protocol)) |
| 460 | return ERR_PTR(-EINVAL); |
| 461 | } else { |
| 462 | tp = NULL; |
| 463 | } |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | chain_info->pprev = pprev; |
| 468 | chain_info->next = tp ? tp->next : NULL; |
| 469 | return tp; |
| 470 | } |
| 471 | |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 472 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 473 | struct tcf_proto *tp, struct Qdisc *q, u32 parent, |
| 474 | void *fh, u32 portid, u32 seq, u16 flags, int event) |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 475 | { |
| 476 | struct tcmsg *tcm; |
| 477 | struct nlmsghdr *nlh; |
| 478 | unsigned char *b = skb_tail_pointer(skb); |
| 479 | |
| 480 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 481 | if (!nlh) |
| 482 | goto out_nlmsg_trim; |
| 483 | tcm = nlmsg_data(nlh); |
| 484 | tcm->tcm_family = AF_UNSPEC; |
| 485 | tcm->tcm__pad1 = 0; |
| 486 | tcm->tcm__pad2 = 0; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 487 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 488 | tcm->tcm_parent = parent; |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 489 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 490 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 491 | goto nla_put_failure; |
| 492 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 493 | goto nla_put_failure; |
| 494 | if (!fh) { |
| 495 | tcm->tcm_handle = 0; |
| 496 | } else { |
| 497 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 498 | goto nla_put_failure; |
| 499 | } |
| 500 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 501 | return skb->len; |
| 502 | |
| 503 | out_nlmsg_trim: |
| 504 | nla_put_failure: |
| 505 | nlmsg_trim(skb, b); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 510 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 511 | struct Qdisc *q, u32 parent, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 512 | void *fh, int event, bool unicast) |
| 513 | { |
| 514 | struct sk_buff *skb; |
| 515 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 516 | |
| 517 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 518 | if (!skb) |
| 519 | return -ENOBUFS; |
| 520 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 521 | if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 522 | n->nlmsg_flags, event) <= 0) { |
| 523 | kfree_skb(skb); |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
| 527 | if (unicast) |
| 528 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 529 | |
| 530 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 531 | n->nlmsg_flags & NLM_F_ECHO); |
| 532 | } |
| 533 | |
| 534 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 535 | struct nlmsghdr *n, struct tcf_proto *tp, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 536 | struct Qdisc *q, u32 parent, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 537 | void *fh, bool unicast, bool *last) |
| 538 | { |
| 539 | struct sk_buff *skb; |
| 540 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 541 | int err; |
| 542 | |
| 543 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 544 | if (!skb) |
| 545 | return -ENOBUFS; |
| 546 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 547 | if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 548 | n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
| 549 | kfree_skb(skb); |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | |
| 553 | err = tp->ops->delete(tp, fh, last); |
| 554 | if (err) { |
| 555 | kfree_skb(skb); |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | if (unicast) |
| 560 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 561 | |
| 562 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 563 | n->nlmsg_flags & NLM_F_ECHO); |
| 564 | } |
| 565 | |
| 566 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 567 | struct Qdisc *q, u32 parent, |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 568 | struct nlmsghdr *n, |
| 569 | struct tcf_chain *chain, int event) |
| 570 | { |
| 571 | struct tcf_proto *tp; |
| 572 | |
| 573 | for (tp = rtnl_dereference(chain->filter_chain); |
| 574 | tp; tp = rtnl_dereference(tp->next)) |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 575 | tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false); |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | /* Add/change/delete/get a filter node */ |
| 579 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 580 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 581 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 583 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 584 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | struct tcmsg *t; |
| 586 | u32 protocol; |
| 587 | u32 prio; |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 588 | bool prio_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 590 | u32 chain_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | struct net_device *dev; |
| 592 | struct Qdisc *q; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 593 | struct tcf_chain_info chain_info; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 594 | struct tcf_chain *chain = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 595 | struct tcf_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | struct tcf_proto *tp; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 597 | const struct Qdisc_class_ops *cops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | unsigned long cl; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 599 | void *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | int err; |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 601 | int tp_created; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Stéphane Graber | 4e8bbb8 | 2014-04-30 11:25:43 -0400 | [diff] [blame] | 603 | if ((n->nlmsg_type != RTM_GETTFILTER) && |
David S. Miller | 5f013c9b | 2014-05-12 13:19:14 -0400 | [diff] [blame] | 604 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 605 | return -EPERM; |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | replay: |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 608 | tp_created = 0; |
| 609 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 610 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 611 | if (err < 0) |
| 612 | return err; |
| 613 | |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 614 | t = nlmsg_data(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | protocol = TC_H_MIN(t->tcm_info); |
| 616 | prio = TC_H_MAJ(t->tcm_info); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 617 | prio_allocate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | parent = t->tcm_parent; |
| 619 | cl = 0; |
| 620 | |
| 621 | if (prio == 0) { |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 622 | switch (n->nlmsg_type) { |
| 623 | case RTM_DELTFILTER: |
Daniel Borkmann | 9f6ed03 | 2016-06-16 23:19:29 +0200 | [diff] [blame] | 624 | if (protocol || t->tcm_handle || tca[TCA_KIND]) |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 625 | return -ENOENT; |
| 626 | break; |
| 627 | case RTM_NEWTFILTER: |
| 628 | /* If no priority is provided by the user, |
| 629 | * we allocate one. |
| 630 | */ |
| 631 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 632 | prio = TC_H_MAKE(0x80000000U, 0U); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 633 | prio_allocate = true; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | /* fall-through */ |
| 637 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | return -ENOENT; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 639 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* Find head of filter chain. */ |
| 643 | |
| 644 | /* Find link */ |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 645 | dev = __dev_get_by_index(net, t->tcm_ifindex); |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 646 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | return -ENODEV; |
| 648 | |
| 649 | /* Find qdisc */ |
| 650 | if (!parent) { |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 651 | q = dev->qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | parent = q->handle; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 653 | } else { |
| 654 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); |
| 655 | if (q == NULL) |
| 656 | return -EINVAL; |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | /* Is it classful? */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 660 | cops = q->ops->cl_ops; |
| 661 | if (!cops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return -EINVAL; |
| 663 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 664 | if (!cops->tcf_block) |
Patrick McHardy | 71ebe5e | 2009-09-04 06:41:15 +0000 | [diff] [blame] | 665 | return -EOPNOTSUPP; |
| 666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | /* Do we search for filter, attached to class? */ |
| 668 | if (TC_H_MIN(parent)) { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 669 | cl = cops->find(q, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (cl == 0) |
| 671 | return -ENOENT; |
| 672 | } |
| 673 | |
| 674 | /* And the last stroke */ |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 675 | block = cops->tcf_block(q, cl); |
| 676 | if (!block) { |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 677 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 679 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 680 | |
| 681 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 682 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 683 | err = -EINVAL; |
| 684 | goto errout; |
| 685 | } |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 686 | chain = tcf_chain_get(block, chain_index, |
| 687 | n->nlmsg_type == RTM_NEWTFILTER); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 688 | if (!chain) { |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 689 | err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 690 | goto errout; |
| 691 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 692 | |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 693 | if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 694 | tfilter_notify_chain(net, skb, q, parent, n, |
| 695 | chain, RTM_DELTFILTER); |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 696 | tcf_chain_flush(chain); |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 697 | err = 0; |
| 698 | goto errout; |
| 699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 701 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 702 | prio, prio_allocate); |
| 703 | if (IS_ERR(tp)) { |
| 704 | err = PTR_ERR(tp); |
| 705 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | if (tp == NULL) { |
| 709 | /* Proto-tcf does not exist, create new one */ |
| 710 | |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 711 | if (tca[TCA_KIND] == NULL || !protocol) { |
| 712 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 714 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 716 | if (n->nlmsg_type != RTM_NEWTFILTER || |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 717 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
| 718 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 722 | if (prio_allocate) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 723 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 725 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 726 | protocol, prio, parent, q, chain); |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 727 | if (IS_ERR(tp)) { |
| 728 | err = PTR_ERR(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | goto errout; |
| 730 | } |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 731 | tp_created = 1; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 732 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 733 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 735 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | fh = tp->ops->get(tp, t->tcm_handle); |
| 738 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 739 | if (!fh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 741 | tcf_chain_tp_remove(chain, &chain_info, tp); |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 742 | tfilter_notify(net, skb, n, tp, q, parent, fh, |
Eric Dumazet | fa59b27 | 2016-10-09 20:25:55 -0700 | [diff] [blame] | 743 | RTM_DELTFILTER, false); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 744 | tcf_proto_destroy(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | err = 0; |
| 746 | goto errout; |
| 747 | } |
| 748 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 749 | if (n->nlmsg_type != RTM_NEWTFILTER || |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 750 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
| 751 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } else { |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 755 | bool last; |
| 756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | switch (n->nlmsg_type) { |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 758 | case RTM_NEWTFILTER: |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 759 | if (n->nlmsg_flags & NLM_F_EXCL) { |
| 760 | if (tp_created) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 761 | tcf_proto_destroy(tp); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 762 | err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | goto errout; |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 764 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | break; |
| 766 | case RTM_DELTFILTER: |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 767 | err = tfilter_del_notify(net, skb, n, tp, q, parent, |
| 768 | fh, false, &last); |
Jiri Pirko | 40c81b2 | 2017-02-09 14:39:00 +0100 | [diff] [blame] | 769 | if (err) |
| 770 | goto errout; |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 771 | if (last) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 772 | tcf_chain_tp_remove(chain, &chain_info, tp); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 773 | tcf_proto_destroy(tp); |
| 774 | } |
Jiri Pirko | d7cf52c | 2017-02-14 16:27:13 +0100 | [diff] [blame] | 775 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | case RTM_GETTFILTER: |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 777 | err = tfilter_notify(net, skb, n, tp, q, parent, fh, |
Eric Dumazet | fa59b27 | 2016-10-09 20:25:55 -0700 | [diff] [blame] | 778 | RTM_NEWTFILTER, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goto errout; |
| 780 | default: |
| 781 | err = -EINVAL; |
| 782 | goto errout; |
| 783 | } |
| 784 | } |
| 785 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 786 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
| 787 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 788 | if (err == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 789 | if (tp_created) |
| 790 | tcf_chain_tp_insert(chain, &chain_info, tp); |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 791 | tfilter_notify(net, skb, n, tp, q, parent, fh, |
| 792 | RTM_NEWTFILTER, false); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 793 | } else { |
| 794 | if (tp_created) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 795 | tcf_proto_destroy(tp); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 796 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | errout: |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 799 | if (chain) |
| 800 | tcf_chain_put(chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | if (err == -EAGAIN) |
| 802 | /* Replay the request. */ |
| 803 | goto replay; |
| 804 | return err; |
| 805 | } |
| 806 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 807 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | struct tcf_walker w; |
| 809 | struct sk_buff *skb; |
| 810 | struct netlink_callback *cb; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 811 | struct Qdisc *q; |
| 812 | u32 parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | }; |
| 814 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 815 | 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] | 816 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 817 | struct tcf_dump_args *a = (void *)arg; |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 818 | struct net *net = sock_net(a->skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 820 | return tcf_fill_node(net, a->skb, tp, a->q, a->parent, |
| 821 | n, NETLINK_CB(a->cb->skb).portid, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 822 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 823 | RTM_NEWTFILTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 826 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 827 | struct sk_buff *skb, struct netlink_callback *cb, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 828 | long index_start, long *p_index) |
| 829 | { |
| 830 | struct net *net = sock_net(skb->sk); |
| 831 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 832 | struct tcf_dump_args arg; |
| 833 | struct tcf_proto *tp; |
| 834 | |
| 835 | for (tp = rtnl_dereference(chain->filter_chain); |
| 836 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 837 | if (*p_index < index_start) |
| 838 | continue; |
| 839 | if (TC_H_MAJ(tcm->tcm_info) && |
| 840 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 841 | continue; |
| 842 | if (TC_H_MIN(tcm->tcm_info) && |
| 843 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 844 | continue; |
| 845 | if (*p_index > index_start) |
| 846 | memset(&cb->args[1], 0, |
| 847 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 848 | if (cb->args[1] == 0) { |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 849 | if (tcf_fill_node(net, skb, tp, q, parent, 0, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 850 | NETLINK_CB(cb->skb).portid, |
| 851 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 852 | RTM_NEWTFILTER) <= 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 853 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 854 | |
| 855 | cb->args[1] = 1; |
| 856 | } |
| 857 | if (!tp->ops->walk) |
| 858 | continue; |
| 859 | arg.w.fn = tcf_node_dump; |
| 860 | arg.skb = skb; |
| 861 | arg.cb = cb; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 862 | arg.q = q; |
| 863 | arg.parent = parent; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 864 | arg.w.stop = 0; |
| 865 | arg.w.skip = cb->args[1] - 1; |
| 866 | arg.w.count = 0; |
| 867 | tp->ops->walk(tp, &arg.w); |
| 868 | cb->args[1] = arg.w.count + 1; |
| 869 | if (arg.w.stop) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 870 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 871 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 872 | return true; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 875 | /* called with RTNL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 877 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 878 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 879 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | struct net_device *dev; |
| 881 | struct Qdisc *q; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 882 | struct tcf_block *block; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 883 | struct tcf_chain *chain; |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 884 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | unsigned long cl = 0; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 886 | const struct Qdisc_class_ops *cops; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 887 | long index_start; |
| 888 | long index; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 889 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 890 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 892 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | return skb->len; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 894 | |
| 895 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 896 | if (err) |
| 897 | return err; |
| 898 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 899 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 900 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | return skb->len; |
| 902 | |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 903 | parent = tcm->tcm_parent; |
| 904 | if (!parent) { |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 905 | q = dev->qdisc; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 906 | parent = q->handle; |
| 907 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (!q) |
| 911 | goto out; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 912 | cops = q->ops->cl_ops; |
| 913 | if (!cops) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 914 | goto out; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 915 | if (!cops->tcf_block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 916 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | if (TC_H_MIN(tcm->tcm_parent)) { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 918 | cl = cops->find(q, tcm->tcm_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | if (cl == 0) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 920 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 922 | block = cops->tcf_block(q, cl); |
| 923 | if (!block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 924 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 926 | index_start = cb->args[0]; |
| 927 | index = 0; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 928 | |
| 929 | list_for_each_entry(chain, &block->chain_list, list) { |
| 930 | if (tca[TCA_CHAIN] && |
| 931 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 932 | continue; |
Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 933 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
| 934 | index_start, &index)) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 935 | break; |
| 936 | } |
| 937 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 938 | cb->args[0] = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return skb->len; |
| 942 | } |
| 943 | |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 944 | void tcf_exts_destroy(struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | { |
| 946 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 947 | LIST_HEAD(actions); |
| 948 | |
| 949 | tcf_exts_to_list(exts, &actions); |
| 950 | tcf_action_destroy(&actions, TCA_ACT_UNBIND); |
| 951 | kfree(exts->actions); |
| 952 | exts->nr_actions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | #endif |
| 954 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 955 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 957 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 958 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | #ifdef CONFIG_NET_CLS_ACT |
| 961 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | struct tc_action *act; |
| 963 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 964 | if (exts->police && tb[exts->police]) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 965 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 966 | rate_tlv, "police", ovr, |
| 967 | TCA_ACT_BIND); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 968 | if (IS_ERR(act)) |
| 969 | return PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 971 | act->type = exts->type = TCA_OLD_COMPAT; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 972 | exts->actions[0] = act; |
| 973 | exts->nr_actions = 1; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 974 | } else if (exts->action && tb[exts->action]) { |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 975 | LIST_HEAD(actions); |
| 976 | int err, i = 0; |
| 977 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 978 | err = tcf_action_init(net, tp, tb[exts->action], |
| 979 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 980 | &actions); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 981 | if (err) |
| 982 | return err; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 983 | list_for_each_entry(act, &actions, list) |
| 984 | exts->actions[i++] = act; |
| 985 | exts->nr_actions = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | #else |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 989 | if ((exts->action && tb[exts->action]) || |
| 990 | (exts->police && tb[exts->police])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | return -EOPNOTSUPP; |
| 992 | #endif |
| 993 | |
| 994 | return 0; |
| 995 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 996 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 998 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
| 1000 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1001 | struct tcf_exts old = *dst; |
| 1002 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 1003 | *dst = *src; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1004 | tcf_exts_destroy(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | #endif |
| 1006 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1007 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1009 | #ifdef CONFIG_NET_CLS_ACT |
| 1010 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 1011 | { |
| 1012 | if (exts->nr_actions == 0) |
| 1013 | return NULL; |
| 1014 | else |
| 1015 | return exts->actions[0]; |
| 1016 | } |
| 1017 | #endif |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1018 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1019 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | { |
| 1021 | #ifdef CONFIG_NET_CLS_ACT |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1022 | struct nlattr *nest; |
| 1023 | |
Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 1024 | if (exts->action && tcf_exts_has_actions(exts)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | /* |
| 1026 | * again for backward compatible mode - we want |
| 1027 | * to work with both old and new modes of entering |
| 1028 | * tc data even if iproute2 was newer - jhs |
| 1029 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1030 | if (exts->type != TCA_OLD_COMPAT) { |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1031 | LIST_HEAD(actions); |
| 1032 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1033 | nest = nla_nest_start(skb, exts->action); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1034 | if (nest == NULL) |
| 1035 | goto nla_put_failure; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1036 | |
| 1037 | tcf_exts_to_list(exts, &actions); |
| 1038 | if (tcf_action_dump(skb, &actions, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1039 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1040 | nla_nest_end(skb, nest); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1041 | } else if (exts->police) { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1042 | struct tc_action *act = tcf_exts_first_act(exts); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1043 | nest = nla_nest_start(skb, exts->police); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 1044 | if (nest == NULL || !act) |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1045 | goto nla_put_failure; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1046 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1047 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1048 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | return 0; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1052 | |
| 1053 | nla_put_failure: |
| 1054 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | return -1; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1056 | #else |
| 1057 | return 0; |
| 1058 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1060 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1062 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1063 | 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] | 1064 | { |
| 1065 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1066 | struct tc_action *a = tcf_exts_first_act(exts); |
Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 1067 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1068 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | #endif |
| 1070 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1072 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1074 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
| 1075 | enum tc_setup_type type, |
| 1076 | void *type_data, bool err_stop) |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1077 | { |
| 1078 | int ok_count = 0; |
| 1079 | #ifdef CONFIG_NET_CLS_ACT |
| 1080 | const struct tc_action *a; |
| 1081 | struct net_device *dev; |
| 1082 | LIST_HEAD(actions); |
| 1083 | int ret; |
| 1084 | |
| 1085 | if (!tcf_exts_has_actions(exts)) |
| 1086 | return 0; |
| 1087 | |
| 1088 | tcf_exts_to_list(exts, &actions); |
| 1089 | list_for_each_entry(a, &actions, list) { |
| 1090 | if (!a->ops->get_dev) |
| 1091 | continue; |
| 1092 | dev = a->ops->get_dev(a); |
| 1093 | if (!dev || !tc_can_offload(dev)) |
| 1094 | continue; |
| 1095 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); |
| 1096 | if (ret < 0) |
| 1097 | return ret; |
| 1098 | ok_count += ret; |
| 1099 | } |
| 1100 | #endif |
| 1101 | return ok_count; |
| 1102 | } |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1103 | |
| 1104 | int tc_setup_cb_call(struct tcf_exts *exts, enum tc_setup_type type, |
| 1105 | void *type_data, bool err_stop) |
| 1106 | { |
| 1107 | return tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); |
| 1108 | } |
| 1109 | EXPORT_SYMBOL(tc_setup_cb_call); |
Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | static int __init tc_filter_init(void) |
| 1112 | { |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1113 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0); |
| 1114 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0); |
Thomas Graf | 82623c0 | 2007-03-22 11:56:22 -0700 | [diff] [blame] | 1115 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1116 | tc_dump_tfilter, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | subsys_initcall(tc_filter_init); |