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 | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 198 | static void tcf_chain_flush(struct tcf_chain *chain) |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 199 | { |
| 200 | struct tcf_proto *tp; |
| 201 | |
Jiri Pirko | acc8b31 | 2017-08-18 10:10:43 +0200 | [diff] [blame] | 202 | if (chain->p_filter_chain) |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 203 | RCU_INIT_POINTER(*chain->p_filter_chain, NULL); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 204 | while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) { |
| 205 | RCU_INIT_POINTER(chain->filter_chain, tp->next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 206 | tcf_chain_put(chain); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 207 | tcf_proto_destroy(tp); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 208 | } |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static void tcf_chain_destroy(struct tcf_chain *chain) |
| 212 | { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 213 | list_del(&chain->list); |
| 214 | kfree(chain); |
| 215 | } |
Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 216 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 217 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 218 | { |
| 219 | ++chain->refcnt; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 220 | } |
| 221 | |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 222 | struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 223 | bool create) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 224 | { |
| 225 | struct tcf_chain *chain; |
| 226 | |
| 227 | list_for_each_entry(chain, &block->chain_list, list) { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 228 | if (chain->index == chain_index) { |
| 229 | tcf_chain_hold(chain); |
| 230 | return chain; |
| 231 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 232 | } |
Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 233 | |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 234 | return create ? tcf_chain_create(block, chain_index) : NULL; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 235 | } |
| 236 | EXPORT_SYMBOL(tcf_chain_get); |
| 237 | |
| 238 | void tcf_chain_put(struct tcf_chain *chain) |
| 239 | { |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 240 | if (--chain->refcnt == 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 241 | tcf_chain_destroy(chain); |
| 242 | } |
| 243 | EXPORT_SYMBOL(tcf_chain_put); |
| 244 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 245 | static void |
| 246 | tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain, |
| 247 | struct tcf_proto __rcu **p_filter_chain) |
| 248 | { |
| 249 | chain->p_filter_chain = p_filter_chain; |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 250 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 251 | |
| 252 | int tcf_block_get(struct tcf_block **p_block, |
| 253 | struct tcf_proto __rcu **p_filter_chain) |
| 254 | { |
| 255 | struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 256 | struct tcf_chain *chain; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 257 | int err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 258 | |
| 259 | if (!block) |
| 260 | return -ENOMEM; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 261 | INIT_LIST_HEAD(&block->chain_list); |
| 262 | /* Create chain 0 by default, it has to be always present. */ |
| 263 | chain = tcf_chain_create(block, 0); |
| 264 | if (!chain) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 265 | err = -ENOMEM; |
| 266 | goto err_chain_create; |
| 267 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 268 | tcf_chain_filter_chain_ptr_set(chain, p_filter_chain); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 269 | *p_block = block; |
| 270 | return 0; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 271 | |
| 272 | err_chain_create: |
| 273 | kfree(block); |
| 274 | return err; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 275 | } |
| 276 | EXPORT_SYMBOL(tcf_block_get); |
| 277 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 278 | static void tcf_block_put_final(struct work_struct *work) |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 279 | { |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 280 | struct tcf_block *block = container_of(work, struct tcf_block, work); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 281 | struct tcf_chain *chain, *tmp; |
| 282 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 283 | rtnl_lock(); |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 284 | /* Only chain 0 should be still here. */ |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 285 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
| 286 | tcf_chain_put(chain); |
| 287 | rtnl_unlock(); |
| 288 | kfree(block); |
| 289 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 290 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 291 | /* XXX: Standalone actions are not allowed to jump to any chain, and bound |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 292 | * actions should be all removed after flushing. However, filters are now |
| 293 | * destroyed in tc filter workqueue with RTNL lock, they can not race here. |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 294 | */ |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 295 | void tcf_block_put(struct tcf_block *block) |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 296 | { |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 297 | struct tcf_chain *chain, *tmp; |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 298 | |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 299 | if (!block) |
| 300 | return; |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 301 | |
Cong Wang | 822e86d | 2017-10-30 11:10:09 -0700 | [diff] [blame] | 302 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 303 | tcf_chain_flush(chain); |
| 304 | |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 305 | INIT_WORK(&block->work, tcf_block_put_final); |
| 306 | /* Wait for RCU callbacks to release the reference count and make |
| 307 | * sure their works have been queued before this. |
| 308 | */ |
Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 309 | rcu_barrier(); |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 310 | tcf_queue_work(&block->work); |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 311 | } |
| 312 | EXPORT_SYMBOL(tcf_block_put); |
Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 313 | |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 314 | /* Main classifier routine: scans classifier chain attached |
| 315 | * to this qdisc, (optionally) tests for protocol and asks |
| 316 | * specific classifiers. |
| 317 | */ |
| 318 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 319 | struct tcf_result *res, bool compat_mode) |
| 320 | { |
| 321 | __be16 protocol = tc_skb_protocol(skb); |
| 322 | #ifdef CONFIG_NET_CLS_ACT |
| 323 | const int max_reclassify_loop = 4; |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 324 | const struct tcf_proto *orig_tp = tp; |
| 325 | const struct tcf_proto *first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 326 | int limit = 0; |
| 327 | |
| 328 | reclassify: |
| 329 | #endif |
| 330 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 331 | int err; |
| 332 | |
| 333 | if (tp->protocol != protocol && |
| 334 | tp->protocol != htons(ETH_P_ALL)) |
| 335 | continue; |
| 336 | |
| 337 | err = tp->classify(skb, tp, res); |
| 338 | #ifdef CONFIG_NET_CLS_ACT |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 339 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 340 | first_tp = orig_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 341 | goto reset; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 342 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 343 | first_tp = res->goto_tp; |
Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 344 | goto reset; |
| 345 | } |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 346 | #endif |
| 347 | if (err >= 0) |
| 348 | return err; |
| 349 | } |
| 350 | |
| 351 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 352 | #ifdef CONFIG_NET_CLS_ACT |
| 353 | reset: |
| 354 | if (unlikely(limit++ >= max_reclassify_loop)) { |
| 355 | net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n", |
| 356 | tp->q->ops->id, tp->prio & 0xffff, |
| 357 | ntohs(tp->protocol)); |
| 358 | return TC_ACT_SHOT; |
| 359 | } |
| 360 | |
Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 361 | tp = first_tp; |
Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 362 | protocol = tc_skb_protocol(skb); |
| 363 | goto reclassify; |
| 364 | #endif |
| 365 | } |
| 366 | EXPORT_SYMBOL(tcf_classify); |
| 367 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 368 | struct tcf_chain_info { |
| 369 | struct tcf_proto __rcu **pprev; |
| 370 | struct tcf_proto __rcu *next; |
| 371 | }; |
| 372 | |
| 373 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 374 | { |
| 375 | return rtnl_dereference(*chain_info->pprev); |
| 376 | } |
| 377 | |
| 378 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 379 | struct tcf_chain_info *chain_info, |
| 380 | struct tcf_proto *tp) |
| 381 | { |
| 382 | if (chain->p_filter_chain && |
| 383 | *chain_info->pprev == chain->filter_chain) |
Jiri Pirko | 31efcc2 | 2017-05-20 15:01:31 +0200 | [diff] [blame] | 384 | rcu_assign_pointer(*chain->p_filter_chain, tp); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 385 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 386 | rcu_assign_pointer(*chain_info->pprev, tp); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 387 | tcf_chain_hold(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 391 | struct tcf_chain_info *chain_info, |
| 392 | struct tcf_proto *tp) |
| 393 | { |
| 394 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 395 | |
| 396 | if (chain->p_filter_chain && tp == chain->filter_chain) |
Jiri Pirko | 31efcc2 | 2017-05-20 15:01:31 +0200 | [diff] [blame] | 397 | RCU_INIT_POINTER(*chain->p_filter_chain, next); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 398 | RCU_INIT_POINTER(*chain_info->pprev, next); |
Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 399 | tcf_chain_put(chain); |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 403 | struct tcf_chain_info *chain_info, |
| 404 | u32 protocol, u32 prio, |
| 405 | bool prio_allocate) |
| 406 | { |
| 407 | struct tcf_proto **pprev; |
| 408 | struct tcf_proto *tp; |
| 409 | |
| 410 | /* Check the chain for existence of proto-tcf with this priority */ |
| 411 | for (pprev = &chain->filter_chain; |
| 412 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 413 | if (tp->prio >= prio) { |
| 414 | if (tp->prio == prio) { |
| 415 | if (prio_allocate || |
| 416 | (tp->protocol != protocol && protocol)) |
| 417 | return ERR_PTR(-EINVAL); |
| 418 | } else { |
| 419 | tp = NULL; |
| 420 | } |
| 421 | break; |
| 422 | } |
| 423 | } |
| 424 | chain_info->pprev = pprev; |
| 425 | chain_info->next = tp ? tp->next : NULL; |
| 426 | return tp; |
| 427 | } |
| 428 | |
WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 429 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
| 430 | struct tcf_proto *tp, void *fh, u32 portid, |
| 431 | u32 seq, u16 flags, int event) |
| 432 | { |
| 433 | struct tcmsg *tcm; |
| 434 | struct nlmsghdr *nlh; |
| 435 | unsigned char *b = skb_tail_pointer(skb); |
| 436 | |
| 437 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 438 | if (!nlh) |
| 439 | goto out_nlmsg_trim; |
| 440 | tcm = nlmsg_data(nlh); |
| 441 | tcm->tcm_family = AF_UNSPEC; |
| 442 | tcm->tcm__pad1 = 0; |
| 443 | tcm->tcm__pad2 = 0; |
| 444 | tcm->tcm_ifindex = qdisc_dev(tp->q)->ifindex; |
| 445 | tcm->tcm_parent = tp->classid; |
| 446 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 447 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 448 | goto nla_put_failure; |
| 449 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 450 | goto nla_put_failure; |
| 451 | if (!fh) { |
| 452 | tcm->tcm_handle = 0; |
| 453 | } else { |
| 454 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 455 | goto nla_put_failure; |
| 456 | } |
| 457 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 458 | return skb->len; |
| 459 | |
| 460 | out_nlmsg_trim: |
| 461 | nla_put_failure: |
| 462 | nlmsg_trim(skb, b); |
| 463 | return -1; |
| 464 | } |
| 465 | |
| 466 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 467 | struct nlmsghdr *n, struct tcf_proto *tp, |
| 468 | void *fh, int event, bool unicast) |
| 469 | { |
| 470 | struct sk_buff *skb; |
| 471 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 472 | |
| 473 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 474 | if (!skb) |
| 475 | return -ENOBUFS; |
| 476 | |
| 477 | if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, |
| 478 | n->nlmsg_flags, event) <= 0) { |
| 479 | kfree_skb(skb); |
| 480 | return -EINVAL; |
| 481 | } |
| 482 | |
| 483 | if (unicast) |
| 484 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 485 | |
| 486 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 487 | n->nlmsg_flags & NLM_F_ECHO); |
| 488 | } |
| 489 | |
| 490 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 491 | struct nlmsghdr *n, struct tcf_proto *tp, |
| 492 | void *fh, bool unicast, bool *last) |
| 493 | { |
| 494 | struct sk_buff *skb; |
| 495 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 496 | int err; |
| 497 | |
| 498 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 499 | if (!skb) |
| 500 | return -ENOBUFS; |
| 501 | |
| 502 | if (tcf_fill_node(net, skb, tp, fh, portid, n->nlmsg_seq, |
| 503 | n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
| 504 | kfree_skb(skb); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | err = tp->ops->delete(tp, fh, last); |
| 509 | if (err) { |
| 510 | kfree_skb(skb); |
| 511 | return err; |
| 512 | } |
| 513 | |
| 514 | if (unicast) |
| 515 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 516 | |
| 517 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 518 | n->nlmsg_flags & NLM_F_ECHO); |
| 519 | } |
| 520 | |
| 521 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
| 522 | struct nlmsghdr *n, |
| 523 | struct tcf_chain *chain, int event) |
| 524 | { |
| 525 | struct tcf_proto *tp; |
| 526 | |
| 527 | for (tp = rtnl_dereference(chain->filter_chain); |
| 528 | tp; tp = rtnl_dereference(tp->next)) |
| 529 | tfilter_notify(net, oskb, n, tp, 0, event, false); |
| 530 | } |
| 531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | /* Add/change/delete/get a filter node */ |
| 533 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 534 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 535 | struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 537 | struct net *net = sock_net(skb->sk); |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 538 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | struct tcmsg *t; |
| 540 | u32 protocol; |
| 541 | u32 prio; |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 542 | bool prio_allocate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | u32 parent; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 544 | u32 chain_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | struct net_device *dev; |
| 546 | struct Qdisc *q; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 547 | struct tcf_chain_info chain_info; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 548 | struct tcf_chain *chain = NULL; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 549 | struct tcf_block *block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | struct tcf_proto *tp; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 551 | const struct Qdisc_class_ops *cops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | unsigned long cl; |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 553 | void *fh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | int err; |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 555 | int tp_created; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Stéphane Graber | 4e8bbb8 | 2014-04-30 11:25:43 -0400 | [diff] [blame] | 557 | if ((n->nlmsg_type != RTM_GETTFILTER) && |
David S. Miller | 5f013c9b | 2014-05-12 13:19:14 -0400 | [diff] [blame] | 558 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 559 | return -EPERM; |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | replay: |
Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 562 | tp_created = 0; |
| 563 | |
David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 564 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 565 | if (err < 0) |
| 566 | return err; |
| 567 | |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 568 | t = nlmsg_data(n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | protocol = TC_H_MIN(t->tcm_info); |
| 570 | prio = TC_H_MAJ(t->tcm_info); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 571 | prio_allocate = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | parent = t->tcm_parent; |
| 573 | cl = 0; |
| 574 | |
| 575 | if (prio == 0) { |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 576 | switch (n->nlmsg_type) { |
| 577 | case RTM_DELTFILTER: |
Daniel Borkmann | 9f6ed03 | 2016-06-16 23:19:29 +0200 | [diff] [blame] | 578 | if (protocol || t->tcm_handle || tca[TCA_KIND]) |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 579 | return -ENOENT; |
| 580 | break; |
| 581 | case RTM_NEWTFILTER: |
| 582 | /* If no priority is provided by the user, |
| 583 | * we allocate one. |
| 584 | */ |
| 585 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 586 | prio = TC_H_MAKE(0x80000000U, 0U); |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 587 | prio_allocate = true; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | /* fall-through */ |
| 591 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | return -ENOENT; |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* Find head of filter chain. */ |
| 597 | |
| 598 | /* Find link */ |
Tom Goff | 7316ae8 | 2010-03-19 15:40:13 +0000 | [diff] [blame] | 599 | dev = __dev_get_by_index(net, t->tcm_ifindex); |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 600 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | return -ENODEV; |
| 602 | |
| 603 | /* Find qdisc */ |
| 604 | if (!parent) { |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 605 | q = dev->qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | parent = q->handle; |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 607 | } else { |
| 608 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); |
| 609 | if (q == NULL) |
| 610 | return -EINVAL; |
| 611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | /* Is it classful? */ |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 614 | cops = q->ops->cl_ops; |
| 615 | if (!cops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | return -EINVAL; |
| 617 | |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 618 | if (!cops->tcf_block) |
Patrick McHardy | 71ebe5e | 2009-09-04 06:41:15 +0000 | [diff] [blame] | 619 | return -EOPNOTSUPP; |
| 620 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | /* Do we search for filter, attached to class? */ |
| 622 | if (TC_H_MIN(parent)) { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 623 | cl = cops->find(q, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (cl == 0) |
| 625 | return -ENOENT; |
| 626 | } |
| 627 | |
| 628 | /* And the last stroke */ |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 629 | block = cops->tcf_block(q, cl); |
| 630 | if (!block) { |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 631 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 633 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 634 | |
| 635 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 636 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 637 | err = -EINVAL; |
| 638 | goto errout; |
| 639 | } |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 640 | chain = tcf_chain_get(block, chain_index, |
| 641 | n->nlmsg_type == RTM_NEWTFILTER); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 642 | if (!chain) { |
WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 643 | err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 644 | goto errout; |
| 645 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 646 | |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 647 | if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { |
| 648 | tfilter_notify_chain(net, skb, n, chain, RTM_DELTFILTER); |
Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 649 | tcf_chain_flush(chain); |
Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 650 | err = 0; |
| 651 | goto errout; |
| 652 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 654 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 655 | prio, prio_allocate); |
| 656 | if (IS_ERR(tp)) { |
| 657 | err = PTR_ERR(tp); |
| 658 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (tp == NULL) { |
| 662 | /* Proto-tcf does not exist, create new one */ |
| 663 | |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 664 | if (tca[TCA_KIND] == NULL || !protocol) { |
| 665 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 667 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 669 | if (n->nlmsg_type != RTM_NEWTFILTER || |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 670 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
| 671 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 675 | if (prio_allocate) |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 676 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 678 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 679 | protocol, prio, parent, q, chain); |
Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 680 | if (IS_ERR(tp)) { |
| 681 | err = PTR_ERR(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | goto errout; |
| 683 | } |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 684 | tp_created = 1; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 685 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 686 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
| 690 | fh = tp->ops->get(tp, t->tcm_handle); |
| 691 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 692 | if (!fh) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 694 | tcf_chain_tp_remove(chain, &chain_info, tp); |
Eric Dumazet | fa59b27 | 2016-10-09 20:25:55 -0700 | [diff] [blame] | 695 | tfilter_notify(net, skb, n, tp, fh, |
| 696 | RTM_DELTFILTER, false); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 697 | tcf_proto_destroy(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | err = 0; |
| 699 | goto errout; |
| 700 | } |
| 701 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 702 | if (n->nlmsg_type != RTM_NEWTFILTER || |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 703 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
| 704 | err = -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | goto errout; |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } else { |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 708 | bool last; |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | switch (n->nlmsg_type) { |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 711 | case RTM_NEWTFILTER: |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 712 | if (n->nlmsg_flags & NLM_F_EXCL) { |
| 713 | if (tp_created) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 714 | tcf_proto_destroy(tp); |
Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 715 | err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | goto errout; |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 717 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | break; |
| 719 | case RTM_DELTFILTER: |
WANG Cong | 54df2cf | 2017-08-04 21:31:42 -0700 | [diff] [blame] | 720 | err = tfilter_del_notify(net, skb, n, tp, fh, false, |
| 721 | &last); |
Jiri Pirko | 40c81b2 | 2017-02-09 14:39:00 +0100 | [diff] [blame] | 722 | if (err) |
| 723 | goto errout; |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 724 | if (last) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 725 | tcf_chain_tp_remove(chain, &chain_info, tp); |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 726 | tcf_proto_destroy(tp); |
| 727 | } |
Jiri Pirko | d7cf52c | 2017-02-14 16:27:13 +0100 | [diff] [blame] | 728 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | case RTM_GETTFILTER: |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 730 | err = tfilter_notify(net, skb, n, tp, fh, |
Eric Dumazet | fa59b27 | 2016-10-09 20:25:55 -0700 | [diff] [blame] | 731 | RTM_NEWTFILTER, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | goto errout; |
| 733 | default: |
| 734 | err = -EINVAL; |
| 735 | goto errout; |
| 736 | } |
| 737 | } |
| 738 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 739 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
| 740 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 741 | if (err == 0) { |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 742 | if (tp_created) |
| 743 | tcf_chain_tp_insert(chain, &chain_info, tp); |
Eric Dumazet | fa59b27 | 2016-10-09 20:25:55 -0700 | [diff] [blame] | 744 | tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER, false); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 745 | } else { |
| 746 | if (tp_created) |
WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 747 | tcf_proto_destroy(tp); |
Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | errout: |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 751 | if (chain) |
| 752 | tcf_chain_put(chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (err == -EAGAIN) |
| 754 | /* Replay the request. */ |
| 755 | goto replay; |
| 756 | return err; |
| 757 | } |
| 758 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 759 | struct tcf_dump_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | struct tcf_walker w; |
| 761 | struct sk_buff *skb; |
| 762 | struct netlink_callback *cb; |
| 763 | }; |
| 764 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 765 | 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] | 766 | { |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 767 | struct tcf_dump_args *a = (void *)arg; |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 768 | struct net *net = sock_net(a->skb->sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 770 | return tcf_fill_node(net, a->skb, tp, n, NETLINK_CB(a->cb->skb).portid, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 771 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 772 | RTM_NEWTFILTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 775 | static bool tcf_chain_dump(struct tcf_chain *chain, struct sk_buff *skb, |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 776 | struct netlink_callback *cb, |
| 777 | long index_start, long *p_index) |
| 778 | { |
| 779 | struct net *net = sock_net(skb->sk); |
| 780 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 781 | struct tcf_dump_args arg; |
| 782 | struct tcf_proto *tp; |
| 783 | |
| 784 | for (tp = rtnl_dereference(chain->filter_chain); |
| 785 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 786 | if (*p_index < index_start) |
| 787 | continue; |
| 788 | if (TC_H_MAJ(tcm->tcm_info) && |
| 789 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 790 | continue; |
| 791 | if (TC_H_MIN(tcm->tcm_info) && |
| 792 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 793 | continue; |
| 794 | if (*p_index > index_start) |
| 795 | memset(&cb->args[1], 0, |
| 796 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 797 | if (cb->args[1] == 0) { |
| 798 | if (tcf_fill_node(net, skb, tp, 0, |
| 799 | NETLINK_CB(cb->skb).portid, |
| 800 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 801 | RTM_NEWTFILTER) <= 0) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 802 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 803 | |
| 804 | cb->args[1] = 1; |
| 805 | } |
| 806 | if (!tp->ops->walk) |
| 807 | continue; |
| 808 | arg.w.fn = tcf_node_dump; |
| 809 | arg.skb = skb; |
| 810 | arg.cb = cb; |
| 811 | arg.w.stop = 0; |
| 812 | arg.w.skip = cb->args[1] - 1; |
| 813 | arg.w.count = 0; |
| 814 | tp->ops->walk(tp, &arg.w); |
| 815 | cb->args[1] = arg.w.count + 1; |
| 816 | if (arg.w.stop) |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 817 | return false; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 818 | } |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 819 | return true; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 822 | /* called with RTNL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 824 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 825 | struct net *net = sock_net(skb->sk); |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 826 | struct nlattr *tca[TCA_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | struct net_device *dev; |
| 828 | struct Qdisc *q; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 829 | struct tcf_block *block; |
Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 830 | struct tcf_chain *chain; |
David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 831 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | unsigned long cl = 0; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 833 | const struct Qdisc_class_ops *cops; |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 834 | long index_start; |
| 835 | long index; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 836 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 838 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | return skb->len; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 840 | |
| 841 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 842 | if (err) |
| 843 | return err; |
| 844 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 845 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 846 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | return skb->len; |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | if (!tcm->tcm_parent) |
Patrick McHardy | af356af | 2009-09-04 06:41:18 +0000 | [diff] [blame] | 850 | q = dev->qdisc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | else |
| 852 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 853 | if (!q) |
| 854 | goto out; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 855 | cops = q->ops->cl_ops; |
| 856 | if (!cops) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 857 | goto out; |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 858 | if (!cops->tcf_block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 859 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | if (TC_H_MIN(tcm->tcm_parent)) { |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 861 | cl = cops->find(q, tcm->tcm_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | if (cl == 0) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 863 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 865 | block = cops->tcf_block(q, cl); |
| 866 | if (!block) |
WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 867 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 869 | index_start = cb->args[0]; |
| 870 | index = 0; |
Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 871 | |
| 872 | list_for_each_entry(chain, &block->chain_list, list) { |
| 873 | if (tca[TCA_CHAIN] && |
| 874 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 875 | continue; |
| 876 | if (!tcf_chain_dump(chain, skb, cb, index_start, &index)) |
| 877 | break; |
| 878 | } |
| 879 | |
Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 880 | cb->args[0] = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | return skb->len; |
| 884 | } |
| 885 | |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 886 | void tcf_exts_destroy(struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
| 888 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 889 | LIST_HEAD(actions); |
| 890 | |
Cong Wang | 2d132eb | 2017-10-26 18:24:40 -0700 | [diff] [blame] | 891 | ASSERT_RTNL(); |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 892 | tcf_exts_to_list(exts, &actions); |
| 893 | tcf_action_destroy(&actions, TCA_ACT_UNBIND); |
| 894 | kfree(exts->actions); |
| 895 | exts->nr_actions = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | #endif |
| 897 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 898 | EXPORT_SYMBOL(tcf_exts_destroy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 900 | 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] | 901 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | #ifdef CONFIG_NET_CLS_ACT |
| 904 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | struct tc_action *act; |
| 906 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 907 | if (exts->police && tb[exts->police]) { |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 908 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 909 | rate_tlv, "police", ovr, |
| 910 | TCA_ACT_BIND); |
Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 911 | if (IS_ERR(act)) |
| 912 | return PTR_ERR(act); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 914 | act->type = exts->type = TCA_OLD_COMPAT; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 915 | exts->actions[0] = act; |
| 916 | exts->nr_actions = 1; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 917 | } else if (exts->action && tb[exts->action]) { |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 918 | LIST_HEAD(actions); |
| 919 | int err, i = 0; |
| 920 | |
Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 921 | err = tcf_action_init(net, tp, tb[exts->action], |
| 922 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 923 | &actions); |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 924 | if (err) |
| 925 | return err; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 926 | list_for_each_entry(act, &actions, list) |
| 927 | exts->actions[i++] = act; |
| 928 | exts->nr_actions = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | #else |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 932 | if ((exts->action && tb[exts->action]) || |
| 933 | (exts->police && tb[exts->police])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | return -EOPNOTSUPP; |
| 935 | #endif |
| 936 | |
| 937 | return 0; |
| 938 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 939 | EXPORT_SYMBOL(tcf_exts_validate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 941 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
| 943 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 944 | struct tcf_exts old = *dst; |
| 945 | |
Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 946 | *dst = *src; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 947 | tcf_exts_destroy(&old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | #endif |
| 949 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 950 | EXPORT_SYMBOL(tcf_exts_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 952 | #ifdef CONFIG_NET_CLS_ACT |
| 953 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 954 | { |
| 955 | if (exts->nr_actions == 0) |
| 956 | return NULL; |
| 957 | else |
| 958 | return exts->actions[0]; |
| 959 | } |
| 960 | #endif |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 961 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 962 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | { |
| 964 | #ifdef CONFIG_NET_CLS_ACT |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 965 | struct nlattr *nest; |
| 966 | |
Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 967 | if (exts->action && tcf_exts_has_actions(exts)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | /* |
| 969 | * again for backward compatible mode - we want |
| 970 | * to work with both old and new modes of entering |
| 971 | * tc data even if iproute2 was newer - jhs |
| 972 | */ |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 973 | if (exts->type != TCA_OLD_COMPAT) { |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 974 | LIST_HEAD(actions); |
| 975 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 976 | nest = nla_nest_start(skb, exts->action); |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 977 | if (nest == NULL) |
| 978 | goto nla_put_failure; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 979 | |
| 980 | tcf_exts_to_list(exts, &actions); |
| 981 | if (tcf_action_dump(skb, &actions, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 982 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 983 | nla_nest_end(skb, nest); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 984 | } else if (exts->police) { |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 985 | struct tc_action *act = tcf_exts_first_act(exts); |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 986 | nest = nla_nest_start(skb, exts->police); |
Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 987 | if (nest == NULL || !act) |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 988 | goto nla_put_failure; |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 989 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 990 | goto nla_put_failure; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 991 | nla_nest_end(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | return 0; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 995 | |
| 996 | nla_put_failure: |
| 997 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return -1; |
Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 999 | #else |
| 1000 | return 0; |
| 1001 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1003 | EXPORT_SYMBOL(tcf_exts_dump); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1005 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1006 | 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] | 1007 | { |
| 1008 | #ifdef CONFIG_NET_CLS_ACT |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1009 | struct tc_action *a = tcf_exts_first_act(exts); |
Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 1010 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1011 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | #endif |
| 1013 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | } |
Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1015 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 1017 | int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, |
| 1018 | struct net_device **hw_dev) |
| 1019 | { |
| 1020 | #ifdef CONFIG_NET_CLS_ACT |
| 1021 | const struct tc_action *a; |
| 1022 | LIST_HEAD(actions); |
| 1023 | |
Jiri Pirko | 3bcc0ce | 2017-08-04 14:28:58 +0200 | [diff] [blame] | 1024 | if (!tcf_exts_has_actions(exts)) |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 1025 | return -EINVAL; |
| 1026 | |
| 1027 | tcf_exts_to_list(exts, &actions); |
| 1028 | list_for_each_entry(a, &actions, list) { |
| 1029 | if (a->ops->get_dev) { |
| 1030 | a->ops->get_dev(a, dev_net(dev), hw_dev); |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | if (*hw_dev) |
| 1035 | return 0; |
| 1036 | #endif |
| 1037 | return -EOPNOTSUPP; |
| 1038 | } |
| 1039 | EXPORT_SYMBOL(tcf_exts_get_dev); |
| 1040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | static int __init tc_filter_init(void) |
| 1042 | { |
Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 1043 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 1044 | if (!tc_filter_wq) |
| 1045 | return -ENOMEM; |
| 1046 | |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1047 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0); |
| 1048 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0); |
Thomas Graf | 82623c0 | 2007-03-22 11:56:22 -0700 | [diff] [blame] | 1049 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1050 | tc_dump_tfilter, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | subsys_initcall(tc_filter_init); |