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