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