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