| 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> |
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 27 | #include <linux/idr.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 | f34e8bf | 2018-07-23 09:23:04 +0200 | [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 | |
| Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 60 | static const struct tcf_proto_ops * |
| 61 | tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack) |
| 62 | { |
| 63 | const struct tcf_proto_ops *ops; |
| 64 | |
| 65 | ops = __tcf_proto_lookup_ops(kind); |
| 66 | if (ops) |
| 67 | return ops; |
| 68 | #ifdef CONFIG_MODULES |
| 69 | rtnl_unlock(); |
| 70 | request_module("cls_%s", kind); |
| 71 | rtnl_lock(); |
| 72 | ops = __tcf_proto_lookup_ops(kind); |
| 73 | /* We dropped the RTNL semaphore in order to perform |
| 74 | * the module load. So, even if we succeeded in loading |
| 75 | * the module we have to replay the request. We indicate |
| 76 | * this using -EAGAIN. |
| 77 | */ |
| 78 | if (ops) { |
| 79 | module_put(ops->owner); |
| 80 | return ERR_PTR(-EAGAIN); |
| 81 | } |
| 82 | #endif |
| 83 | NL_SET_ERR_MSG(extack, "TC classifier not found"); |
| 84 | return ERR_PTR(-ENOENT); |
| 85 | } |
| 86 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* Register(unregister) new classifier type */ |
| 88 | |
| 89 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 90 | { |
| WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 91 | struct tcf_proto_ops *t; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int rc = -EEXIST; |
| 93 | |
| 94 | write_lock(&cls_mod_lock); |
| WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 95 | list_for_each_entry(t, &tcf_proto_base, head) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | if (!strcmp(ops->kind, t->kind)) |
| 97 | goto out; |
| 98 | |
| WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 99 | list_add_tail(&ops->head, &tcf_proto_base); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | rc = 0; |
| 101 | out: |
| 102 | write_unlock(&cls_mod_lock); |
| 103 | return rc; |
| 104 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 105 | EXPORT_SYMBOL(register_tcf_proto_ops); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 107 | static struct workqueue_struct *tc_filter_wq; |
| 108 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
| 110 | { |
| WANG Cong | 3627287 | 2013-12-15 20:15:11 -0800 | [diff] [blame] | 111 | struct tcf_proto_ops *t; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int rc = -ENOENT; |
| 113 | |
| Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 114 | /* Wait for outstanding call_rcu()s, if any, from a |
| 115 | * tcf_proto_ops's destroy() handler. |
| 116 | */ |
| 117 | rcu_barrier(); |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 118 | flush_workqueue(tc_filter_wq); |
| Daniel Borkmann | c78e174 | 2015-05-20 17:13:33 +0200 | [diff] [blame] | 119 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | write_lock(&cls_mod_lock); |
| Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 121 | list_for_each_entry(t, &tcf_proto_base, head) { |
| 122 | if (t == ops) { |
| 123 | list_del(&t->head); |
| 124 | rc = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | break; |
| Eric Dumazet | dcd7608 | 2013-12-20 10:04:18 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | write_unlock(&cls_mod_lock); |
| 129 | return rc; |
| 130 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 131 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 133 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 134 | { |
| Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 135 | INIT_RCU_WORK(rwork, func); |
| 136 | return queue_rcu_work(tc_filter_wq, rwork); |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 137 | } |
| 138 | EXPORT_SYMBOL(tcf_queue_work); |
| 139 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* Select new prio value from the range, managed by kernel. */ |
| 141 | |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 142 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 144 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
| 146 | if (tp) |
| Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 147 | first = tp->prio - 1; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| Jiri Pirko | 7961973 | 2017-05-17 11:07:58 +0200 | [diff] [blame] | 149 | return TC_H_MAJ(first); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 152 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 153 | u32 prio, struct tcf_chain *chain, |
| 154 | struct netlink_ext_ack *extack) |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 155 | { |
| 156 | struct tcf_proto *tp; |
| 157 | int err; |
| 158 | |
| 159 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); |
| 160 | if (!tp) |
| 161 | return ERR_PTR(-ENOBUFS); |
| 162 | |
| Jiri Pirko | f34e8bf | 2018-07-23 09:23:04 +0200 | [diff] [blame] | 163 | tp->ops = tcf_proto_lookup_ops(kind, extack); |
| 164 | if (IS_ERR(tp->ops)) { |
| 165 | err = PTR_ERR(tp->ops); |
| Jiri Pirko | d68d75f | 2018-05-11 17:45:32 +0200 | [diff] [blame] | 166 | goto errout; |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 167 | } |
| 168 | tp->classify = tp->ops->classify; |
| 169 | tp->protocol = protocol; |
| 170 | tp->prio = prio; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 171 | tp->chain = chain; |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 172 | |
| 173 | err = tp->ops->init(tp); |
| 174 | if (err) { |
| 175 | module_put(tp->ops->owner); |
| 176 | goto errout; |
| 177 | } |
| 178 | return tp; |
| 179 | |
| 180 | errout: |
| 181 | kfree(tp); |
| 182 | return ERR_PTR(err); |
| 183 | } |
| 184 | |
| Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 185 | static void tcf_proto_destroy(struct tcf_proto *tp, |
| 186 | struct netlink_ext_ack *extack) |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 187 | { |
| Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 188 | tp->ops->destroy(tp, extack); |
| WANG Cong | 763dbf6 | 2017-04-19 14:21:21 -0700 | [diff] [blame] | 189 | module_put(tp->ops->owner); |
| 190 | kfree_rcu(tp, rcu); |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 193 | struct tcf_filter_chain_list_item { |
| 194 | struct list_head list; |
| 195 | tcf_chain_head_change_t *chain_head_change; |
| 196 | void *chain_head_change_priv; |
| 197 | }; |
| 198 | |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 199 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
| 200 | u32 chain_index) |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 201 | { |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 202 | struct tcf_chain *chain; |
| 203 | |
| 204 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
| 205 | if (!chain) |
| 206 | return NULL; |
| 207 | list_add_tail(&chain->list, &block->chain_list); |
| 208 | chain->block = block; |
| 209 | chain->index = chain_index; |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 210 | chain->refcnt = 1; |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 211 | if (!chain->index) |
| 212 | block->chain0.chain = chain; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 213 | return chain; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 216 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, |
| 217 | struct tcf_proto *tp_head) |
| 218 | { |
| 219 | if (item->chain_head_change) |
| 220 | item->chain_head_change(tp_head, item->chain_head_change_priv); |
| 221 | } |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 222 | |
| 223 | static void tcf_chain0_head_change(struct tcf_chain *chain, |
| 224 | struct tcf_proto *tp_head) |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 225 | { |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 226 | struct tcf_filter_chain_list_item *item; |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 227 | struct tcf_block *block = chain->block; |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 228 | |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 229 | if (chain->index) |
| 230 | return; |
| 231 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 232 | tcf_chain_head_change_item(item, tp_head); |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 235 | static void tcf_chain_destroy(struct tcf_chain *chain) |
| 236 | { |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 237 | struct tcf_block *block = chain->block; |
| 238 | |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 239 | list_del(&chain->list); |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 240 | if (!chain->index) |
| 241 | block->chain0.chain = NULL; |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 242 | kfree(chain); |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 243 | if (list_empty(&block->chain_list) && !refcount_read(&block->refcnt)) |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 244 | kfree(block); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 245 | } |
| Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 246 | |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 247 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 248 | { |
| 249 | ++chain->refcnt; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 252 | static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 253 | { |
| 254 | /* In case all the references are action references, this |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 255 | * chain should not be shown to the user. |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 256 | */ |
| 257 | return chain->refcnt == chain->action_refcnt; |
| 258 | } |
| 259 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 260 | static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, |
| 261 | u32 chain_index) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 262 | { |
| 263 | struct tcf_chain *chain; |
| 264 | |
| 265 | list_for_each_entry(chain, &block->chain_list, list) { |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 266 | if (chain->index == chain_index) |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 267 | return chain; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 268 | } |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 273 | u32 seq, u16 flags, int event, bool unicast); |
| 274 | |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 275 | static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, |
| 276 | u32 chain_index, bool create, |
| 277 | bool by_act) |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 278 | { |
| 279 | struct tcf_chain *chain = tcf_chain_lookup(block, chain_index); |
| 280 | |
| 281 | if (chain) { |
| 282 | tcf_chain_hold(chain); |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 283 | } else { |
| 284 | if (!create) |
| 285 | return NULL; |
| 286 | chain = tcf_chain_create(block, chain_index); |
| 287 | if (!chain) |
| 288 | return NULL; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 289 | } |
| Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 290 | |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 291 | if (by_act) |
| 292 | ++chain->action_refcnt; |
| 293 | |
| 294 | /* Send notification only in case we got the first |
| 295 | * non-action reference. Until then, the chain acts only as |
| 296 | * a placeholder for actions pointing to it and user ought |
| 297 | * not know about them. |
| 298 | */ |
| 299 | if (chain->refcnt - chain->action_refcnt == 1 && !by_act) |
| 300 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 301 | RTM_NEWCHAIN, false); |
| 302 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 303 | return chain; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 304 | } |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 305 | |
| Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 306 | static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 307 | bool create) |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 308 | { |
| 309 | return __tcf_chain_get(block, chain_index, create, false); |
| 310 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 311 | |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 312 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) |
| 313 | { |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 314 | return __tcf_chain_get(block, chain_index, true, true); |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 315 | } |
| 316 | EXPORT_SYMBOL(tcf_chain_get_by_act); |
| 317 | |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 318 | static void tc_chain_tmplt_del(struct tcf_chain *chain); |
| 319 | |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 320 | static void __tcf_chain_put(struct tcf_chain *chain, bool by_act) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 321 | { |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 322 | if (by_act) |
| 323 | chain->action_refcnt--; |
| 324 | chain->refcnt--; |
| 325 | |
| 326 | /* The last dropped non-action reference will trigger notification. */ |
| 327 | if (chain->refcnt - chain->action_refcnt == 0 && !by_act) |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 328 | tc_chain_notify(chain, NULL, 0, 0, RTM_DELCHAIN, false); |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 329 | |
| 330 | if (chain->refcnt == 0) { |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 331 | tc_chain_tmplt_del(chain); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 332 | tcf_chain_destroy(chain); |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 333 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 334 | } |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 335 | |
| Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 336 | static void tcf_chain_put(struct tcf_chain *chain) |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 337 | { |
| 338 | __tcf_chain_put(chain, false); |
| 339 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 340 | |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 341 | void tcf_chain_put_by_act(struct tcf_chain *chain) |
| 342 | { |
| Jiri Pirko | 5368140 | 2018-08-01 12:36:56 +0200 | [diff] [blame] | 343 | __tcf_chain_put(chain, true); |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 344 | } |
| 345 | EXPORT_SYMBOL(tcf_chain_put_by_act); |
| 346 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 347 | static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) |
| 348 | { |
| 349 | if (chain->explicitly_created) |
| 350 | tcf_chain_put(chain); |
| 351 | } |
| 352 | |
| Jiri Pirko | 290b1c8 | 2018-08-01 12:36:57 +0200 | [diff] [blame] | 353 | static void tcf_chain_flush(struct tcf_chain *chain) |
| 354 | { |
| 355 | struct tcf_proto *tp = rtnl_dereference(chain->filter_chain); |
| 356 | |
| 357 | tcf_chain0_head_change(chain, NULL); |
| 358 | while (tp) { |
| 359 | RCU_INIT_POINTER(chain->filter_chain, tp->next); |
| 360 | tcf_proto_destroy(tp, NULL); |
| 361 | tp = rtnl_dereference(chain->filter_chain); |
| 362 | tcf_chain_put(chain); |
| 363 | } |
| 364 | } |
| 365 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 366 | static bool tcf_block_offload_in_use(struct tcf_block *block) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 367 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 368 | return block->offloadcnt; |
| 369 | } |
| 370 | |
| 371 | static int tcf_block_offload_cmd(struct tcf_block *block, |
| 372 | struct net_device *dev, |
| 373 | struct tcf_block_ext_info *ei, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 374 | enum tc_block_command command, |
| 375 | struct netlink_ext_ack *extack) |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 376 | { |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 377 | struct tc_block_offload bo = {}; |
| 378 | |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 379 | bo.command = command; |
| 380 | bo.binder_type = ei->binder_type; |
| 381 | bo.block = block; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 382 | bo.extack = extack; |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 383 | return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 386 | static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 387 | struct tcf_block_ext_info *ei, |
| 388 | struct netlink_ext_ack *extack) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 389 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 390 | struct net_device *dev = q->dev_queue->dev; |
| 391 | int err; |
| 392 | |
| 393 | if (!dev->netdev_ops->ndo_setup_tc) |
| 394 | goto no_offload_dev_inc; |
| 395 | |
| 396 | /* If tc offload feature is disabled and the block we try to bind |
| 397 | * to already has some offloaded filters, forbid to bind. |
| 398 | */ |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 399 | if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) { |
| 400 | NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled"); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 401 | return -EOPNOTSUPP; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 402 | } |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 403 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 404 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 405 | if (err == -EOPNOTSUPP) |
| 406 | goto no_offload_dev_inc; |
| 407 | return err; |
| 408 | |
| 409 | no_offload_dev_inc: |
| 410 | if (tcf_block_offload_in_use(block)) |
| 411 | return -EOPNOTSUPP; |
| 412 | block->nooffloaddevcnt++; |
| 413 | return 0; |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, |
| 417 | struct tcf_block_ext_info *ei) |
| 418 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 419 | struct net_device *dev = q->dev_queue->dev; |
| 420 | int err; |
| 421 | |
| 422 | if (!dev->netdev_ops->ndo_setup_tc) |
| 423 | goto no_offload_dev_dec; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 424 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 425 | if (err == -EOPNOTSUPP) |
| 426 | goto no_offload_dev_dec; |
| 427 | return; |
| 428 | |
| 429 | no_offload_dev_dec: |
| 430 | WARN_ON(block->nooffloaddevcnt-- == 0); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 433 | static int |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 434 | tcf_chain0_head_change_cb_add(struct tcf_block *block, |
| 435 | struct tcf_block_ext_info *ei, |
| 436 | struct netlink_ext_ack *extack) |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 437 | { |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 438 | struct tcf_chain *chain0 = block->chain0.chain; |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 439 | struct tcf_filter_chain_list_item *item; |
| 440 | |
| 441 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 442 | if (!item) { |
| 443 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); |
| 444 | return -ENOMEM; |
| 445 | } |
| 446 | item->chain_head_change = ei->chain_head_change; |
| 447 | item->chain_head_change_priv = ei->chain_head_change_priv; |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 448 | if (chain0 && chain0->filter_chain) |
| 449 | tcf_chain_head_change_item(item, chain0->filter_chain); |
| 450 | list_add(&item->list, &block->chain0.filter_chain_list); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 455 | tcf_chain0_head_change_cb_del(struct tcf_block *block, |
| 456 | struct tcf_block_ext_info *ei) |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 457 | { |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 458 | struct tcf_chain *chain0 = block->chain0.chain; |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 459 | struct tcf_filter_chain_list_item *item; |
| 460 | |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 461 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) { |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 462 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) || |
| 463 | (item->chain_head_change == ei->chain_head_change && |
| 464 | item->chain_head_change_priv == ei->chain_head_change_priv)) { |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 465 | if (chain0) |
| 466 | tcf_chain_head_change_item(item, NULL); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 467 | list_del(&item->list); |
| 468 | kfree(item); |
| 469 | return; |
| 470 | } |
| 471 | } |
| 472 | WARN_ON(1); |
| 473 | } |
| 474 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 475 | struct tcf_net { |
| 476 | struct idr idr; |
| 477 | }; |
| 478 | |
| 479 | static unsigned int tcf_net_id; |
| 480 | |
| 481 | static int tcf_block_insert(struct tcf_block *block, struct net *net, |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 482 | struct netlink_ext_ack *extack) |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 483 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 484 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 485 | |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 486 | return idr_alloc_u32(&tn->idr, block, &block->index, block->index, |
| 487 | GFP_KERNEL); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 488 | } |
| 489 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 490 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 491 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 492 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 493 | |
| Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 494 | idr_remove(&tn->idr, block->index); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 498 | u32 block_index, |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 499 | struct netlink_ext_ack *extack) |
| 500 | { |
| 501 | struct tcf_block *block; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 502 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 503 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 504 | if (!block) { |
| 505 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 506 | return ERR_PTR(-ENOMEM); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 507 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 508 | INIT_LIST_HEAD(&block->chain_list); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 509 | INIT_LIST_HEAD(&block->cb_list); |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 510 | INIT_LIST_HEAD(&block->owner_list); |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 511 | INIT_LIST_HEAD(&block->chain0.filter_chain_list); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 512 | |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 513 | refcount_set(&block->refcnt, 1); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 514 | block->net = net; |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 515 | block->index = block_index; |
| 516 | |
| 517 | /* Don't store q pointer for blocks which are shared */ |
| 518 | if (!tcf_block_shared(block)) |
| 519 | block->q = q; |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 520 | return block; |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) |
| 524 | { |
| 525 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 526 | |
| Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 527 | return idr_find(&tn->idr, block_index); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 528 | } |
| 529 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 530 | /* Find tcf block. |
| 531 | * Set q, parent, cl when appropriate. |
| 532 | */ |
| 533 | |
| 534 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, |
| 535 | u32 *parent, unsigned long *cl, |
| 536 | int ifindex, u32 block_index, |
| 537 | struct netlink_ext_ack *extack) |
| 538 | { |
| 539 | struct tcf_block *block; |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 540 | int err = 0; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 541 | |
| 542 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 543 | block = tcf_block_lookup(net, block_index); |
| 544 | if (!block) { |
| 545 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); |
| 546 | return ERR_PTR(-EINVAL); |
| 547 | } |
| 548 | } else { |
| 549 | const struct Qdisc_class_ops *cops; |
| 550 | struct net_device *dev; |
| 551 | |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 552 | rcu_read_lock(); |
| 553 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 554 | /* Find link */ |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 555 | dev = dev_get_by_index_rcu(net, ifindex); |
| 556 | if (!dev) { |
| 557 | rcu_read_unlock(); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 558 | return ERR_PTR(-ENODEV); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 559 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 560 | |
| 561 | /* Find qdisc */ |
| 562 | if (!*parent) { |
| 563 | *q = dev->qdisc; |
| 564 | *parent = (*q)->handle; |
| 565 | } else { |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 566 | *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 567 | if (!*q) { |
| 568 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 569 | err = -EINVAL; |
| 570 | goto errout_rcu; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 574 | *q = qdisc_refcount_inc_nz(*q); |
| 575 | if (!*q) { |
| 576 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 577 | err = -EINVAL; |
| 578 | goto errout_rcu; |
| 579 | } |
| 580 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 581 | /* Is it classful? */ |
| 582 | cops = (*q)->ops->cl_ops; |
| 583 | if (!cops) { |
| 584 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 585 | err = -EINVAL; |
| 586 | goto errout_rcu; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | if (!cops->tcf_block) { |
| 590 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 591 | err = -EOPNOTSUPP; |
| 592 | goto errout_rcu; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 593 | } |
| 594 | |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 595 | /* At this point we know that qdisc is not noop_qdisc, |
| 596 | * which means that qdisc holds a reference to net_device |
| 597 | * and we hold a reference to qdisc, so it is safe to release |
| 598 | * rcu read lock. |
| 599 | */ |
| 600 | rcu_read_unlock(); |
| 601 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 602 | /* Do we search for filter, attached to class? */ |
| 603 | if (TC_H_MIN(*parent)) { |
| 604 | *cl = cops->find(*q, *parent); |
| 605 | if (*cl == 0) { |
| 606 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 607 | err = -ENOENT; |
| 608 | goto errout_qdisc; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | /* And the last stroke */ |
| 613 | block = cops->tcf_block(*q, *cl, extack); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 614 | if (!block) { |
| 615 | err = -EINVAL; |
| 616 | goto errout_qdisc; |
| 617 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 618 | if (tcf_block_shared(block)) { |
| 619 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 620 | err = -EOPNOTSUPP; |
| 621 | goto errout_qdisc; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | return block; |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 626 | |
| 627 | errout_rcu: |
| 628 | rcu_read_unlock(); |
| 629 | errout_qdisc: |
| 630 | if (*q) |
| 631 | qdisc_put(*q); |
| 632 | return ERR_PTR(err); |
| 633 | } |
| 634 | |
| 635 | static void tcf_block_release(struct Qdisc *q, struct tcf_block *block) |
| 636 | { |
| 637 | if (q) |
| 638 | qdisc_put(q); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 639 | } |
| 640 | |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 641 | struct tcf_block_owner_item { |
| 642 | struct list_head list; |
| 643 | struct Qdisc *q; |
| 644 | enum tcf_block_binder_type binder_type; |
| 645 | }; |
| 646 | |
| 647 | static void |
| 648 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, |
| 649 | struct Qdisc *q, |
| 650 | enum tcf_block_binder_type binder_type) |
| 651 | { |
| 652 | if (block->keep_dst && |
| 653 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS && |
| 654 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
| 655 | netif_keep_dst(qdisc_dev(q)); |
| 656 | } |
| 657 | |
| 658 | void tcf_block_netif_keep_dst(struct tcf_block *block) |
| 659 | { |
| 660 | struct tcf_block_owner_item *item; |
| 661 | |
| 662 | block->keep_dst = true; |
| 663 | list_for_each_entry(item, &block->owner_list, list) |
| 664 | tcf_block_owner_netif_keep_dst(block, item->q, |
| 665 | item->binder_type); |
| 666 | } |
| 667 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); |
| 668 | |
| 669 | static int tcf_block_owner_add(struct tcf_block *block, |
| 670 | struct Qdisc *q, |
| 671 | enum tcf_block_binder_type binder_type) |
| 672 | { |
| 673 | struct tcf_block_owner_item *item; |
| 674 | |
| 675 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 676 | if (!item) |
| 677 | return -ENOMEM; |
| 678 | item->q = q; |
| 679 | item->binder_type = binder_type; |
| 680 | list_add(&item->list, &block->owner_list); |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static void tcf_block_owner_del(struct tcf_block *block, |
| 685 | struct Qdisc *q, |
| 686 | enum tcf_block_binder_type binder_type) |
| 687 | { |
| 688 | struct tcf_block_owner_item *item; |
| 689 | |
| 690 | list_for_each_entry(item, &block->owner_list, list) { |
| 691 | if (item->q == q && item->binder_type == binder_type) { |
| 692 | list_del(&item->list); |
| 693 | kfree(item); |
| 694 | return; |
| 695 | } |
| 696 | } |
| 697 | WARN_ON(1); |
| 698 | } |
| 699 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 700 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
| 701 | struct tcf_block_ext_info *ei, |
| 702 | struct netlink_ext_ack *extack) |
| 703 | { |
| 704 | struct net *net = qdisc_net(q); |
| 705 | struct tcf_block *block = NULL; |
| 706 | bool created = false; |
| 707 | int err; |
| 708 | |
| 709 | if (ei->block_index) { |
| 710 | /* block_index not 0 means the shared block is requested */ |
| 711 | block = tcf_block_lookup(net, ei->block_index); |
| 712 | if (block) |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 713 | refcount_inc(&block->refcnt); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (!block) { |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 717 | block = tcf_block_create(net, q, ei->block_index, extack); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 718 | if (IS_ERR(block)) |
| 719 | return PTR_ERR(block); |
| 720 | created = true; |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 721 | if (tcf_block_shared(block)) { |
| 722 | err = tcf_block_insert(block, net, extack); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 723 | if (err) |
| 724 | goto err_block_insert; |
| 725 | } |
| 726 | } |
| 727 | |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 728 | err = tcf_block_owner_add(block, q, ei->binder_type); |
| 729 | if (err) |
| 730 | goto err_block_owner_add; |
| 731 | |
| 732 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); |
| 733 | |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 734 | err = tcf_chain0_head_change_cb_add(block, ei, extack); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 735 | if (err) |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 736 | goto err_chain0_head_change_cb_add; |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 737 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 738 | err = tcf_block_offload_bind(block, q, ei, extack); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 739 | if (err) |
| 740 | goto err_block_offload_bind; |
| 741 | |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 742 | *p_block = block; |
| 743 | return 0; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 744 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 745 | err_block_offload_bind: |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 746 | tcf_chain0_head_change_cb_del(block, ei); |
| 747 | err_chain0_head_change_cb_add: |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 748 | tcf_block_owner_del(block, q, ei->binder_type); |
| 749 | err_block_owner_add: |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 750 | if (created) { |
| 751 | if (tcf_block_shared(block)) |
| 752 | tcf_block_remove(block, net); |
| 753 | err_block_insert: |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 754 | kfree(block); |
| 755 | } else { |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 756 | refcount_dec(&block->refcnt); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 757 | } |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 758 | return err; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 759 | } |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 760 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 761 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 762 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
| 763 | { |
| 764 | struct tcf_proto __rcu **p_filter_chain = priv; |
| 765 | |
| 766 | rcu_assign_pointer(*p_filter_chain, tp_head); |
| 767 | } |
| 768 | |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 769 | int tcf_block_get(struct tcf_block **p_block, |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 770 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 771 | struct netlink_ext_ack *extack) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 772 | { |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 773 | struct tcf_block_ext_info ei = { |
| 774 | .chain_head_change = tcf_chain_head_change_dflt, |
| 775 | .chain_head_change_priv = p_filter_chain, |
| 776 | }; |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 777 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 778 | WARN_ON(!p_filter_chain); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 779 | return tcf_block_get_ext(p_block, q, &ei, extack); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 780 | } |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 781 | EXPORT_SYMBOL(tcf_block_get); |
| 782 | |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 783 | /* XXX: Standalone actions are not allowed to jump to any chain, and bound |
| Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 784 | * actions should be all removed after flushing. |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 785 | */ |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 786 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, |
| David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 787 | struct tcf_block_ext_info *ei) |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 788 | { |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 789 | struct tcf_chain *chain, *tmp; |
| Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 790 | |
| David S. Miller | c30abd5 | 2017-12-16 22:11:55 -0500 | [diff] [blame] | 791 | if (!block) |
| 792 | return; |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 793 | tcf_chain0_head_change_cb_del(block, ei); |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 794 | tcf_block_owner_del(block, q, ei->binder_type); |
| Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 795 | |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 796 | if (refcount_dec_and_test(&block->refcnt)) { |
| 797 | /* Flushing/putting all chains will cause the block to be |
| 798 | * deallocated when last chain is freed. However, if chain_list |
| 799 | * is empty, block has to be manually deallocated. After block |
| 800 | * reference counter reached 0, it is no longer possible to |
| 801 | * increment it or add new chains to block. |
| 802 | */ |
| 803 | bool free_block = list_empty(&block->chain_list); |
| 804 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 805 | if (tcf_block_shared(block)) |
| 806 | tcf_block_remove(block, block->net); |
| 807 | |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 808 | if (!free_block) { |
| 809 | /* Hold a refcnt for all chains, so that they don't |
| 810 | * disappear while we are iterating. |
| 811 | */ |
| 812 | list_for_each_entry(chain, &block->chain_list, list) |
| 813 | tcf_chain_hold(chain); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 814 | |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 815 | list_for_each_entry(chain, &block->chain_list, list) |
| 816 | tcf_chain_flush(chain); |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 817 | } |
| Jiri Pirko | df45bf8 | 2017-12-08 19:27:27 +0100 | [diff] [blame] | 818 | |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 819 | tcf_block_offload_unbind(block, q, ei); |
| 820 | |
| 821 | if (free_block) { |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 822 | kfree(block); |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 823 | } else { |
| 824 | /* At this point, all the chains should have |
| 825 | * refcnt >= 1. |
| 826 | */ |
| 827 | list_for_each_entry_safe(chain, tmp, &block->chain_list, |
| 828 | list) { |
| 829 | tcf_chain_put_explicitly_created(chain); |
| 830 | tcf_chain_put(chain); |
| 831 | } |
| 832 | } |
| Jiri Pirko | 63cc5bc | 2018-08-08 14:04:13 +0200 | [diff] [blame] | 833 | } else { |
| Vlad Buslov | cfebd7e | 2018-09-24 19:22:54 +0300 | [diff] [blame^] | 834 | tcf_block_offload_unbind(block, q, ei); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 835 | } |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 836 | } |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 837 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 838 | |
| 839 | void tcf_block_put(struct tcf_block *block) |
| 840 | { |
| 841 | struct tcf_block_ext_info ei = {0, }; |
| 842 | |
| Jiri Pirko | 4853f12 | 2017-12-21 13:13:59 +0100 | [diff] [blame] | 843 | if (!block) |
| 844 | return; |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 845 | tcf_block_put_ext(block, block->q, &ei); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 846 | } |
| David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 847 | |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 848 | EXPORT_SYMBOL(tcf_block_put); |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 849 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 850 | struct tcf_block_cb { |
| 851 | struct list_head list; |
| 852 | tc_setup_cb_t *cb; |
| 853 | void *cb_ident; |
| 854 | void *cb_priv; |
| 855 | unsigned int refcnt; |
| 856 | }; |
| 857 | |
| 858 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) |
| 859 | { |
| 860 | return block_cb->cb_priv; |
| 861 | } |
| 862 | EXPORT_SYMBOL(tcf_block_cb_priv); |
| 863 | |
| 864 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, |
| 865 | tc_setup_cb_t *cb, void *cb_ident) |
| 866 | { struct tcf_block_cb *block_cb; |
| 867 | |
| 868 | list_for_each_entry(block_cb, &block->cb_list, list) |
| 869 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) |
| 870 | return block_cb; |
| 871 | return NULL; |
| 872 | } |
| 873 | EXPORT_SYMBOL(tcf_block_cb_lookup); |
| 874 | |
| 875 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) |
| 876 | { |
| 877 | block_cb->refcnt++; |
| 878 | } |
| 879 | EXPORT_SYMBOL(tcf_block_cb_incref); |
| 880 | |
| 881 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) |
| 882 | { |
| 883 | return --block_cb->refcnt; |
| 884 | } |
| 885 | EXPORT_SYMBOL(tcf_block_cb_decref); |
| 886 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 887 | static int |
| 888 | tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, |
| 889 | void *cb_priv, bool add, bool offload_in_use, |
| 890 | struct netlink_ext_ack *extack) |
| 891 | { |
| 892 | struct tcf_chain *chain; |
| 893 | struct tcf_proto *tp; |
| 894 | int err; |
| 895 | |
| 896 | list_for_each_entry(chain, &block->chain_list, list) { |
| 897 | for (tp = rtnl_dereference(chain->filter_chain); tp; |
| 898 | tp = rtnl_dereference(tp->next)) { |
| 899 | if (tp->ops->reoffload) { |
| 900 | err = tp->ops->reoffload(tp, add, cb, cb_priv, |
| 901 | extack); |
| 902 | if (err && add) |
| 903 | goto err_playback_remove; |
| 904 | } else if (add && offload_in_use) { |
| 905 | err = -EOPNOTSUPP; |
| 906 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); |
| 907 | goto err_playback_remove; |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return 0; |
| 913 | |
| 914 | err_playback_remove: |
| 915 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
| 916 | extack); |
| 917 | return err; |
| 918 | } |
| 919 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 920 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, |
| 921 | tc_setup_cb_t *cb, void *cb_ident, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 922 | void *cb_priv, |
| 923 | struct netlink_ext_ack *extack) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 924 | { |
| 925 | struct tcf_block_cb *block_cb; |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 926 | int err; |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 927 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 928 | /* Replay any already present rules */ |
| 929 | err = tcf_block_playback_offloads(block, cb, cb_priv, true, |
| 930 | tcf_block_offload_in_use(block), |
| 931 | extack); |
| 932 | if (err) |
| 933 | return ERR_PTR(err); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 934 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 935 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); |
| 936 | if (!block_cb) |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 937 | return ERR_PTR(-ENOMEM); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 938 | block_cb->cb = cb; |
| 939 | block_cb->cb_ident = cb_ident; |
| 940 | block_cb->cb_priv = cb_priv; |
| 941 | list_add(&block_cb->list, &block->cb_list); |
| 942 | return block_cb; |
| 943 | } |
| 944 | EXPORT_SYMBOL(__tcf_block_cb_register); |
| 945 | |
| 946 | int tcf_block_cb_register(struct tcf_block *block, |
| 947 | tc_setup_cb_t *cb, void *cb_ident, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 948 | void *cb_priv, struct netlink_ext_ack *extack) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 949 | { |
| 950 | struct tcf_block_cb *block_cb; |
| 951 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 952 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv, |
| 953 | extack); |
| Gustavo A. R. Silva | baa2d2b | 2018-07-18 23:14:17 -0500 | [diff] [blame] | 954 | return PTR_ERR_OR_ZERO(block_cb); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 955 | } |
| 956 | EXPORT_SYMBOL(tcf_block_cb_register); |
| 957 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 958 | void __tcf_block_cb_unregister(struct tcf_block *block, |
| 959 | struct tcf_block_cb *block_cb) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 960 | { |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 961 | tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv, |
| 962 | false, tcf_block_offload_in_use(block), |
| 963 | NULL); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 964 | list_del(&block_cb->list); |
| 965 | kfree(block_cb); |
| 966 | } |
| 967 | EXPORT_SYMBOL(__tcf_block_cb_unregister); |
| 968 | |
| 969 | void tcf_block_cb_unregister(struct tcf_block *block, |
| 970 | tc_setup_cb_t *cb, void *cb_ident) |
| 971 | { |
| 972 | struct tcf_block_cb *block_cb; |
| 973 | |
| 974 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); |
| 975 | if (!block_cb) |
| 976 | return; |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 977 | __tcf_block_cb_unregister(block, block_cb); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 978 | } |
| 979 | EXPORT_SYMBOL(tcf_block_cb_unregister); |
| 980 | |
| 981 | static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 982 | void *type_data, bool err_stop) |
| 983 | { |
| 984 | struct tcf_block_cb *block_cb; |
| 985 | int ok_count = 0; |
| 986 | int err; |
| 987 | |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 988 | /* Make sure all netdevs sharing this block are offload-capable. */ |
| 989 | if (block->nooffloaddevcnt && err_stop) |
| 990 | return -EOPNOTSUPP; |
| 991 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 992 | list_for_each_entry(block_cb, &block->cb_list, list) { |
| 993 | err = block_cb->cb(type, type_data, block_cb->cb_priv); |
| 994 | if (err) { |
| 995 | if (err_stop) |
| 996 | return err; |
| 997 | } else { |
| 998 | ok_count++; |
| 999 | } |
| 1000 | } |
| 1001 | return ok_count; |
| 1002 | } |
| 1003 | |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1004 | /* Main classifier routine: scans classifier chain attached |
| 1005 | * to this qdisc, (optionally) tests for protocol and asks |
| 1006 | * specific classifiers. |
| 1007 | */ |
| 1008 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 1009 | struct tcf_result *res, bool compat_mode) |
| 1010 | { |
| 1011 | __be16 protocol = tc_skb_protocol(skb); |
| 1012 | #ifdef CONFIG_NET_CLS_ACT |
| 1013 | const int max_reclassify_loop = 4; |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1014 | const struct tcf_proto *orig_tp = tp; |
| 1015 | const struct tcf_proto *first_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1016 | int limit = 0; |
| 1017 | |
| 1018 | reclassify: |
| 1019 | #endif |
| 1020 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 1021 | int err; |
| 1022 | |
| 1023 | if (tp->protocol != protocol && |
| 1024 | tp->protocol != htons(ETH_P_ALL)) |
| 1025 | continue; |
| 1026 | |
| 1027 | err = tp->classify(skb, tp, res); |
| 1028 | #ifdef CONFIG_NET_CLS_ACT |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1029 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1030 | first_tp = orig_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1031 | goto reset; |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1032 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1033 | first_tp = res->goto_tp; |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 1034 | goto reset; |
| 1035 | } |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1036 | #endif |
| 1037 | if (err >= 0) |
| 1038 | return err; |
| 1039 | } |
| 1040 | |
| 1041 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 1042 | #ifdef CONFIG_NET_CLS_ACT |
| 1043 | reset: |
| 1044 | if (unlikely(limit++ >= max_reclassify_loop)) { |
| Jiri Pirko | 9d3aaff | 2018-01-17 11:46:47 +0100 | [diff] [blame] | 1045 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
| 1046 | tp->chain->block->index, |
| 1047 | tp->prio & 0xffff, |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1048 | ntohs(tp->protocol)); |
| 1049 | return TC_ACT_SHOT; |
| 1050 | } |
| 1051 | |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 1052 | tp = first_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 1053 | protocol = tc_skb_protocol(skb); |
| 1054 | goto reclassify; |
| 1055 | #endif |
| 1056 | } |
| 1057 | EXPORT_SYMBOL(tcf_classify); |
| 1058 | |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1059 | struct tcf_chain_info { |
| 1060 | struct tcf_proto __rcu **pprev; |
| 1061 | struct tcf_proto __rcu *next; |
| 1062 | }; |
| 1063 | |
| 1064 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 1065 | { |
| 1066 | return rtnl_dereference(*chain_info->pprev); |
| 1067 | } |
| 1068 | |
| 1069 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 1070 | struct tcf_chain_info *chain_info, |
| 1071 | struct tcf_proto *tp) |
| 1072 | { |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1073 | if (*chain_info->pprev == chain->filter_chain) |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1074 | tcf_chain0_head_change(chain, tp); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1075 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 1076 | rcu_assign_pointer(*chain_info->pprev, tp); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1077 | tcf_chain_hold(chain); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 1081 | struct tcf_chain_info *chain_info, |
| 1082 | struct tcf_proto *tp) |
| 1083 | { |
| 1084 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 1085 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 1086 | if (tp == chain->filter_chain) |
| Jiri Pirko | f71e0ca4 | 2018-07-23 09:23:05 +0200 | [diff] [blame] | 1087 | tcf_chain0_head_change(chain, next); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1088 | RCU_INIT_POINTER(*chain_info->pprev, next); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 1089 | tcf_chain_put(chain); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 1093 | struct tcf_chain_info *chain_info, |
| 1094 | u32 protocol, u32 prio, |
| 1095 | bool prio_allocate) |
| 1096 | { |
| 1097 | struct tcf_proto **pprev; |
| 1098 | struct tcf_proto *tp; |
| 1099 | |
| 1100 | /* Check the chain for existence of proto-tcf with this priority */ |
| 1101 | for (pprev = &chain->filter_chain; |
| 1102 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 1103 | if (tp->prio >= prio) { |
| 1104 | if (tp->prio == prio) { |
| 1105 | if (prio_allocate || |
| 1106 | (tp->protocol != protocol && protocol)) |
| 1107 | return ERR_PTR(-EINVAL); |
| 1108 | } else { |
| 1109 | tp = NULL; |
| 1110 | } |
| 1111 | break; |
| 1112 | } |
| 1113 | } |
| 1114 | chain_info->pprev = pprev; |
| 1115 | chain_info->next = tp ? tp->next : NULL; |
| 1116 | return tp; |
| 1117 | } |
| 1118 | |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1119 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1120 | struct tcf_proto *tp, struct tcf_block *block, |
| 1121 | struct Qdisc *q, u32 parent, void *fh, |
| 1122 | u32 portid, u32 seq, u16 flags, int event) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1123 | { |
| 1124 | struct tcmsg *tcm; |
| 1125 | struct nlmsghdr *nlh; |
| 1126 | unsigned char *b = skb_tail_pointer(skb); |
| 1127 | |
| 1128 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1129 | if (!nlh) |
| 1130 | goto out_nlmsg_trim; |
| 1131 | tcm = nlmsg_data(nlh); |
| 1132 | tcm->tcm_family = AF_UNSPEC; |
| 1133 | tcm->tcm__pad1 = 0; |
| 1134 | tcm->tcm__pad2 = 0; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1135 | if (q) { |
| 1136 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 1137 | tcm->tcm_parent = parent; |
| 1138 | } else { |
| 1139 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1140 | tcm->tcm_block_index = block->index; |
| 1141 | } |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1142 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 1143 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 1144 | goto nla_put_failure; |
| 1145 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 1146 | goto nla_put_failure; |
| 1147 | if (!fh) { |
| 1148 | tcm->tcm_handle = 0; |
| 1149 | } else { |
| 1150 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 1151 | goto nla_put_failure; |
| 1152 | } |
| 1153 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1154 | return skb->len; |
| 1155 | |
| 1156 | out_nlmsg_trim: |
| 1157 | nla_put_failure: |
| 1158 | nlmsg_trim(skb, b); |
| 1159 | return -1; |
| 1160 | } |
| 1161 | |
| 1162 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 1163 | struct nlmsghdr *n, struct tcf_proto *tp, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1164 | struct tcf_block *block, struct Qdisc *q, |
| 1165 | u32 parent, void *fh, int event, bool unicast) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1166 | { |
| 1167 | struct sk_buff *skb; |
| 1168 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1169 | |
| 1170 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1171 | if (!skb) |
| 1172 | return -ENOBUFS; |
| 1173 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1174 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1175 | n->nlmsg_seq, n->nlmsg_flags, event) <= 0) { |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1176 | kfree_skb(skb); |
| 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | |
| 1180 | if (unicast) |
| 1181 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1182 | |
| 1183 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1184 | n->nlmsg_flags & NLM_F_ECHO); |
| 1185 | } |
| 1186 | |
| 1187 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 1188 | struct nlmsghdr *n, struct tcf_proto *tp, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1189 | struct tcf_block *block, struct Qdisc *q, |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1190 | u32 parent, void *fh, bool unicast, bool *last, |
| 1191 | struct netlink_ext_ack *extack) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1192 | { |
| 1193 | struct sk_buff *skb; |
| 1194 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1195 | int err; |
| 1196 | |
| 1197 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1198 | if (!skb) |
| 1199 | return -ENOBUFS; |
| 1200 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1201 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1202 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1203 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1204 | kfree_skb(skb); |
| 1205 | return -EINVAL; |
| 1206 | } |
| 1207 | |
| Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 1208 | err = tp->ops->delete(tp, fh, last, extack); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1209 | if (err) { |
| 1210 | kfree_skb(skb); |
| 1211 | return err; |
| 1212 | } |
| 1213 | |
| 1214 | if (unicast) |
| 1215 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1216 | |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1217 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1218 | n->nlmsg_flags & NLM_F_ECHO); |
| 1219 | if (err < 0) |
| 1220 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); |
| 1221 | return err; |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1225 | struct tcf_block *block, struct Qdisc *q, |
| 1226 | u32 parent, struct nlmsghdr *n, |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1227 | struct tcf_chain *chain, int event) |
| 1228 | { |
| 1229 | struct tcf_proto *tp; |
| 1230 | |
| 1231 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1232 | tp; tp = rtnl_dereference(tp->next)) |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1233 | tfilter_notify(net, oskb, n, tp, block, |
| YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1234 | q, parent, NULL, event, false); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1237 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1238 | struct netlink_ext_ack *extack) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | { |
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1240 | struct net *net = sock_net(skb->sk); |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1241 | struct nlattr *tca[TCA_MAX + 1]; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | struct tcmsg *t; |
| 1243 | u32 protocol; |
| 1244 | u32 prio; |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1245 | bool prio_allocate; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | u32 parent; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1247 | u32 chain_index; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1248 | struct Qdisc *q = NULL; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1249 | struct tcf_chain_info chain_info; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1250 | struct tcf_chain *chain = NULL; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1251 | struct tcf_block *block; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | struct tcf_proto *tp; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | unsigned long cl; |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1254 | void *fh; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | int err; |
| Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1256 | int tp_created; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1258 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1259 | return -EPERM; |
| Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1260 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | replay: |
| Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1262 | tp_created = 0; |
| 1263 | |
| David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1264 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1265 | if (err < 0) |
| 1266 | return err; |
| 1267 | |
| David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1268 | t = nlmsg_data(n); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | protocol = TC_H_MIN(t->tcm_info); |
| 1270 | prio = TC_H_MAJ(t->tcm_info); |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1271 | prio_allocate = false; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | parent = t->tcm_parent; |
| 1273 | cl = 0; |
| 1274 | |
| 1275 | if (prio == 0) { |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1276 | /* If no priority is provided by the user, |
| 1277 | * we allocate one. |
| 1278 | */ |
| 1279 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 1280 | prio = TC_H_MAKE(0x80000000U, 0U); |
| 1281 | prio_allocate = true; |
| 1282 | } else { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1283 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | return -ENOENT; |
| Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1285 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | /* Find head of filter chain. */ |
| 1289 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1290 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1291 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1292 | if (IS_ERR(block)) { |
| 1293 | err = PTR_ERR(block); |
| 1294 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1295 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1296 | |
| 1297 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1298 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1299 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1300 | err = -EINVAL; |
| 1301 | goto errout; |
| 1302 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1303 | chain = tcf_chain_get(block, chain_index, true); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1304 | if (!chain) { |
| Jiri Pirko | d5ed72a | 2018-08-27 20:58:43 +0200 | [diff] [blame] | 1305 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1306 | err = -ENOMEM; |
| Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1307 | goto errout; |
| 1308 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1310 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1311 | prio, prio_allocate); |
| 1312 | if (IS_ERR(tp)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1313 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1314 | err = PTR_ERR(tp); |
| 1315 | goto errout; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | if (tp == NULL) { |
| 1319 | /* Proto-tcf does not exist, create new one */ |
| 1320 | |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1321 | if (tca[TCA_KIND] == NULL || !protocol) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1322 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1323 | err = -EINVAL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1325 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1327 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1328 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1329 | err = -ENOENT; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1331 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1333 | if (prio_allocate) |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1334 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1336 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1337 | protocol, prio, chain, extack); |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1338 | if (IS_ERR(tp)) { |
| 1339 | err = PTR_ERR(tp); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | goto errout; |
| 1341 | } |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1342 | tp_created = 1; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1343 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1344 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1345 | err = -EINVAL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1347 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | |
| 1349 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1350 | |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1351 | if (!fh) { |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1352 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1353 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1354 | err = -ENOENT; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1356 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1357 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
| 1358 | NL_SET_ERR_MSG(extack, "Filter already exists"); |
| 1359 | err = -EEXIST; |
| 1360 | goto errout; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1363 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { |
| 1364 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); |
| 1365 | err = -EINVAL; |
| 1366 | goto errout; |
| 1367 | } |
| 1368 | |
| Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 1369 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
| Alexander Aring | 7306db3 | 2018-01-18 11:20:51 -0500 | [diff] [blame] | 1370 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
| 1371 | extack); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1372 | if (err == 0) { |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1373 | if (tp_created) |
| 1374 | tcf_chain_tp_insert(chain, &chain_info, tp); |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1375 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1376 | RTM_NEWTFILTER, false); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1377 | } else { |
| 1378 | if (tp_created) |
| Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 1379 | tcf_proto_destroy(tp, NULL); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1380 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
| 1382 | errout: |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1383 | if (chain) |
| 1384 | tcf_chain_put(chain); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1385 | tcf_block_release(q, block); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | if (err == -EAGAIN) |
| 1387 | /* Replay the request. */ |
| 1388 | goto replay; |
| 1389 | return err; |
| 1390 | } |
| 1391 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1392 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1393 | struct netlink_ext_ack *extack) |
| 1394 | { |
| 1395 | struct net *net = sock_net(skb->sk); |
| 1396 | struct nlattr *tca[TCA_MAX + 1]; |
| 1397 | struct tcmsg *t; |
| 1398 | u32 protocol; |
| 1399 | u32 prio; |
| 1400 | u32 parent; |
| 1401 | u32 chain_index; |
| 1402 | struct Qdisc *q = NULL; |
| 1403 | struct tcf_chain_info chain_info; |
| 1404 | struct tcf_chain *chain = NULL; |
| 1405 | struct tcf_block *block; |
| 1406 | struct tcf_proto *tp = NULL; |
| 1407 | unsigned long cl = 0; |
| 1408 | void *fh = NULL; |
| 1409 | int err; |
| 1410 | |
| 1411 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1412 | return -EPERM; |
| 1413 | |
| 1414 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1415 | if (err < 0) |
| 1416 | return err; |
| 1417 | |
| 1418 | t = nlmsg_data(n); |
| 1419 | protocol = TC_H_MIN(t->tcm_info); |
| 1420 | prio = TC_H_MAJ(t->tcm_info); |
| 1421 | parent = t->tcm_parent; |
| 1422 | |
| 1423 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { |
| 1424 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); |
| 1425 | return -ENOENT; |
| 1426 | } |
| 1427 | |
| 1428 | /* Find head of filter chain. */ |
| 1429 | |
| 1430 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1431 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1432 | if (IS_ERR(block)) { |
| 1433 | err = PTR_ERR(block); |
| 1434 | goto errout; |
| 1435 | } |
| 1436 | |
| 1437 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1438 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1439 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1440 | err = -EINVAL; |
| 1441 | goto errout; |
| 1442 | } |
| 1443 | chain = tcf_chain_get(block, chain_index, false); |
| 1444 | if (!chain) { |
| Jiri Pirko | 5ca8a25 | 2018-08-03 11:08:47 +0200 | [diff] [blame] | 1445 | /* User requested flush on non-existent chain. Nothing to do, |
| 1446 | * so just return success. |
| 1447 | */ |
| 1448 | if (prio == 0) { |
| 1449 | err = 0; |
| 1450 | goto errout; |
| 1451 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1452 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| Jiri Pirko | b7b4247 | 2018-08-27 20:58:44 +0200 | [diff] [blame] | 1453 | err = -ENOENT; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1454 | goto errout; |
| 1455 | } |
| 1456 | |
| 1457 | if (prio == 0) { |
| 1458 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1459 | chain, RTM_DELTFILTER); |
| 1460 | tcf_chain_flush(chain); |
| 1461 | err = 0; |
| 1462 | goto errout; |
| 1463 | } |
| 1464 | |
| 1465 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1466 | prio, false); |
| 1467 | if (!tp || IS_ERR(tp)) { |
| 1468 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1469 | err = tp ? PTR_ERR(tp) : -ENOENT; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1470 | goto errout; |
| 1471 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1472 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1473 | err = -EINVAL; |
| 1474 | goto errout; |
| 1475 | } |
| 1476 | |
| 1477 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1478 | |
| 1479 | if (!fh) { |
| 1480 | if (t->tcm_handle == 0) { |
| 1481 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1482 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| 1483 | RTM_DELTFILTER, false); |
| 1484 | tcf_proto_destroy(tp, extack); |
| 1485 | err = 0; |
| 1486 | } else { |
| 1487 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1488 | err = -ENOENT; |
| 1489 | } |
| 1490 | } else { |
| 1491 | bool last; |
| 1492 | |
| 1493 | err = tfilter_del_notify(net, skb, n, tp, block, |
| 1494 | q, parent, fh, false, &last, |
| 1495 | extack); |
| 1496 | if (err) |
| 1497 | goto errout; |
| 1498 | if (last) { |
| 1499 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1500 | tcf_proto_destroy(tp, extack); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | errout: |
| 1505 | if (chain) |
| 1506 | tcf_chain_put(chain); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1507 | tcf_block_release(q, block); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1508 | return err; |
| 1509 | } |
| 1510 | |
| 1511 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1512 | struct netlink_ext_ack *extack) |
| 1513 | { |
| 1514 | struct net *net = sock_net(skb->sk); |
| 1515 | struct nlattr *tca[TCA_MAX + 1]; |
| 1516 | struct tcmsg *t; |
| 1517 | u32 protocol; |
| 1518 | u32 prio; |
| 1519 | u32 parent; |
| 1520 | u32 chain_index; |
| 1521 | struct Qdisc *q = NULL; |
| 1522 | struct tcf_chain_info chain_info; |
| 1523 | struct tcf_chain *chain = NULL; |
| 1524 | struct tcf_block *block; |
| 1525 | struct tcf_proto *tp = NULL; |
| 1526 | unsigned long cl = 0; |
| 1527 | void *fh = NULL; |
| 1528 | int err; |
| 1529 | |
| 1530 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1531 | if (err < 0) |
| 1532 | return err; |
| 1533 | |
| 1534 | t = nlmsg_data(n); |
| 1535 | protocol = TC_H_MIN(t->tcm_info); |
| 1536 | prio = TC_H_MAJ(t->tcm_info); |
| 1537 | parent = t->tcm_parent; |
| 1538 | |
| 1539 | if (prio == 0) { |
| 1540 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| 1541 | return -ENOENT; |
| 1542 | } |
| 1543 | |
| 1544 | /* Find head of filter chain. */ |
| 1545 | |
| 1546 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1547 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1548 | if (IS_ERR(block)) { |
| 1549 | err = PTR_ERR(block); |
| 1550 | goto errout; |
| 1551 | } |
| 1552 | |
| 1553 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1554 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1555 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1556 | err = -EINVAL; |
| 1557 | goto errout; |
| 1558 | } |
| 1559 | chain = tcf_chain_get(block, chain_index, false); |
| 1560 | if (!chain) { |
| 1561 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1562 | err = -EINVAL; |
| 1563 | goto errout; |
| 1564 | } |
| 1565 | |
| 1566 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1567 | prio, false); |
| 1568 | if (!tp || IS_ERR(tp)) { |
| 1569 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1570 | err = tp ? PTR_ERR(tp) : -ENOENT; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1571 | goto errout; |
| 1572 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1573 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1574 | err = -EINVAL; |
| 1575 | goto errout; |
| 1576 | } |
| 1577 | |
| 1578 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1579 | |
| 1580 | if (!fh) { |
| 1581 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1582 | err = -ENOENT; |
| 1583 | } else { |
| 1584 | err = tfilter_notify(net, skb, n, tp, block, q, parent, |
| 1585 | fh, RTM_NEWTFILTER, true); |
| 1586 | if (err < 0) |
| 1587 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); |
| 1588 | } |
| 1589 | |
| 1590 | errout: |
| 1591 | if (chain) |
| 1592 | tcf_chain_put(chain); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1593 | tcf_block_release(q, block); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1594 | return err; |
| 1595 | } |
| 1596 | |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1597 | struct tcf_dump_args { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | struct tcf_walker w; |
| 1599 | struct sk_buff *skb; |
| 1600 | struct netlink_callback *cb; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1601 | struct tcf_block *block; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1602 | struct Qdisc *q; |
| 1603 | u32 parent; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | }; |
| 1605 | |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1606 | static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | { |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1608 | struct tcf_dump_args *a = (void *)arg; |
| WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 1609 | struct net *net = sock_net(a->skb->sk); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1611 | return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent, |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1612 | n, NETLINK_CB(a->cb->skb).portid, |
| Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1613 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1614 | RTM_NEWTFILTER); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1617 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 1618 | struct sk_buff *skb, struct netlink_callback *cb, |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1619 | long index_start, long *p_index) |
| 1620 | { |
| 1621 | struct net *net = sock_net(skb->sk); |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1622 | struct tcf_block *block = chain->block; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1623 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 1624 | struct tcf_dump_args arg; |
| 1625 | struct tcf_proto *tp; |
| 1626 | |
| 1627 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1628 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 1629 | if (*p_index < index_start) |
| 1630 | continue; |
| 1631 | if (TC_H_MAJ(tcm->tcm_info) && |
| 1632 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 1633 | continue; |
| 1634 | if (TC_H_MIN(tcm->tcm_info) && |
| 1635 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 1636 | continue; |
| 1637 | if (*p_index > index_start) |
| 1638 | memset(&cb->args[1], 0, |
| 1639 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 1640 | if (cb->args[1] == 0) { |
| YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1641 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1642 | NETLINK_CB(cb->skb).portid, |
| 1643 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1644 | RTM_NEWTFILTER) <= 0) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1645 | return false; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1646 | |
| 1647 | cb->args[1] = 1; |
| 1648 | } |
| 1649 | if (!tp->ops->walk) |
| 1650 | continue; |
| 1651 | arg.w.fn = tcf_node_dump; |
| 1652 | arg.skb = skb; |
| 1653 | arg.cb = cb; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1654 | arg.block = block; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1655 | arg.q = q; |
| 1656 | arg.parent = parent; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1657 | arg.w.stop = 0; |
| 1658 | arg.w.skip = cb->args[1] - 1; |
| 1659 | arg.w.count = 0; |
| Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1660 | arg.w.cookie = cb->args[2]; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1661 | tp->ops->walk(tp, &arg.w); |
| Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1662 | cb->args[2] = arg.w.cookie; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1663 | cb->args[1] = arg.w.count + 1; |
| 1664 | if (arg.w.stop) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1665 | return false; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1666 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1667 | return true; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1668 | } |
| 1669 | |
| Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 1670 | /* called with RTNL */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 1672 | { |
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1673 | struct net *net = sock_net(skb->sk); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1674 | struct nlattr *tca[TCA_MAX + 1]; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1675 | struct Qdisc *q = NULL; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1676 | struct tcf_block *block; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1677 | struct tcf_chain *chain; |
| David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1678 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1679 | long index_start; |
| 1680 | long index; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1681 | u32 parent; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1682 | int err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
| Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1684 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | return skb->len; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1686 | |
| 1687 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 1688 | if (err) |
| 1689 | return err; |
| 1690 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1691 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 1692 | block = tcf_block_lookup(net, tcm->tcm_block_index); |
| 1693 | if (!block) |
| WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 1694 | goto out; |
| Jiri Pirko | d680b35 | 2018-01-18 16:14:49 +0100 | [diff] [blame] | 1695 | /* If we work with block index, q is NULL and parent value |
| 1696 | * will never be used in the following code. The check |
| 1697 | * in tcf_fill_node prevents it. However, compiler does not |
| 1698 | * see that far, so set parent to zero to silence the warning |
| 1699 | * about parent being uninitialized. |
| 1700 | */ |
| 1701 | parent = 0; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1702 | } else { |
| 1703 | const struct Qdisc_class_ops *cops; |
| 1704 | struct net_device *dev; |
| 1705 | unsigned long cl = 0; |
| 1706 | |
| 1707 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 1708 | if (!dev) |
| 1709 | return skb->len; |
| 1710 | |
| 1711 | parent = tcm->tcm_parent; |
| 1712 | if (!parent) { |
| 1713 | q = dev->qdisc; |
| 1714 | parent = q->handle; |
| 1715 | } else { |
| 1716 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 1717 | } |
| 1718 | if (!q) |
| 1719 | goto out; |
| 1720 | cops = q->ops->cl_ops; |
| 1721 | if (!cops) |
| 1722 | goto out; |
| 1723 | if (!cops->tcf_block) |
| 1724 | goto out; |
| 1725 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 1726 | cl = cops->find(q, tcm->tcm_parent); |
| 1727 | if (cl == 0) |
| 1728 | goto out; |
| 1729 | } |
| 1730 | block = cops->tcf_block(q, cl, NULL); |
| 1731 | if (!block) |
| 1732 | goto out; |
| 1733 | if (tcf_block_shared(block)) |
| 1734 | q = NULL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1737 | index_start = cb->args[0]; |
| 1738 | index = 0; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1739 | |
| 1740 | list_for_each_entry(chain, &block->chain_list, list) { |
| 1741 | if (tca[TCA_CHAIN] && |
| 1742 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 1743 | continue; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1744 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1745 | index_start, &index)) { |
| 1746 | err = -EMSGSIZE; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1747 | break; |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1748 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1749 | } |
| 1750 | |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1751 | cb->args[0] = index; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | out: |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1754 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 1755 | if (skb->len == 0 && err) |
| 1756 | return err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | return skb->len; |
| 1758 | } |
| 1759 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1760 | static int tc_chain_fill_node(struct tcf_chain *chain, struct net *net, |
| 1761 | struct sk_buff *skb, struct tcf_block *block, |
| 1762 | u32 portid, u32 seq, u16 flags, int event) |
| 1763 | { |
| 1764 | unsigned char *b = skb_tail_pointer(skb); |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1765 | const struct tcf_proto_ops *ops; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1766 | struct nlmsghdr *nlh; |
| 1767 | struct tcmsg *tcm; |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1768 | void *priv; |
| 1769 | |
| 1770 | ops = chain->tmplt_ops; |
| 1771 | priv = chain->tmplt_priv; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1772 | |
| 1773 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1774 | if (!nlh) |
| 1775 | goto out_nlmsg_trim; |
| 1776 | tcm = nlmsg_data(nlh); |
| 1777 | tcm->tcm_family = AF_UNSPEC; |
| 1778 | tcm->tcm__pad1 = 0; |
| 1779 | tcm->tcm__pad2 = 0; |
| 1780 | tcm->tcm_handle = 0; |
| 1781 | if (block->q) { |
| 1782 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; |
| 1783 | tcm->tcm_parent = block->q->handle; |
| 1784 | } else { |
| 1785 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1786 | tcm->tcm_block_index = block->index; |
| 1787 | } |
| 1788 | |
| 1789 | if (nla_put_u32(skb, TCA_CHAIN, chain->index)) |
| 1790 | goto nla_put_failure; |
| 1791 | |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1792 | if (ops) { |
| 1793 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
| 1794 | goto nla_put_failure; |
| 1795 | if (ops->tmplt_dump(skb, net, priv) < 0) |
| 1796 | goto nla_put_failure; |
| 1797 | } |
| 1798 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1799 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1800 | return skb->len; |
| 1801 | |
| 1802 | out_nlmsg_trim: |
| 1803 | nla_put_failure: |
| 1804 | nlmsg_trim(skb, b); |
| 1805 | return -EMSGSIZE; |
| 1806 | } |
| 1807 | |
| 1808 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, |
| 1809 | u32 seq, u16 flags, int event, bool unicast) |
| 1810 | { |
| 1811 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1812 | struct tcf_block *block = chain->block; |
| 1813 | struct net *net = block->net; |
| 1814 | struct sk_buff *skb; |
| 1815 | |
| 1816 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1817 | if (!skb) |
| 1818 | return -ENOBUFS; |
| 1819 | |
| 1820 | if (tc_chain_fill_node(chain, net, skb, block, portid, |
| 1821 | seq, flags, event) <= 0) { |
| 1822 | kfree_skb(skb); |
| 1823 | return -EINVAL; |
| 1824 | } |
| 1825 | |
| 1826 | if (unicast) |
| 1827 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1828 | |
| 1829 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); |
| 1830 | } |
| 1831 | |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1832 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, |
| 1833 | struct nlattr **tca, |
| 1834 | struct netlink_ext_ack *extack) |
| 1835 | { |
| 1836 | const struct tcf_proto_ops *ops; |
| 1837 | void *tmplt_priv; |
| 1838 | |
| 1839 | /* If kind is not set, user did not specify template. */ |
| 1840 | if (!tca[TCA_KIND]) |
| 1841 | return 0; |
| 1842 | |
| 1843 | ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack); |
| 1844 | if (IS_ERR(ops)) |
| 1845 | return PTR_ERR(ops); |
| 1846 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { |
| 1847 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); |
| 1848 | return -EOPNOTSUPP; |
| 1849 | } |
| 1850 | |
| 1851 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack); |
| 1852 | if (IS_ERR(tmplt_priv)) { |
| 1853 | module_put(ops->owner); |
| 1854 | return PTR_ERR(tmplt_priv); |
| 1855 | } |
| 1856 | chain->tmplt_ops = ops; |
| 1857 | chain->tmplt_priv = tmplt_priv; |
| 1858 | return 0; |
| 1859 | } |
| 1860 | |
| 1861 | static void tc_chain_tmplt_del(struct tcf_chain *chain) |
| 1862 | { |
| 1863 | const struct tcf_proto_ops *ops = chain->tmplt_ops; |
| 1864 | |
| 1865 | /* If template ops are set, no work to do for us. */ |
| 1866 | if (!ops) |
| 1867 | return; |
| 1868 | |
| 1869 | ops->tmplt_destroy(chain->tmplt_priv); |
| 1870 | module_put(ops->owner); |
| 1871 | } |
| 1872 | |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1873 | /* Add/delete/get a chain */ |
| 1874 | |
| 1875 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, |
| 1876 | struct netlink_ext_ack *extack) |
| 1877 | { |
| 1878 | struct net *net = sock_net(skb->sk); |
| 1879 | struct nlattr *tca[TCA_MAX + 1]; |
| 1880 | struct tcmsg *t; |
| 1881 | u32 parent; |
| 1882 | u32 chain_index; |
| 1883 | struct Qdisc *q = NULL; |
| 1884 | struct tcf_chain *chain = NULL; |
| 1885 | struct tcf_block *block; |
| 1886 | unsigned long cl; |
| 1887 | int err; |
| 1888 | |
| 1889 | if (n->nlmsg_type != RTM_GETCHAIN && |
| 1890 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1891 | return -EPERM; |
| 1892 | |
| 1893 | replay: |
| 1894 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1895 | if (err < 0) |
| 1896 | return err; |
| 1897 | |
| 1898 | t = nlmsg_data(n); |
| 1899 | parent = t->tcm_parent; |
| 1900 | cl = 0; |
| 1901 | |
| 1902 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1903 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1904 | if (IS_ERR(block)) |
| 1905 | return PTR_ERR(block); |
| 1906 | |
| 1907 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1908 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1909 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1910 | err = -EINVAL; |
| 1911 | goto errout_block; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1912 | } |
| 1913 | chain = tcf_chain_lookup(block, chain_index); |
| 1914 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
| 1915 | if (chain) { |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1916 | if (tcf_chain_held_by_acts_only(chain)) { |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1917 | /* The chain exists only because there is |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1918 | * some action referencing it. |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1919 | */ |
| 1920 | tcf_chain_hold(chain); |
| 1921 | } else { |
| 1922 | NL_SET_ERR_MSG(extack, "Filter chain already exists"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1923 | err = -EEXIST; |
| 1924 | goto errout_block; |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1925 | } |
| 1926 | } else { |
| 1927 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| 1928 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1929 | err = -ENOENT; |
| 1930 | goto errout_block; |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1931 | } |
| 1932 | chain = tcf_chain_create(block, chain_index); |
| 1933 | if (!chain) { |
| 1934 | NL_SET_ERR_MSG(extack, "Failed to create filter chain"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1935 | err = -ENOMEM; |
| 1936 | goto errout_block; |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 1937 | } |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1938 | } |
| 1939 | } else { |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 1940 | if (!chain || tcf_chain_held_by_acts_only(chain)) { |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1941 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1942 | err = -EINVAL; |
| 1943 | goto errout_block; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1944 | } |
| 1945 | tcf_chain_hold(chain); |
| 1946 | } |
| 1947 | |
| 1948 | switch (n->nlmsg_type) { |
| 1949 | case RTM_NEWCHAIN: |
| Jiri Pirko | 9f407f1 | 2018-07-23 09:23:07 +0200 | [diff] [blame] | 1950 | err = tc_chain_tmplt_add(chain, net, tca, extack); |
| 1951 | if (err) |
| 1952 | goto errout; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1953 | /* In case the chain was successfully added, take a reference |
| 1954 | * to the chain. This ensures that an empty chain |
| 1955 | * does not disappear at the end of this function. |
| 1956 | */ |
| 1957 | tcf_chain_hold(chain); |
| 1958 | chain->explicitly_created = true; |
| 1959 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
| 1960 | RTM_NEWCHAIN, false); |
| 1961 | break; |
| 1962 | case RTM_DELCHAIN: |
| Cong Wang | f5b9bac | 2018-09-11 14:22:23 -0700 | [diff] [blame] | 1963 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1964 | chain, RTM_DELTFILTER); |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1965 | /* Flush the chain first as the user requested chain removal. */ |
| 1966 | tcf_chain_flush(chain); |
| 1967 | /* In case the chain was successfully deleted, put a reference |
| 1968 | * to the chain previously taken during addition. |
| 1969 | */ |
| 1970 | tcf_chain_put_explicitly_created(chain); |
| Jiri Pirko | c921d7d | 2018-07-26 18:27:58 +0200 | [diff] [blame] | 1971 | chain->explicitly_created = false; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1972 | break; |
| 1973 | case RTM_GETCHAIN: |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1974 | err = tc_chain_notify(chain, skb, n->nlmsg_seq, |
| 1975 | n->nlmsg_seq, n->nlmsg_type, true); |
| 1976 | if (err < 0) |
| 1977 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); |
| 1978 | break; |
| 1979 | default: |
| 1980 | err = -EOPNOTSUPP; |
| 1981 | NL_SET_ERR_MSG(extack, "Unsupported message type"); |
| 1982 | goto errout; |
| 1983 | } |
| 1984 | |
| 1985 | errout: |
| 1986 | tcf_chain_put(chain); |
| Vlad Buslov | e368fdb | 2018-09-24 19:22:53 +0300 | [diff] [blame] | 1987 | errout_block: |
| 1988 | tcf_block_release(q, block); |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 1989 | if (err == -EAGAIN) |
| 1990 | /* Replay the request. */ |
| 1991 | goto replay; |
| 1992 | return err; |
| 1993 | } |
| 1994 | |
| 1995 | /* called with RTNL */ |
| 1996 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) |
| 1997 | { |
| 1998 | struct net *net = sock_net(skb->sk); |
| 1999 | struct nlattr *tca[TCA_MAX + 1]; |
| 2000 | struct Qdisc *q = NULL; |
| 2001 | struct tcf_block *block; |
| 2002 | struct tcf_chain *chain; |
| 2003 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 2004 | long index_start; |
| 2005 | long index; |
| 2006 | u32 parent; |
| 2007 | int err; |
| 2008 | |
| 2009 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| 2010 | return skb->len; |
| 2011 | |
| 2012 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 2013 | if (err) |
| 2014 | return err; |
| 2015 | |
| 2016 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 2017 | block = tcf_block_lookup(net, tcm->tcm_block_index); |
| 2018 | if (!block) |
| 2019 | goto out; |
| 2020 | /* If we work with block index, q is NULL and parent value |
| 2021 | * will never be used in the following code. The check |
| 2022 | * in tcf_fill_node prevents it. However, compiler does not |
| 2023 | * see that far, so set parent to zero to silence the warning |
| 2024 | * about parent being uninitialized. |
| 2025 | */ |
| 2026 | parent = 0; |
| 2027 | } else { |
| 2028 | const struct Qdisc_class_ops *cops; |
| 2029 | struct net_device *dev; |
| 2030 | unsigned long cl = 0; |
| 2031 | |
| 2032 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 2033 | if (!dev) |
| 2034 | return skb->len; |
| 2035 | |
| 2036 | parent = tcm->tcm_parent; |
| 2037 | if (!parent) { |
| 2038 | q = dev->qdisc; |
| 2039 | parent = q->handle; |
| 2040 | } else { |
| 2041 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 2042 | } |
| 2043 | if (!q) |
| 2044 | goto out; |
| 2045 | cops = q->ops->cl_ops; |
| 2046 | if (!cops) |
| 2047 | goto out; |
| 2048 | if (!cops->tcf_block) |
| 2049 | goto out; |
| 2050 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 2051 | cl = cops->find(q, tcm->tcm_parent); |
| 2052 | if (cl == 0) |
| 2053 | goto out; |
| 2054 | } |
| 2055 | block = cops->tcf_block(q, cl, NULL); |
| 2056 | if (!block) |
| 2057 | goto out; |
| 2058 | if (tcf_block_shared(block)) |
| 2059 | q = NULL; |
| 2060 | } |
| 2061 | |
| 2062 | index_start = cb->args[0]; |
| 2063 | index = 0; |
| 2064 | |
| 2065 | list_for_each_entry(chain, &block->chain_list, list) { |
| 2066 | if ((tca[TCA_CHAIN] && |
| 2067 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)) |
| 2068 | continue; |
| 2069 | if (index < index_start) { |
| 2070 | index++; |
| 2071 | continue; |
| 2072 | } |
| Jiri Pirko | 3d32f4c | 2018-08-01 12:36:55 +0200 | [diff] [blame] | 2073 | if (tcf_chain_held_by_acts_only(chain)) |
| Jiri Pirko | 1f3ed38 | 2018-07-27 09:45:05 +0200 | [diff] [blame] | 2074 | continue; |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2075 | err = tc_chain_fill_node(chain, net, skb, block, |
| 2076 | NETLINK_CB(cb->skb).portid, |
| 2077 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 2078 | RTM_NEWCHAIN); |
| 2079 | if (err <= 0) |
| 2080 | break; |
| 2081 | index++; |
| 2082 | } |
| 2083 | |
| 2084 | cb->args[0] = index; |
| 2085 | |
| 2086 | out: |
| 2087 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 2088 | if (skb->len == 0 && err) |
| 2089 | return err; |
| 2090 | return skb->len; |
| 2091 | } |
| 2092 | |
| WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 2093 | void tcf_exts_destroy(struct tcf_exts *exts) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | { |
| 2095 | #ifdef CONFIG_NET_CLS_ACT |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2096 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2097 | kfree(exts->actions); |
| 2098 | exts->nr_actions = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | #endif |
| 2100 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2101 | EXPORT_SYMBOL(tcf_exts_destroy); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | |
| Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 2103 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
| Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2104 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
| 2105 | struct netlink_ext_ack *extack) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | #ifdef CONFIG_NET_CLS_ACT |
| 2108 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | struct tc_action *act; |
| Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 2110 | size_t attr_size = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2112 | if (exts->police && tb[exts->police]) { |
| Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2113 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 2114 | rate_tlv, "police", ovr, |
| Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2115 | TCA_ACT_BIND, true, extack); |
| Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 2116 | if (IS_ERR(act)) |
| 2117 | return PTR_ERR(act); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2119 | act->type = exts->type = TCA_OLD_COMPAT; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2120 | exts->actions[0] = act; |
| 2121 | exts->nr_actions = 1; |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2122 | } else if (exts->action && tb[exts->action]) { |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2123 | int err; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2124 | |
| Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 2125 | err = tcf_action_init(net, tp, tb[exts->action], |
| 2126 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2127 | exts->actions, &attr_size, true, |
| Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 2128 | extack); |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2129 | if (err < 0) |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2130 | return err; |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2131 | exts->nr_actions = err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | } |
| Cong Wang | e4b95c4 | 2017-11-06 13:47:19 -0800 | [diff] [blame] | 2133 | exts->net = net; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | #else |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2136 | if ((exts->action && tb[exts->action]) || |
| Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2137 | (exts->police && tb[exts->police])) { |
| 2138 | NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | return -EOPNOTSUPP; |
| Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 2140 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | #endif |
| 2142 | |
| 2143 | return 0; |
| 2144 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2145 | EXPORT_SYMBOL(tcf_exts_validate); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | |
| Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2147 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | { |
| 2149 | #ifdef CONFIG_NET_CLS_ACT |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2150 | struct tcf_exts old = *dst; |
| 2151 | |
| Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 2152 | *dst = *src; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2153 | tcf_exts_destroy(&old); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | #endif |
| 2155 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2156 | EXPORT_SYMBOL(tcf_exts_change); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2158 | #ifdef CONFIG_NET_CLS_ACT |
| 2159 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 2160 | { |
| 2161 | if (exts->nr_actions == 0) |
| 2162 | return NULL; |
| 2163 | else |
| 2164 | return exts->actions[0]; |
| 2165 | } |
| 2166 | #endif |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2167 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2168 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | { |
| 2170 | #ifdef CONFIG_NET_CLS_ACT |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2171 | struct nlattr *nest; |
| 2172 | |
| Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 2173 | if (exts->action && tcf_exts_has_actions(exts)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | /* |
| 2175 | * again for backward compatible mode - we want |
| 2176 | * to work with both old and new modes of entering |
| 2177 | * tc data even if iproute2 was newer - jhs |
| 2178 | */ |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2179 | if (exts->type != TCA_OLD_COMPAT) { |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2180 | nest = nla_nest_start(skb, exts->action); |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2181 | if (nest == NULL) |
| 2182 | goto nla_put_failure; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 2183 | |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 2184 | if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2185 | goto nla_put_failure; |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2186 | nla_nest_end(skb, nest); |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2187 | } else if (exts->police) { |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2188 | struct tc_action *act = tcf_exts_first_act(exts); |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2189 | nest = nla_nest_start(skb, exts->police); |
| Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 2190 | if (nest == NULL || !act) |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2191 | goto nla_put_failure; |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2192 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 2193 | goto nla_put_failure; |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 2194 | nla_nest_end(skb, nest); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
| 2196 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | return 0; |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2198 | |
| 2199 | nla_put_failure: |
| 2200 | nla_nest_cancel(skb, nest); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | return -1; |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 2202 | #else |
| 2203 | return 0; |
| 2204 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2206 | EXPORT_SYMBOL(tcf_exts_dump); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2208 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 2209 | 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] | 2210 | { |
| 2211 | #ifdef CONFIG_NET_CLS_ACT |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2212 | struct tc_action *a = tcf_exts_first_act(exts); |
| Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 2213 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 2214 | return -1; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | #endif |
| 2216 | return 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 2218 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2220 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
| 2221 | enum tc_setup_type type, |
| 2222 | void *type_data, bool err_stop) |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2223 | { |
| 2224 | int ok_count = 0; |
| 2225 | #ifdef CONFIG_NET_CLS_ACT |
| 2226 | const struct tc_action *a; |
| 2227 | struct net_device *dev; |
| Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2228 | int i, ret; |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2229 | |
| 2230 | if (!tcf_exts_has_actions(exts)) |
| 2231 | return 0; |
| 2232 | |
| Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 2233 | for (i = 0; i < exts->nr_actions; i++) { |
| 2234 | a = exts->actions[i]; |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2235 | if (!a->ops->get_dev) |
| 2236 | continue; |
| 2237 | dev = a->ops->get_dev(a); |
| Jiri Pirko | 7612fb0 | 2017-11-01 11:47:40 +0100 | [diff] [blame] | 2238 | if (!dev) |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2239 | continue; |
| 2240 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); |
| Vlad Buslov | 84a75b3 | 2018-08-10 20:51:52 +0300 | [diff] [blame] | 2241 | a->ops->put_dev(dev); |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2242 | if (ret < 0) |
| 2243 | return ret; |
| 2244 | ok_count += ret; |
| 2245 | } |
| 2246 | #endif |
| 2247 | return ok_count; |
| 2248 | } |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2249 | |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2250 | int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, |
| 2251 | enum tc_setup_type type, void *type_data, bool err_stop) |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2252 | { |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2253 | int ok_count; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2254 | int ret; |
| 2255 | |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2256 | ret = tcf_block_cb_call(block, type, type_data, err_stop); |
| 2257 | if (ret < 0) |
| 2258 | return ret; |
| 2259 | ok_count = ret; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2260 | |
| Or Gerlitz | f8f4bef | 2018-05-23 19:24:48 +0300 | [diff] [blame] | 2261 | if (!exts || ok_count) |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 2262 | return ok_count; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 2263 | ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); |
| 2264 | if (ret < 0) |
| 2265 | return ret; |
| 2266 | ok_count += ret; |
| 2267 | |
| 2268 | return ok_count; |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 2269 | } |
| 2270 | EXPORT_SYMBOL(tc_setup_cb_call); |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 2271 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2272 | static __net_init int tcf_net_init(struct net *net) |
| 2273 | { |
| 2274 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2275 | |
| 2276 | idr_init(&tn->idr); |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | static void __net_exit tcf_net_exit(struct net *net) |
| 2281 | { |
| 2282 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 2283 | |
| 2284 | idr_destroy(&tn->idr); |
| 2285 | } |
| 2286 | |
| 2287 | static struct pernet_operations tcf_net_ops = { |
| 2288 | .init = tcf_net_init, |
| 2289 | .exit = tcf_net_exit, |
| 2290 | .id = &tcf_net_id, |
| 2291 | .size = sizeof(struct tcf_net), |
| 2292 | }; |
| 2293 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | static int __init tc_filter_init(void) |
| 2295 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2296 | int err; |
| 2297 | |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 2298 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 2299 | if (!tc_filter_wq) |
| 2300 | return -ENOMEM; |
| 2301 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2302 | err = register_pernet_subsys(&tcf_net_ops); |
| 2303 | if (err) |
| 2304 | goto err_register_pernet_subsys; |
| 2305 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 2306 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0); |
| 2307 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0); |
| 2308 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, |
| Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 2309 | tc_dump_tfilter, 0); |
| Jiri Pirko | 32a4f5e | 2018-07-23 09:23:06 +0200 | [diff] [blame] | 2310 | rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); |
| 2311 | rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); |
| 2312 | rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, |
| 2313 | tc_dump_chain, 0); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | return 0; |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 2316 | |
| 2317 | err_register_pernet_subsys: |
| 2318 | destroy_workqueue(tc_filter_wq); |
| 2319 | return err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | subsys_initcall(tc_filter_init); |