| 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; |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 207 | INIT_LIST_HEAD(&chain->filter_chain_list); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 208 | list_add_tail(&chain->list, &block->chain_list); |
| 209 | chain->block = block; |
| 210 | chain->index = chain_index; |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 211 | chain->refcnt = 1; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 212 | return chain; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 215 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, |
| 216 | struct tcf_proto *tp_head) |
| 217 | { |
| 218 | if (item->chain_head_change) |
| 219 | item->chain_head_change(tp_head, item->chain_head_change_priv); |
| 220 | } |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 221 | static void tcf_chain_head_change(struct tcf_chain *chain, |
| 222 | struct tcf_proto *tp_head) |
| 223 | { |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 224 | struct tcf_filter_chain_list_item *item; |
| 225 | |
| 226 | list_for_each_entry(item, &chain->filter_chain_list, list) |
| 227 | tcf_chain_head_change_item(item, tp_head); |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 230 | static void tcf_chain_flush(struct tcf_chain *chain) |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 231 | { |
| Roman Kapl | d7aa04a | 2017-11-20 22:21:13 +0100 | [diff] [blame] | 232 | struct tcf_proto *tp = rtnl_dereference(chain->filter_chain); |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 233 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 234 | tcf_chain_head_change(chain, NULL); |
| Roman Kapl | d7aa04a | 2017-11-20 22:21:13 +0100 | [diff] [blame] | 235 | while (tp) { |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 236 | RCU_INIT_POINTER(chain->filter_chain, tp->next); |
| Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 237 | tcf_proto_destroy(tp, NULL); |
| Roman Kapl | d7aa04a | 2017-11-20 22:21:13 +0100 | [diff] [blame] | 238 | tp = rtnl_dereference(chain->filter_chain); |
| 239 | tcf_chain_put(chain); |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 240 | } |
| Jiri Pirko | f93e1cd | 2017-05-20 15:01:32 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void tcf_chain_destroy(struct tcf_chain *chain) |
| 244 | { |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 245 | struct tcf_block *block = chain->block; |
| 246 | |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 247 | list_del(&chain->list); |
| 248 | kfree(chain); |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 249 | if (list_empty(&block->chain_list)) |
| 250 | kfree(block); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 251 | } |
| Jiri Pirko | 744a4cf | 2017-08-22 22:46:49 +0200 | [diff] [blame] | 252 | |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 253 | static void tcf_chain_hold(struct tcf_chain *chain) |
| 254 | { |
| 255 | ++chain->refcnt; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| WANG Cong | 367a8ce | 2017-05-23 09:42:37 -0700 | [diff] [blame] | 258 | struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
| 259 | bool create) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 260 | { |
| 261 | struct tcf_chain *chain; |
| 262 | |
| 263 | list_for_each_entry(chain, &block->chain_list, list) { |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 264 | if (chain->index == chain_index) { |
| 265 | tcf_chain_hold(chain); |
| 266 | return chain; |
| 267 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 268 | } |
| Jiri Pirko | 8053238 | 2017-09-06 13:14:19 +0200 | [diff] [blame] | 269 | |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 270 | return create ? tcf_chain_create(block, chain_index) : NULL; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 271 | } |
| 272 | EXPORT_SYMBOL(tcf_chain_get); |
| 273 | |
| 274 | void tcf_chain_put(struct tcf_chain *chain) |
| 275 | { |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 276 | if (--chain->refcnt == 0) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 277 | tcf_chain_destroy(chain); |
| 278 | } |
| 279 | EXPORT_SYMBOL(tcf_chain_put); |
| 280 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 281 | static bool tcf_block_offload_in_use(struct tcf_block *block) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 282 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 283 | return block->offloadcnt; |
| 284 | } |
| 285 | |
| 286 | static int tcf_block_offload_cmd(struct tcf_block *block, |
| 287 | struct net_device *dev, |
| 288 | struct tcf_block_ext_info *ei, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 289 | enum tc_block_command command, |
| 290 | struct netlink_ext_ack *extack) |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 291 | { |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 292 | struct tc_block_offload bo = {}; |
| 293 | |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 294 | bo.command = command; |
| 295 | bo.binder_type = ei->binder_type; |
| 296 | bo.block = block; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 297 | bo.extack = extack; |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 298 | return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 299 | } |
| 300 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 301 | 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] | 302 | struct tcf_block_ext_info *ei, |
| 303 | struct netlink_ext_ack *extack) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 304 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 305 | struct net_device *dev = q->dev_queue->dev; |
| 306 | int err; |
| 307 | |
| 308 | if (!dev->netdev_ops->ndo_setup_tc) |
| 309 | goto no_offload_dev_inc; |
| 310 | |
| 311 | /* If tc offload feature is disabled and the block we try to bind |
| 312 | * to already has some offloaded filters, forbid to bind. |
| 313 | */ |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 314 | if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) { |
| 315 | 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] | 316 | return -EOPNOTSUPP; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 317 | } |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 318 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 319 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 320 | if (err == -EOPNOTSUPP) |
| 321 | goto no_offload_dev_inc; |
| 322 | return err; |
| 323 | |
| 324 | no_offload_dev_inc: |
| 325 | if (tcf_block_offload_in_use(block)) |
| 326 | return -EOPNOTSUPP; |
| 327 | block->nooffloaddevcnt++; |
| 328 | return 0; |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, |
| 332 | struct tcf_block_ext_info *ei) |
| 333 | { |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 334 | struct net_device *dev = q->dev_queue->dev; |
| 335 | int err; |
| 336 | |
| 337 | if (!dev->netdev_ops->ndo_setup_tc) |
| 338 | goto no_offload_dev_dec; |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 339 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 340 | if (err == -EOPNOTSUPP) |
| 341 | goto no_offload_dev_dec; |
| 342 | return; |
| 343 | |
| 344 | no_offload_dev_dec: |
| 345 | WARN_ON(block->nooffloaddevcnt-- == 0); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 348 | static int |
| 349 | tcf_chain_head_change_cb_add(struct tcf_chain *chain, |
| 350 | struct tcf_block_ext_info *ei, |
| 351 | struct netlink_ext_ack *extack) |
| 352 | { |
| 353 | struct tcf_filter_chain_list_item *item; |
| 354 | |
| 355 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 356 | if (!item) { |
| 357 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | item->chain_head_change = ei->chain_head_change; |
| 361 | item->chain_head_change_priv = ei->chain_head_change_priv; |
| 362 | if (chain->filter_chain) |
| 363 | tcf_chain_head_change_item(item, chain->filter_chain); |
| 364 | list_add(&item->list, &chain->filter_chain_list); |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static void |
| 369 | tcf_chain_head_change_cb_del(struct tcf_chain *chain, |
| 370 | struct tcf_block_ext_info *ei) |
| 371 | { |
| 372 | struct tcf_filter_chain_list_item *item; |
| 373 | |
| 374 | list_for_each_entry(item, &chain->filter_chain_list, list) { |
| 375 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) || |
| 376 | (item->chain_head_change == ei->chain_head_change && |
| 377 | item->chain_head_change_priv == ei->chain_head_change_priv)) { |
| 378 | tcf_chain_head_change_item(item, NULL); |
| 379 | list_del(&item->list); |
| 380 | kfree(item); |
| 381 | return; |
| 382 | } |
| 383 | } |
| 384 | WARN_ON(1); |
| 385 | } |
| 386 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 387 | struct tcf_net { |
| 388 | struct idr idr; |
| 389 | }; |
| 390 | |
| 391 | static unsigned int tcf_net_id; |
| 392 | |
| 393 | static int tcf_block_insert(struct tcf_block *block, struct net *net, |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 394 | struct netlink_ext_ack *extack) |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 395 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 396 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 397 | |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 398 | return idr_alloc_u32(&tn->idr, block, &block->index, block->index, |
| 399 | GFP_KERNEL); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 400 | } |
| 401 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 402 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 403 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 404 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 405 | |
| Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 406 | idr_remove(&tn->idr, block->index); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | 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] | 410 | u32 block_index, |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 411 | struct netlink_ext_ack *extack) |
| 412 | { |
| 413 | struct tcf_block *block; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 414 | struct tcf_chain *chain; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 415 | int err; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 416 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 417 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 418 | if (!block) { |
| 419 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 420 | return ERR_PTR(-ENOMEM); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 421 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 422 | INIT_LIST_HEAD(&block->chain_list); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 423 | INIT_LIST_HEAD(&block->cb_list); |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 424 | INIT_LIST_HEAD(&block->owner_list); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 425 | |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 426 | /* Create chain 0 by default, it has to be always present. */ |
| 427 | chain = tcf_chain_create(block, 0); |
| 428 | if (!chain) { |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 429 | NL_SET_ERR_MSG(extack, "Failed to create new tcf chain"); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 430 | err = -ENOMEM; |
| 431 | goto err_chain_create; |
| 432 | } |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 433 | block->refcnt = 1; |
| 434 | block->net = net; |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 435 | block->index = block_index; |
| 436 | |
| 437 | /* Don't store q pointer for blocks which are shared */ |
| 438 | if (!tcf_block_shared(block)) |
| 439 | block->q = q; |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 440 | return block; |
| 441 | |
| 442 | err_chain_create: |
| 443 | kfree(block); |
| 444 | return ERR_PTR(err); |
| 445 | } |
| 446 | |
| 447 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) |
| 448 | { |
| 449 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 450 | |
| Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 451 | return idr_find(&tn->idr, block_index); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 452 | } |
| 453 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 454 | /* Find tcf block. |
| 455 | * Set q, parent, cl when appropriate. |
| 456 | */ |
| 457 | |
| 458 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, |
| 459 | u32 *parent, unsigned long *cl, |
| 460 | int ifindex, u32 block_index, |
| 461 | struct netlink_ext_ack *extack) |
| 462 | { |
| 463 | struct tcf_block *block; |
| 464 | |
| 465 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 466 | block = tcf_block_lookup(net, block_index); |
| 467 | if (!block) { |
| 468 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); |
| 469 | return ERR_PTR(-EINVAL); |
| 470 | } |
| 471 | } else { |
| 472 | const struct Qdisc_class_ops *cops; |
| 473 | struct net_device *dev; |
| 474 | |
| 475 | /* Find link */ |
| 476 | dev = __dev_get_by_index(net, ifindex); |
| 477 | if (!dev) |
| 478 | return ERR_PTR(-ENODEV); |
| 479 | |
| 480 | /* Find qdisc */ |
| 481 | if (!*parent) { |
| 482 | *q = dev->qdisc; |
| 483 | *parent = (*q)->handle; |
| 484 | } else { |
| 485 | *q = qdisc_lookup(dev, TC_H_MAJ(*parent)); |
| 486 | if (!*q) { |
| 487 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); |
| 488 | return ERR_PTR(-EINVAL); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* Is it classful? */ |
| 493 | cops = (*q)->ops->cl_ops; |
| 494 | if (!cops) { |
| 495 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); |
| 496 | return ERR_PTR(-EINVAL); |
| 497 | } |
| 498 | |
| 499 | if (!cops->tcf_block) { |
| 500 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); |
| 501 | return ERR_PTR(-EOPNOTSUPP); |
| 502 | } |
| 503 | |
| 504 | /* Do we search for filter, attached to class? */ |
| 505 | if (TC_H_MIN(*parent)) { |
| 506 | *cl = cops->find(*q, *parent); |
| 507 | if (*cl == 0) { |
| 508 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); |
| 509 | return ERR_PTR(-ENOENT); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* And the last stroke */ |
| 514 | block = cops->tcf_block(*q, *cl, extack); |
| 515 | if (!block) |
| 516 | return ERR_PTR(-EINVAL); |
| 517 | if (tcf_block_shared(block)) { |
| 518 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); |
| 519 | return ERR_PTR(-EOPNOTSUPP); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return block; |
| 524 | } |
| 525 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 526 | static struct tcf_chain *tcf_block_chain_zero(struct tcf_block *block) |
| 527 | { |
| 528 | return list_first_entry(&block->chain_list, struct tcf_chain, list); |
| 529 | } |
| 530 | |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 531 | struct tcf_block_owner_item { |
| 532 | struct list_head list; |
| 533 | struct Qdisc *q; |
| 534 | enum tcf_block_binder_type binder_type; |
| 535 | }; |
| 536 | |
| 537 | static void |
| 538 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, |
| 539 | struct Qdisc *q, |
| 540 | enum tcf_block_binder_type binder_type) |
| 541 | { |
| 542 | if (block->keep_dst && |
| 543 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS && |
| 544 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) |
| 545 | netif_keep_dst(qdisc_dev(q)); |
| 546 | } |
| 547 | |
| 548 | void tcf_block_netif_keep_dst(struct tcf_block *block) |
| 549 | { |
| 550 | struct tcf_block_owner_item *item; |
| 551 | |
| 552 | block->keep_dst = true; |
| 553 | list_for_each_entry(item, &block->owner_list, list) |
| 554 | tcf_block_owner_netif_keep_dst(block, item->q, |
| 555 | item->binder_type); |
| 556 | } |
| 557 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); |
| 558 | |
| 559 | static int tcf_block_owner_add(struct tcf_block *block, |
| 560 | struct Qdisc *q, |
| 561 | enum tcf_block_binder_type binder_type) |
| 562 | { |
| 563 | struct tcf_block_owner_item *item; |
| 564 | |
| 565 | item = kmalloc(sizeof(*item), GFP_KERNEL); |
| 566 | if (!item) |
| 567 | return -ENOMEM; |
| 568 | item->q = q; |
| 569 | item->binder_type = binder_type; |
| 570 | list_add(&item->list, &block->owner_list); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | static void tcf_block_owner_del(struct tcf_block *block, |
| 575 | struct Qdisc *q, |
| 576 | enum tcf_block_binder_type binder_type) |
| 577 | { |
| 578 | struct tcf_block_owner_item *item; |
| 579 | |
| 580 | list_for_each_entry(item, &block->owner_list, list) { |
| 581 | if (item->q == q && item->binder_type == binder_type) { |
| 582 | list_del(&item->list); |
| 583 | kfree(item); |
| 584 | return; |
| 585 | } |
| 586 | } |
| 587 | WARN_ON(1); |
| 588 | } |
| 589 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 590 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
| 591 | struct tcf_block_ext_info *ei, |
| 592 | struct netlink_ext_ack *extack) |
| 593 | { |
| 594 | struct net *net = qdisc_net(q); |
| 595 | struct tcf_block *block = NULL; |
| 596 | bool created = false; |
| 597 | int err; |
| 598 | |
| 599 | if (ei->block_index) { |
| 600 | /* block_index not 0 means the shared block is requested */ |
| 601 | block = tcf_block_lookup(net, ei->block_index); |
| 602 | if (block) |
| 603 | block->refcnt++; |
| 604 | } |
| 605 | |
| 606 | if (!block) { |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 607 | block = tcf_block_create(net, q, ei->block_index, extack); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 608 | if (IS_ERR(block)) |
| 609 | return PTR_ERR(block); |
| 610 | created = true; |
| Jiri Pirko | bb047dd | 2018-02-13 12:00:16 +0100 | [diff] [blame] | 611 | if (tcf_block_shared(block)) { |
| 612 | err = tcf_block_insert(block, net, extack); |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 613 | if (err) |
| 614 | goto err_block_insert; |
| 615 | } |
| 616 | } |
| 617 | |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 618 | err = tcf_block_owner_add(block, q, ei->binder_type); |
| 619 | if (err) |
| 620 | goto err_block_owner_add; |
| 621 | |
| 622 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); |
| 623 | |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 624 | err = tcf_chain_head_change_cb_add(tcf_block_chain_zero(block), |
| 625 | ei, extack); |
| 626 | if (err) |
| 627 | goto err_chain_head_change_cb_add; |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 628 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 629 | err = tcf_block_offload_bind(block, q, ei, extack); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 630 | if (err) |
| 631 | goto err_block_offload_bind; |
| 632 | |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 633 | *p_block = block; |
| 634 | return 0; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 635 | |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 636 | err_block_offload_bind: |
| 637 | tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei); |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 638 | err_chain_head_change_cb_add: |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 639 | tcf_block_owner_del(block, q, ei->binder_type); |
| 640 | err_block_owner_add: |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 641 | if (created) { |
| 642 | if (tcf_block_shared(block)) |
| 643 | tcf_block_remove(block, net); |
| 644 | err_block_insert: |
| 645 | kfree(tcf_block_chain_zero(block)); |
| 646 | kfree(block); |
| 647 | } else { |
| 648 | block->refcnt--; |
| 649 | } |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 650 | return err; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 651 | } |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 652 | EXPORT_SYMBOL(tcf_block_get_ext); |
| 653 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 654 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
| 655 | { |
| 656 | struct tcf_proto __rcu **p_filter_chain = priv; |
| 657 | |
| 658 | rcu_assign_pointer(*p_filter_chain, tp_head); |
| 659 | } |
| 660 | |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 661 | int tcf_block_get(struct tcf_block **p_block, |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 662 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
| 663 | struct netlink_ext_ack *extack) |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 664 | { |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 665 | struct tcf_block_ext_info ei = { |
| 666 | .chain_head_change = tcf_chain_head_change_dflt, |
| 667 | .chain_head_change_priv = p_filter_chain, |
| 668 | }; |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 669 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 670 | WARN_ON(!p_filter_chain); |
| Alexander Aring | 8d1a77f | 2017-12-20 12:35:19 -0500 | [diff] [blame] | 671 | return tcf_block_get_ext(p_block, q, &ei, extack); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 672 | } |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 673 | EXPORT_SYMBOL(tcf_block_get); |
| 674 | |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 675 | /* 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] | 676 | * actions should be all removed after flushing. |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 677 | */ |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 678 | 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] | 679 | struct tcf_block_ext_info *ei) |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 680 | { |
| Cong Wang | efbf789 | 2017-12-04 10:48:18 -0800 | [diff] [blame] | 681 | struct tcf_chain *chain, *tmp; |
| Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 682 | |
| David S. Miller | c30abd5 | 2017-12-16 22:11:55 -0500 | [diff] [blame] | 683 | if (!block) |
| 684 | return; |
| Jiri Pirko | a9b1944 | 2018-01-17 11:46:45 +0100 | [diff] [blame] | 685 | tcf_chain_head_change_cb_del(tcf_block_chain_zero(block), ei); |
| Jiri Pirko | f36fe1c | 2018-01-17 11:46:48 +0100 | [diff] [blame] | 686 | tcf_block_owner_del(block, q, ei->binder_type); |
| Roman Kapl | a60b3f5 | 2017-11-24 12:27:58 +0100 | [diff] [blame] | 687 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 688 | if (--block->refcnt == 0) { |
| 689 | if (tcf_block_shared(block)) |
| 690 | tcf_block_remove(block, block->net); |
| 691 | |
| 692 | /* Hold a refcnt for all chains, so that they don't disappear |
| 693 | * while we are iterating. |
| 694 | */ |
| 695 | list_for_each_entry(chain, &block->chain_list, list) |
| 696 | tcf_chain_hold(chain); |
| 697 | |
| 698 | list_for_each_entry(chain, &block->chain_list, list) |
| 699 | tcf_chain_flush(chain); |
| 700 | } |
| Cong Wang | 1697c4b | 2017-09-11 16:33:32 -0700 | [diff] [blame] | 701 | |
| Jiri Pirko | 4bb1b11 | 2017-11-02 15:07:01 +0100 | [diff] [blame] | 702 | tcf_block_offload_unbind(block, q, ei); |
| 703 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 704 | if (block->refcnt == 0) { |
| 705 | /* At this point, all the chains should have refcnt >= 1. */ |
| 706 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
| 707 | tcf_chain_put(chain); |
| Jiri Pirko | df45bf8 | 2017-12-08 19:27:27 +0100 | [diff] [blame] | 708 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 709 | /* Finally, put chain 0 and allow block to be freed. */ |
| 710 | tcf_chain_put(tcf_block_chain_zero(block)); |
| 711 | } |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 712 | } |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 713 | EXPORT_SYMBOL(tcf_block_put_ext); |
| 714 | |
| 715 | void tcf_block_put(struct tcf_block *block) |
| 716 | { |
| 717 | struct tcf_block_ext_info ei = {0, }; |
| 718 | |
| Jiri Pirko | 4853f12 | 2017-12-21 13:13:59 +0100 | [diff] [blame] | 719 | if (!block) |
| 720 | return; |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 721 | tcf_block_put_ext(block, block->q, &ei); |
| Jiri Pirko | 8c4083b | 2017-10-19 15:50:29 +0200 | [diff] [blame] | 722 | } |
| David S. Miller | e1ea2f9 | 2017-10-30 14:10:01 +0900 | [diff] [blame] | 723 | |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 724 | EXPORT_SYMBOL(tcf_block_put); |
| Jiri Pirko | cf1facd | 2017-02-09 14:38:56 +0100 | [diff] [blame] | 725 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 726 | struct tcf_block_cb { |
| 727 | struct list_head list; |
| 728 | tc_setup_cb_t *cb; |
| 729 | void *cb_ident; |
| 730 | void *cb_priv; |
| 731 | unsigned int refcnt; |
| 732 | }; |
| 733 | |
| 734 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) |
| 735 | { |
| 736 | return block_cb->cb_priv; |
| 737 | } |
| 738 | EXPORT_SYMBOL(tcf_block_cb_priv); |
| 739 | |
| 740 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, |
| 741 | tc_setup_cb_t *cb, void *cb_ident) |
| 742 | { struct tcf_block_cb *block_cb; |
| 743 | |
| 744 | list_for_each_entry(block_cb, &block->cb_list, list) |
| 745 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) |
| 746 | return block_cb; |
| 747 | return NULL; |
| 748 | } |
| 749 | EXPORT_SYMBOL(tcf_block_cb_lookup); |
| 750 | |
| 751 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) |
| 752 | { |
| 753 | block_cb->refcnt++; |
| 754 | } |
| 755 | EXPORT_SYMBOL(tcf_block_cb_incref); |
| 756 | |
| 757 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) |
| 758 | { |
| 759 | return --block_cb->refcnt; |
| 760 | } |
| 761 | EXPORT_SYMBOL(tcf_block_cb_decref); |
| 762 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 763 | static int |
| 764 | tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, |
| 765 | void *cb_priv, bool add, bool offload_in_use, |
| 766 | struct netlink_ext_ack *extack) |
| 767 | { |
| 768 | struct tcf_chain *chain; |
| 769 | struct tcf_proto *tp; |
| 770 | int err; |
| 771 | |
| 772 | list_for_each_entry(chain, &block->chain_list, list) { |
| 773 | for (tp = rtnl_dereference(chain->filter_chain); tp; |
| 774 | tp = rtnl_dereference(tp->next)) { |
| 775 | if (tp->ops->reoffload) { |
| 776 | err = tp->ops->reoffload(tp, add, cb, cb_priv, |
| 777 | extack); |
| 778 | if (err && add) |
| 779 | goto err_playback_remove; |
| 780 | } else if (add && offload_in_use) { |
| 781 | err = -EOPNOTSUPP; |
| 782 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); |
| 783 | goto err_playback_remove; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return 0; |
| 789 | |
| 790 | err_playback_remove: |
| 791 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
| 792 | extack); |
| 793 | return err; |
| 794 | } |
| 795 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 796 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, |
| 797 | tc_setup_cb_t *cb, void *cb_ident, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 798 | void *cb_priv, |
| 799 | struct netlink_ext_ack *extack) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 800 | { |
| 801 | struct tcf_block_cb *block_cb; |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 802 | int err; |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 803 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 804 | /* Replay any already present rules */ |
| 805 | err = tcf_block_playback_offloads(block, cb, cb_priv, true, |
| 806 | tcf_block_offload_in_use(block), |
| 807 | extack); |
| 808 | if (err) |
| 809 | return ERR_PTR(err); |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 810 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 811 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); |
| 812 | if (!block_cb) |
| Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 813 | return ERR_PTR(-ENOMEM); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 814 | block_cb->cb = cb; |
| 815 | block_cb->cb_ident = cb_ident; |
| 816 | block_cb->cb_priv = cb_priv; |
| 817 | list_add(&block_cb->list, &block->cb_list); |
| 818 | return block_cb; |
| 819 | } |
| 820 | EXPORT_SYMBOL(__tcf_block_cb_register); |
| 821 | |
| 822 | int tcf_block_cb_register(struct tcf_block *block, |
| 823 | tc_setup_cb_t *cb, void *cb_ident, |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 824 | void *cb_priv, struct netlink_ext_ack *extack) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 825 | { |
| 826 | struct tcf_block_cb *block_cb; |
| 827 | |
| John Hurley | 60513bd | 2018-06-25 14:30:04 -0700 | [diff] [blame] | 828 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv, |
| 829 | extack); |
| Gustavo A. R. Silva | baa2d2b | 2018-07-18 23:14:17 -0500 | [diff] [blame] | 830 | return PTR_ERR_OR_ZERO(block_cb); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 831 | } |
| 832 | EXPORT_SYMBOL(tcf_block_cb_register); |
| 833 | |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 834 | void __tcf_block_cb_unregister(struct tcf_block *block, |
| 835 | struct tcf_block_cb *block_cb) |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 836 | { |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 837 | tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv, |
| 838 | false, tcf_block_offload_in_use(block), |
| 839 | NULL); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 840 | list_del(&block_cb->list); |
| 841 | kfree(block_cb); |
| 842 | } |
| 843 | EXPORT_SYMBOL(__tcf_block_cb_unregister); |
| 844 | |
| 845 | void tcf_block_cb_unregister(struct tcf_block *block, |
| 846 | tc_setup_cb_t *cb, void *cb_ident) |
| 847 | { |
| 848 | struct tcf_block_cb *block_cb; |
| 849 | |
| 850 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); |
| 851 | if (!block_cb) |
| 852 | return; |
| John Hurley | 3263674 | 2018-06-25 14:30:10 -0700 | [diff] [blame] | 853 | __tcf_block_cb_unregister(block, block_cb); |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 854 | } |
| 855 | EXPORT_SYMBOL(tcf_block_cb_unregister); |
| 856 | |
| 857 | static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type, |
| 858 | void *type_data, bool err_stop) |
| 859 | { |
| 860 | struct tcf_block_cb *block_cb; |
| 861 | int ok_count = 0; |
| 862 | int err; |
| 863 | |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 864 | /* Make sure all netdevs sharing this block are offload-capable. */ |
| 865 | if (block->nooffloaddevcnt && err_stop) |
| 866 | return -EOPNOTSUPP; |
| 867 | |
| Jiri Pirko | acb6744 | 2017-10-19 15:50:31 +0200 | [diff] [blame] | 868 | list_for_each_entry(block_cb, &block->cb_list, list) { |
| 869 | err = block_cb->cb(type, type_data, block_cb->cb_priv); |
| 870 | if (err) { |
| 871 | if (err_stop) |
| 872 | return err; |
| 873 | } else { |
| 874 | ok_count++; |
| 875 | } |
| 876 | } |
| 877 | return ok_count; |
| 878 | } |
| 879 | |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 880 | /* Main classifier routine: scans classifier chain attached |
| 881 | * to this qdisc, (optionally) tests for protocol and asks |
| 882 | * specific classifiers. |
| 883 | */ |
| 884 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 885 | struct tcf_result *res, bool compat_mode) |
| 886 | { |
| 887 | __be16 protocol = tc_skb_protocol(skb); |
| 888 | #ifdef CONFIG_NET_CLS_ACT |
| 889 | const int max_reclassify_loop = 4; |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 890 | const struct tcf_proto *orig_tp = tp; |
| 891 | const struct tcf_proto *first_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 892 | int limit = 0; |
| 893 | |
| 894 | reclassify: |
| 895 | #endif |
| 896 | for (; tp; tp = rcu_dereference_bh(tp->next)) { |
| 897 | int err; |
| 898 | |
| 899 | if (tp->protocol != protocol && |
| 900 | tp->protocol != htons(ETH_P_ALL)) |
| 901 | continue; |
| 902 | |
| 903 | err = tp->classify(skb, tp, res); |
| 904 | #ifdef CONFIG_NET_CLS_ACT |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 905 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 906 | first_tp = orig_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 907 | goto reset; |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 908 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 909 | first_tp = res->goto_tp; |
| Jiri Pirko | db50514 | 2017-05-17 11:08:03 +0200 | [diff] [blame] | 910 | goto reset; |
| 911 | } |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 912 | #endif |
| 913 | if (err >= 0) |
| 914 | return err; |
| 915 | } |
| 916 | |
| 917 | return TC_ACT_UNSPEC; /* signal: continue lookup */ |
| 918 | #ifdef CONFIG_NET_CLS_ACT |
| 919 | reset: |
| 920 | if (unlikely(limit++ >= max_reclassify_loop)) { |
| Jiri Pirko | 9d3aaff | 2018-01-17 11:46:47 +0100 | [diff] [blame] | 921 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
| 922 | tp->chain->block->index, |
| 923 | tp->prio & 0xffff, |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 924 | ntohs(tp->protocol)); |
| 925 | return TC_ACT_SHOT; |
| 926 | } |
| 927 | |
| Jiri Pirko | ee538dc | 2017-05-23 09:11:59 +0200 | [diff] [blame] | 928 | tp = first_tp; |
| Jiri Pirko | 87d8309 | 2017-05-17 11:07:54 +0200 | [diff] [blame] | 929 | protocol = tc_skb_protocol(skb); |
| 930 | goto reclassify; |
| 931 | #endif |
| 932 | } |
| 933 | EXPORT_SYMBOL(tcf_classify); |
| 934 | |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 935 | struct tcf_chain_info { |
| 936 | struct tcf_proto __rcu **pprev; |
| 937 | struct tcf_proto __rcu *next; |
| 938 | }; |
| 939 | |
| 940 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) |
| 941 | { |
| 942 | return rtnl_dereference(*chain_info->pprev); |
| 943 | } |
| 944 | |
| 945 | static void tcf_chain_tp_insert(struct tcf_chain *chain, |
| 946 | struct tcf_chain_info *chain_info, |
| 947 | struct tcf_proto *tp) |
| 948 | { |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 949 | if (*chain_info->pprev == chain->filter_chain) |
| 950 | tcf_chain_head_change(chain, tp); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 951 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
| 952 | rcu_assign_pointer(*chain_info->pprev, tp); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 953 | tcf_chain_hold(chain); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | static void tcf_chain_tp_remove(struct tcf_chain *chain, |
| 957 | struct tcf_chain_info *chain_info, |
| 958 | struct tcf_proto *tp) |
| 959 | { |
| 960 | struct tcf_proto *next = rtnl_dereference(chain_info->next); |
| 961 | |
| Jiri Pirko | c7eb7d7 | 2017-11-03 11:46:24 +0100 | [diff] [blame] | 962 | if (tp == chain->filter_chain) |
| 963 | tcf_chain_head_change(chain, next); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 964 | RCU_INIT_POINTER(*chain_info->pprev, next); |
| Cong Wang | e2ef754 | 2017-09-11 16:33:31 -0700 | [diff] [blame] | 965 | tcf_chain_put(chain); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
| 969 | struct tcf_chain_info *chain_info, |
| 970 | u32 protocol, u32 prio, |
| 971 | bool prio_allocate) |
| 972 | { |
| 973 | struct tcf_proto **pprev; |
| 974 | struct tcf_proto *tp; |
| 975 | |
| 976 | /* Check the chain for existence of proto-tcf with this priority */ |
| 977 | for (pprev = &chain->filter_chain; |
| 978 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { |
| 979 | if (tp->prio >= prio) { |
| 980 | if (tp->prio == prio) { |
| 981 | if (prio_allocate || |
| 982 | (tp->protocol != protocol && protocol)) |
| 983 | return ERR_PTR(-EINVAL); |
| 984 | } else { |
| 985 | tp = NULL; |
| 986 | } |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | chain_info->pprev = pprev; |
| 991 | chain_info->next = tp ? tp->next : NULL; |
| 992 | return tp; |
| 993 | } |
| 994 | |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 995 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 996 | struct tcf_proto *tp, struct tcf_block *block, |
| 997 | struct Qdisc *q, u32 parent, void *fh, |
| 998 | u32 portid, u32 seq, u16 flags, int event) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 999 | { |
| 1000 | struct tcmsg *tcm; |
| 1001 | struct nlmsghdr *nlh; |
| 1002 | unsigned char *b = skb_tail_pointer(skb); |
| 1003 | |
| 1004 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); |
| 1005 | if (!nlh) |
| 1006 | goto out_nlmsg_trim; |
| 1007 | tcm = nlmsg_data(nlh); |
| 1008 | tcm->tcm_family = AF_UNSPEC; |
| 1009 | tcm->tcm__pad1 = 0; |
| 1010 | tcm->tcm__pad2 = 0; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1011 | if (q) { |
| 1012 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
| 1013 | tcm->tcm_parent = parent; |
| 1014 | } else { |
| 1015 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; |
| 1016 | tcm->tcm_block_index = block->index; |
| 1017 | } |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1018 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
| 1019 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) |
| 1020 | goto nla_put_failure; |
| 1021 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) |
| 1022 | goto nla_put_failure; |
| 1023 | if (!fh) { |
| 1024 | tcm->tcm_handle = 0; |
| 1025 | } else { |
| 1026 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) |
| 1027 | goto nla_put_failure; |
| 1028 | } |
| 1029 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
| 1030 | return skb->len; |
| 1031 | |
| 1032 | out_nlmsg_trim: |
| 1033 | nla_put_failure: |
| 1034 | nlmsg_trim(skb, b); |
| 1035 | return -1; |
| 1036 | } |
| 1037 | |
| 1038 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, |
| 1039 | struct nlmsghdr *n, struct tcf_proto *tp, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1040 | struct tcf_block *block, struct Qdisc *q, |
| 1041 | u32 parent, void *fh, int event, bool unicast) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1042 | { |
| 1043 | struct sk_buff *skb; |
| 1044 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1045 | |
| 1046 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1047 | if (!skb) |
| 1048 | return -ENOBUFS; |
| 1049 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1050 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1051 | n->nlmsg_seq, n->nlmsg_flags, event) <= 0) { |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1052 | kfree_skb(skb); |
| 1053 | return -EINVAL; |
| 1054 | } |
| 1055 | |
| 1056 | if (unicast) |
| 1057 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1058 | |
| 1059 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1060 | n->nlmsg_flags & NLM_F_ECHO); |
| 1061 | } |
| 1062 | |
| 1063 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, |
| 1064 | struct nlmsghdr *n, struct tcf_proto *tp, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1065 | struct tcf_block *block, struct Qdisc *q, |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1066 | u32 parent, void *fh, bool unicast, bool *last, |
| 1067 | struct netlink_ext_ack *extack) |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1068 | { |
| 1069 | struct sk_buff *skb; |
| 1070 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; |
| 1071 | int err; |
| 1072 | |
| 1073 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
| 1074 | if (!skb) |
| 1075 | return -ENOBUFS; |
| 1076 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1077 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
| 1078 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1079 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1080 | kfree_skb(skb); |
| 1081 | return -EINVAL; |
| 1082 | } |
| 1083 | |
| Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 1084 | err = tp->ops->delete(tp, fh, last, extack); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1085 | if (err) { |
| 1086 | kfree_skb(skb); |
| 1087 | return err; |
| 1088 | } |
| 1089 | |
| 1090 | if (unicast) |
| 1091 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); |
| 1092 | |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1093 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
| 1094 | n->nlmsg_flags & NLM_F_ECHO); |
| 1095 | if (err < 0) |
| 1096 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); |
| 1097 | return err; |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1101 | struct tcf_block *block, struct Qdisc *q, |
| 1102 | u32 parent, struct nlmsghdr *n, |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1103 | struct tcf_chain *chain, int event) |
| 1104 | { |
| 1105 | struct tcf_proto *tp; |
| 1106 | |
| 1107 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1108 | tp; tp = rtnl_dereference(tp->next)) |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1109 | tfilter_notify(net, oskb, n, tp, block, |
| YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1110 | q, parent, NULL, event, false); |
| WANG Cong | 7120371 | 2017-08-07 15:26:50 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1113 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1114 | struct netlink_ext_ack *extack) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | { |
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1116 | struct net *net = sock_net(skb->sk); |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1117 | struct nlattr *tca[TCA_MAX + 1]; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | struct tcmsg *t; |
| 1119 | u32 protocol; |
| 1120 | u32 prio; |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1121 | bool prio_allocate; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | u32 parent; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1123 | u32 chain_index; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1124 | struct Qdisc *q = NULL; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1125 | struct tcf_chain_info chain_info; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1126 | struct tcf_chain *chain = NULL; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1127 | struct tcf_block *block; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | struct tcf_proto *tp; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | unsigned long cl; |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1130 | void *fh; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | int err; |
| Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1132 | int tp_created; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1134 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| Eric W. Biederman | dfc47ef | 2012-11-16 03:03:00 +0000 | [diff] [blame] | 1135 | return -EPERM; |
| Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1136 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | replay: |
| Daniel Borkmann | 628185c | 2016-12-21 18:04:11 +0100 | [diff] [blame] | 1138 | tp_created = 0; |
| 1139 | |
| David Ahern | c21ef3e | 2017-04-16 09:48:24 -0700 | [diff] [blame] | 1140 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| Hong zhi guo | de179c8 | 2013-03-25 17:36:33 +0000 | [diff] [blame] | 1141 | if (err < 0) |
| 1142 | return err; |
| 1143 | |
| David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1144 | t = nlmsg_data(n); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | protocol = TC_H_MIN(t->tcm_info); |
| 1146 | prio = TC_H_MAJ(t->tcm_info); |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1147 | prio_allocate = false; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | parent = t->tcm_parent; |
| 1149 | cl = 0; |
| 1150 | |
| 1151 | if (prio == 0) { |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1152 | /* If no priority is provided by the user, |
| 1153 | * we allocate one. |
| 1154 | */ |
| 1155 | if (n->nlmsg_flags & NLM_F_CREATE) { |
| 1156 | prio = TC_H_MAKE(0x80000000U, 0U); |
| 1157 | prio_allocate = true; |
| 1158 | } else { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1159 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | return -ENOENT; |
| Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1161 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /* Find head of filter chain. */ |
| 1165 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1166 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1167 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1168 | if (IS_ERR(block)) { |
| 1169 | err = PTR_ERR(block); |
| 1170 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1171 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1172 | |
| 1173 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1174 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1175 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1176 | err = -EINVAL; |
| 1177 | goto errout; |
| 1178 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1179 | chain = tcf_chain_get(block, chain_index, true); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1180 | if (!chain) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1181 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1182 | err = -ENOMEM; |
| Daniel Borkmann | ea7f827 | 2016-06-10 23:10:22 +0200 | [diff] [blame] | 1183 | goto errout; |
| 1184 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1186 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1187 | prio, prio_allocate); |
| 1188 | if (IS_ERR(tp)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1189 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1190 | err = PTR_ERR(tp); |
| 1191 | goto errout; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | if (tp == NULL) { |
| 1195 | /* Proto-tcf does not exist, create new one */ |
| 1196 | |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1197 | if (tca[TCA_KIND] == NULL || !protocol) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1198 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1199 | err = -EINVAL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1201 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1203 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1204 | 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] | 1205 | err = -ENOENT; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1207 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | |
| Jiri Pirko | 9d36d9e | 2017-05-17 11:07:57 +0200 | [diff] [blame] | 1209 | if (prio_allocate) |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1210 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1212 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1213 | protocol, prio, chain, extack); |
| Jiri Pirko | 33a4892 | 2017-02-09 14:38:57 +0100 | [diff] [blame] | 1214 | if (IS_ERR(tp)) { |
| 1215 | err = PTR_ERR(tp); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | goto errout; |
| 1217 | } |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1218 | tp_created = 1; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1219 | } 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] | 1220 | 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] | 1221 | err = -EINVAL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1223 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
| 1225 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1226 | |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1227 | if (!fh) { |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1228 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
| Alexander Aring | c35a4ac | 2018-01-18 11:20:50 -0500 | [diff] [blame] | 1229 | 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] | 1230 | err = -ENOENT; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | goto errout; |
| Jiri Pirko | 6bb16e7 | 2017-02-09 14:38:58 +0100 | [diff] [blame] | 1232 | } |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1233 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
| 1234 | NL_SET_ERR_MSG(extack, "Filter already exists"); |
| 1235 | err = -EEXIST; |
| 1236 | goto errout; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | } |
| 1238 | |
| Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 1239 | 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] | 1240 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
| 1241 | extack); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1242 | if (err == 0) { |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1243 | if (tp_created) |
| 1244 | tcf_chain_tp_insert(chain, &chain_info, tp); |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1245 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1246 | RTM_NEWTFILTER, false); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1247 | } else { |
| 1248 | if (tp_created) |
| Jakub Kicinski | 715df5e | 2018-01-24 12:54:13 -0800 | [diff] [blame] | 1249 | tcf_proto_destroy(tp, NULL); |
| Minoru Usui | 12186be | 2009-06-02 02:17:34 -0700 | [diff] [blame] | 1250 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | |
| 1252 | errout: |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1253 | if (chain) |
| 1254 | tcf_chain_put(chain); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | if (err == -EAGAIN) |
| 1256 | /* Replay the request. */ |
| 1257 | goto replay; |
| 1258 | return err; |
| 1259 | } |
| 1260 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1261 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1262 | struct netlink_ext_ack *extack) |
| 1263 | { |
| 1264 | struct net *net = sock_net(skb->sk); |
| 1265 | struct nlattr *tca[TCA_MAX + 1]; |
| 1266 | struct tcmsg *t; |
| 1267 | u32 protocol; |
| 1268 | u32 prio; |
| 1269 | u32 parent; |
| 1270 | u32 chain_index; |
| 1271 | struct Qdisc *q = NULL; |
| 1272 | struct tcf_chain_info chain_info; |
| 1273 | struct tcf_chain *chain = NULL; |
| 1274 | struct tcf_block *block; |
| 1275 | struct tcf_proto *tp = NULL; |
| 1276 | unsigned long cl = 0; |
| 1277 | void *fh = NULL; |
| 1278 | int err; |
| 1279 | |
| 1280 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
| 1281 | return -EPERM; |
| 1282 | |
| 1283 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1284 | if (err < 0) |
| 1285 | return err; |
| 1286 | |
| 1287 | t = nlmsg_data(n); |
| 1288 | protocol = TC_H_MIN(t->tcm_info); |
| 1289 | prio = TC_H_MAJ(t->tcm_info); |
| 1290 | parent = t->tcm_parent; |
| 1291 | |
| 1292 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { |
| 1293 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); |
| 1294 | return -ENOENT; |
| 1295 | } |
| 1296 | |
| 1297 | /* Find head of filter chain. */ |
| 1298 | |
| 1299 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1300 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1301 | if (IS_ERR(block)) { |
| 1302 | err = PTR_ERR(block); |
| 1303 | goto errout; |
| 1304 | } |
| 1305 | |
| 1306 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1307 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1308 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1309 | err = -EINVAL; |
| 1310 | goto errout; |
| 1311 | } |
| 1312 | chain = tcf_chain_get(block, chain_index, false); |
| 1313 | if (!chain) { |
| 1314 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1315 | err = -EINVAL; |
| 1316 | goto errout; |
| 1317 | } |
| 1318 | |
| 1319 | if (prio == 0) { |
| 1320 | tfilter_notify_chain(net, skb, block, q, parent, n, |
| 1321 | chain, RTM_DELTFILTER); |
| 1322 | tcf_chain_flush(chain); |
| 1323 | err = 0; |
| 1324 | goto errout; |
| 1325 | } |
| 1326 | |
| 1327 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1328 | prio, false); |
| 1329 | if (!tp || IS_ERR(tp)) { |
| 1330 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1331 | err = tp ? PTR_ERR(tp) : -ENOENT; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1332 | goto errout; |
| 1333 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1334 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1335 | err = -EINVAL; |
| 1336 | goto errout; |
| 1337 | } |
| 1338 | |
| 1339 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1340 | |
| 1341 | if (!fh) { |
| 1342 | if (t->tcm_handle == 0) { |
| 1343 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1344 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
| 1345 | RTM_DELTFILTER, false); |
| 1346 | tcf_proto_destroy(tp, extack); |
| 1347 | err = 0; |
| 1348 | } else { |
| 1349 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1350 | err = -ENOENT; |
| 1351 | } |
| 1352 | } else { |
| 1353 | bool last; |
| 1354 | |
| 1355 | err = tfilter_del_notify(net, skb, n, tp, block, |
| 1356 | q, parent, fh, false, &last, |
| 1357 | extack); |
| 1358 | if (err) |
| 1359 | goto errout; |
| 1360 | if (last) { |
| 1361 | tcf_chain_tp_remove(chain, &chain_info, tp); |
| 1362 | tcf_proto_destroy(tp, extack); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | errout: |
| 1367 | if (chain) |
| 1368 | tcf_chain_put(chain); |
| 1369 | return err; |
| 1370 | } |
| 1371 | |
| 1372 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
| 1373 | struct netlink_ext_ack *extack) |
| 1374 | { |
| 1375 | struct net *net = sock_net(skb->sk); |
| 1376 | struct nlattr *tca[TCA_MAX + 1]; |
| 1377 | struct tcmsg *t; |
| 1378 | u32 protocol; |
| 1379 | u32 prio; |
| 1380 | u32 parent; |
| 1381 | u32 chain_index; |
| 1382 | struct Qdisc *q = NULL; |
| 1383 | struct tcf_chain_info chain_info; |
| 1384 | struct tcf_chain *chain = NULL; |
| 1385 | struct tcf_block *block; |
| 1386 | struct tcf_proto *tp = NULL; |
| 1387 | unsigned long cl = 0; |
| 1388 | void *fh = NULL; |
| 1389 | int err; |
| 1390 | |
| 1391 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
| 1392 | if (err < 0) |
| 1393 | return err; |
| 1394 | |
| 1395 | t = nlmsg_data(n); |
| 1396 | protocol = TC_H_MIN(t->tcm_info); |
| 1397 | prio = TC_H_MAJ(t->tcm_info); |
| 1398 | parent = t->tcm_parent; |
| 1399 | |
| 1400 | if (prio == 0) { |
| 1401 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
| 1402 | return -ENOENT; |
| 1403 | } |
| 1404 | |
| 1405 | /* Find head of filter chain. */ |
| 1406 | |
| 1407 | block = tcf_block_find(net, &q, &parent, &cl, |
| 1408 | t->tcm_ifindex, t->tcm_block_index, extack); |
| 1409 | if (IS_ERR(block)) { |
| 1410 | err = PTR_ERR(block); |
| 1411 | goto errout; |
| 1412 | } |
| 1413 | |
| 1414 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; |
| 1415 | if (chain_index > TC_ACT_EXT_VAL_MASK) { |
| 1416 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
| 1417 | err = -EINVAL; |
| 1418 | goto errout; |
| 1419 | } |
| 1420 | chain = tcf_chain_get(block, chain_index, false); |
| 1421 | if (!chain) { |
| 1422 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
| 1423 | err = -EINVAL; |
| 1424 | goto errout; |
| 1425 | } |
| 1426 | |
| 1427 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
| 1428 | prio, false); |
| 1429 | if (!tp || IS_ERR(tp)) { |
| 1430 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
| Vlad Buslov | 0e39903 | 2018-06-04 18:32:23 +0300 | [diff] [blame] | 1431 | err = tp ? PTR_ERR(tp) : -ENOENT; |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1432 | goto errout; |
| 1433 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
| 1434 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); |
| 1435 | err = -EINVAL; |
| 1436 | goto errout; |
| 1437 | } |
| 1438 | |
| 1439 | fh = tp->ops->get(tp, t->tcm_handle); |
| 1440 | |
| 1441 | if (!fh) { |
| 1442 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
| 1443 | err = -ENOENT; |
| 1444 | } else { |
| 1445 | err = tfilter_notify(net, skb, n, tp, block, q, parent, |
| 1446 | fh, RTM_NEWTFILTER, true); |
| 1447 | if (err < 0) |
| 1448 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); |
| 1449 | } |
| 1450 | |
| 1451 | errout: |
| 1452 | if (chain) |
| 1453 | tcf_chain_put(chain); |
| 1454 | return err; |
| 1455 | } |
| 1456 | |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1457 | struct tcf_dump_args { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | struct tcf_walker w; |
| 1459 | struct sk_buff *skb; |
| 1460 | struct netlink_callback *cb; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1461 | struct tcf_block *block; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1462 | struct Qdisc *q; |
| 1463 | u32 parent; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | }; |
| 1465 | |
| WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1466 | 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] | 1467 | { |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1468 | struct tcf_dump_args *a = (void *)arg; |
| WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 1469 | struct net *net = sock_net(a->skb->sk); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1471 | 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] | 1472 | n, NETLINK_CB(a->cb->skb).portid, |
| Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 1473 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1474 | RTM_NEWTFILTER); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1477 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
| 1478 | struct sk_buff *skb, struct netlink_callback *cb, |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1479 | long index_start, long *p_index) |
| 1480 | { |
| 1481 | struct net *net = sock_net(skb->sk); |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1482 | struct tcf_block *block = chain->block; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1483 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| 1484 | struct tcf_dump_args arg; |
| 1485 | struct tcf_proto *tp; |
| 1486 | |
| 1487 | for (tp = rtnl_dereference(chain->filter_chain); |
| 1488 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { |
| 1489 | if (*p_index < index_start) |
| 1490 | continue; |
| 1491 | if (TC_H_MAJ(tcm->tcm_info) && |
| 1492 | TC_H_MAJ(tcm->tcm_info) != tp->prio) |
| 1493 | continue; |
| 1494 | if (TC_H_MIN(tcm->tcm_info) && |
| 1495 | TC_H_MIN(tcm->tcm_info) != tp->protocol) |
| 1496 | continue; |
| 1497 | if (*p_index > index_start) |
| 1498 | memset(&cb->args[1], 0, |
| 1499 | sizeof(cb->args) - sizeof(cb->args[0])); |
| 1500 | if (cb->args[1] == 0) { |
| YueHaibing | 5318918 | 2018-07-17 20:58:14 +0800 | [diff] [blame] | 1501 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1502 | NETLINK_CB(cb->skb).portid, |
| 1503 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 1504 | RTM_NEWTFILTER) <= 0) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1505 | return false; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1506 | |
| 1507 | cb->args[1] = 1; |
| 1508 | } |
| 1509 | if (!tp->ops->walk) |
| 1510 | continue; |
| 1511 | arg.w.fn = tcf_node_dump; |
| 1512 | arg.skb = skb; |
| 1513 | arg.cb = cb; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1514 | arg.block = block; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1515 | arg.q = q; |
| 1516 | arg.parent = parent; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1517 | arg.w.stop = 0; |
| 1518 | arg.w.skip = cb->args[1] - 1; |
| 1519 | arg.w.count = 0; |
| Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1520 | arg.w.cookie = cb->args[2]; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1521 | tp->ops->walk(tp, &arg.w); |
| Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1522 | cb->args[2] = arg.w.cookie; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1523 | cb->args[1] = arg.w.count + 1; |
| 1524 | if (arg.w.stop) |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1525 | return false; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1526 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1527 | return true; |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| Eric Dumazet | bd27a87 | 2009-11-05 20:57:26 -0800 | [diff] [blame] | 1530 | /* called with RTNL */ |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
| 1532 | { |
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1533 | struct net *net = sock_net(skb->sk); |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1534 | struct nlattr *tca[TCA_MAX + 1]; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1535 | struct Qdisc *q = NULL; |
| Jiri Pirko | 6529eab | 2017-05-17 11:07:55 +0200 | [diff] [blame] | 1536 | struct tcf_block *block; |
| Jiri Pirko | 2190d1d | 2017-05-17 11:07:59 +0200 | [diff] [blame] | 1537 | struct tcf_chain *chain; |
| David S. Miller | 942b816 | 2012-06-26 21:48:50 -0700 | [diff] [blame] | 1538 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1539 | long index_start; |
| 1540 | long index; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1541 | u32 parent; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1542 | int err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | |
| Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1544 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | return skb->len; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1546 | |
| 1547 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); |
| 1548 | if (err) |
| 1549 | return err; |
| 1550 | |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1551 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
| 1552 | block = tcf_block_lookup(net, tcm->tcm_block_index); |
| 1553 | if (!block) |
| WANG Cong | 143976c | 2017-08-24 16:51:29 -0700 | [diff] [blame] | 1554 | goto out; |
| Jiri Pirko | d680b35 | 2018-01-18 16:14:49 +0100 | [diff] [blame] | 1555 | /* If we work with block index, q is NULL and parent value |
| 1556 | * will never be used in the following code. The check |
| 1557 | * in tcf_fill_node prevents it. However, compiler does not |
| 1558 | * see that far, so set parent to zero to silence the warning |
| 1559 | * about parent being uninitialized. |
| 1560 | */ |
| 1561 | parent = 0; |
| Jiri Pirko | 7960d1d | 2018-01-17 11:46:51 +0100 | [diff] [blame] | 1562 | } else { |
| 1563 | const struct Qdisc_class_ops *cops; |
| 1564 | struct net_device *dev; |
| 1565 | unsigned long cl = 0; |
| 1566 | |
| 1567 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
| 1568 | if (!dev) |
| 1569 | return skb->len; |
| 1570 | |
| 1571 | parent = tcm->tcm_parent; |
| 1572 | if (!parent) { |
| 1573 | q = dev->qdisc; |
| 1574 | parent = q->handle; |
| 1575 | } else { |
| 1576 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
| 1577 | } |
| 1578 | if (!q) |
| 1579 | goto out; |
| 1580 | cops = q->ops->cl_ops; |
| 1581 | if (!cops) |
| 1582 | goto out; |
| 1583 | if (!cops->tcf_block) |
| 1584 | goto out; |
| 1585 | if (TC_H_MIN(tcm->tcm_parent)) { |
| 1586 | cl = cops->find(q, tcm->tcm_parent); |
| 1587 | if (cl == 0) |
| 1588 | goto out; |
| 1589 | } |
| 1590 | block = cops->tcf_block(q, cl, NULL); |
| 1591 | if (!block) |
| 1592 | goto out; |
| 1593 | if (tcf_block_shared(block)) |
| 1594 | q = NULL; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1597 | index_start = cb->args[0]; |
| 1598 | index = 0; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1599 | |
| 1600 | list_for_each_entry(chain, &block->chain_list, list) { |
| 1601 | if (tca[TCA_CHAIN] && |
| 1602 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) |
| 1603 | continue; |
| Jiri Pirko | a10fa20 | 2017-10-13 14:01:05 +0200 | [diff] [blame] | 1604 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1605 | index_start, &index)) { |
| 1606 | err = -EMSGSIZE; |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1607 | break; |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1608 | } |
| Jiri Pirko | 5bc1701 | 2017-05-17 11:08:01 +0200 | [diff] [blame] | 1609 | } |
| 1610 | |
| Jiri Pirko | acb31fa | 2017-05-17 11:08:00 +0200 | [diff] [blame] | 1611 | cb->args[0] = index; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | out: |
| Roman Kapl | 5ae437a | 2018-02-19 21:32:51 +0100 | [diff] [blame] | 1614 | /* If we did no progress, the error (EMSGSIZE) is real */ |
| 1615 | if (skb->len == 0 && err) |
| 1616 | return err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | return skb->len; |
| 1618 | } |
| 1619 | |
| WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 1620 | void tcf_exts_destroy(struct tcf_exts *exts) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | { |
| 1622 | #ifdef CONFIG_NET_CLS_ACT |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1623 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1624 | kfree(exts->actions); |
| 1625 | exts->nr_actions = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | #endif |
| 1627 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1628 | EXPORT_SYMBOL(tcf_exts_destroy); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
| Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 1630 | 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] | 1631 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
| 1632 | struct netlink_ext_ack *extack) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | #ifdef CONFIG_NET_CLS_ACT |
| 1635 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | struct tc_action *act; |
| Roman Mashak | d04e699 | 2018-03-08 16:59:17 -0500 | [diff] [blame] | 1637 | size_t attr_size = 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1639 | if (exts->police && tb[exts->police]) { |
| Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 1640 | act = tcf_action_init_1(net, tp, tb[exts->police], |
| 1641 | rate_tlv, "police", ovr, |
| Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 1642 | TCA_ACT_BIND, true, extack); |
| Patrick McHardy | ab27cfb | 2008-01-23 20:33:13 -0800 | [diff] [blame] | 1643 | if (IS_ERR(act)) |
| 1644 | return PTR_ERR(act); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1646 | act->type = exts->type = TCA_OLD_COMPAT; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1647 | exts->actions[0] = act; |
| 1648 | exts->nr_actions = 1; |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1649 | } else if (exts->action && tb[exts->action]) { |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1650 | int err; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1651 | |
| Jiri Pirko | 9fb9f25 | 2017-05-17 11:08:02 +0200 | [diff] [blame] | 1652 | err = tcf_action_init(net, tp, tb[exts->action], |
| 1653 | rate_tlv, NULL, ovr, TCA_ACT_BIND, |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1654 | exts->actions, &attr_size, true, |
| Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 1655 | extack); |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1656 | if (err < 0) |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1657 | return err; |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1658 | exts->nr_actions = err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| Cong Wang | e4b95c4 | 2017-11-06 13:47:19 -0800 | [diff] [blame] | 1660 | exts->net = net; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | #else |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1663 | if ((exts->action && tb[exts->action]) || |
| Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 1664 | (exts->police && tb[exts->police])) { |
| 1665 | 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] | 1666 | return -EOPNOTSUPP; |
| Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 1667 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | #endif |
| 1669 | |
| 1670 | return 0; |
| 1671 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1672 | EXPORT_SYMBOL(tcf_exts_validate); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | |
| Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 1674 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | { |
| 1676 | #ifdef CONFIG_NET_CLS_ACT |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1677 | struct tcf_exts old = *dst; |
| 1678 | |
| Jiri Pirko | 9b0d444 | 2017-08-04 14:29:15 +0200 | [diff] [blame] | 1679 | *dst = *src; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1680 | tcf_exts_destroy(&old); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | #endif |
| 1682 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1683 | EXPORT_SYMBOL(tcf_exts_change); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1685 | #ifdef CONFIG_NET_CLS_ACT |
| 1686 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) |
| 1687 | { |
| 1688 | if (exts->nr_actions == 0) |
| 1689 | return NULL; |
| 1690 | else |
| 1691 | return exts->actions[0]; |
| 1692 | } |
| 1693 | #endif |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1694 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1695 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | { |
| 1697 | #ifdef CONFIG_NET_CLS_ACT |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1698 | struct nlattr *nest; |
| 1699 | |
| Jiri Pirko | 978dfd8 | 2017-08-04 14:29:03 +0200 | [diff] [blame] | 1700 | if (exts->action && tcf_exts_has_actions(exts)) { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | /* |
| 1702 | * again for backward compatible mode - we want |
| 1703 | * to work with both old and new modes of entering |
| 1704 | * tc data even if iproute2 was newer - jhs |
| 1705 | */ |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1706 | if (exts->type != TCA_OLD_COMPAT) { |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1707 | nest = nla_nest_start(skb, exts->action); |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1708 | if (nest == NULL) |
| 1709 | goto nla_put_failure; |
| WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1710 | |
| Vlad Buslov | 90b73b7 | 2018-07-05 17:24:33 +0300 | [diff] [blame] | 1711 | if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1712 | goto nla_put_failure; |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1713 | nla_nest_end(skb, nest); |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1714 | } else if (exts->police) { |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1715 | struct tc_action *act = tcf_exts_first_act(exts); |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1716 | nest = nla_nest_start(skb, exts->police); |
| Jamal Hadi Salim | 63acd68 | 2013-12-23 08:02:12 -0500 | [diff] [blame] | 1717 | if (nest == NULL || !act) |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1718 | goto nla_put_failure; |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1719 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
| Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 1720 | goto nla_put_failure; |
| Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 1721 | nla_nest_end(skb, nest); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | } |
| 1723 | } |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | return 0; |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1725 | |
| 1726 | nla_put_failure: |
| 1727 | nla_nest_cancel(skb, nest); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | return -1; |
| Cong Wang | 9cc63db | 2014-07-16 14:25:30 -0700 | [diff] [blame] | 1729 | #else |
| 1730 | return 0; |
| 1731 | #endif |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1733 | EXPORT_SYMBOL(tcf_exts_dump); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1735 | |
| WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 1736 | 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] | 1737 | { |
| 1738 | #ifdef CONFIG_NET_CLS_ACT |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1739 | struct tc_action *a = tcf_exts_first_act(exts); |
| Ignacy Gawędzki | b057df2 | 2015-02-03 19:05:18 +0100 | [diff] [blame] | 1740 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
| WANG Cong | 33be627 | 2013-12-15 20:15:05 -0800 | [diff] [blame] | 1741 | return -1; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | #endif |
| 1743 | return 0; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | } |
| Stephen Hemminger | aa767bf | 2008-01-21 02:26:41 -0800 | [diff] [blame] | 1745 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1747 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
| 1748 | enum tc_setup_type type, |
| 1749 | void *type_data, bool err_stop) |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1750 | { |
| 1751 | int ok_count = 0; |
| 1752 | #ifdef CONFIG_NET_CLS_ACT |
| 1753 | const struct tc_action *a; |
| 1754 | struct net_device *dev; |
| Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 1755 | int i, ret; |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1756 | |
| 1757 | if (!tcf_exts_has_actions(exts)) |
| 1758 | return 0; |
| 1759 | |
| Or Gerlitz | 9d452ce | 2017-10-24 08:58:02 +0300 | [diff] [blame] | 1760 | for (i = 0; i < exts->nr_actions; i++) { |
| 1761 | a = exts->actions[i]; |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1762 | if (!a->ops->get_dev) |
| 1763 | continue; |
| 1764 | dev = a->ops->get_dev(a); |
| Jiri Pirko | 7612fb0 | 2017-11-01 11:47:40 +0100 | [diff] [blame] | 1765 | if (!dev) |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1766 | continue; |
| 1767 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); |
| 1768 | if (ret < 0) |
| 1769 | return ret; |
| 1770 | ok_count += ret; |
| 1771 | } |
| 1772 | #endif |
| 1773 | return ok_count; |
| 1774 | } |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1775 | |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 1776 | int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, |
| 1777 | enum tc_setup_type type, void *type_data, bool err_stop) |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1778 | { |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 1779 | int ok_count; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 1780 | int ret; |
| 1781 | |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 1782 | ret = tcf_block_cb_call(block, type, type_data, err_stop); |
| 1783 | if (ret < 0) |
| 1784 | return ret; |
| 1785 | ok_count = ret; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 1786 | |
| Or Gerlitz | f8f4bef | 2018-05-23 19:24:48 +0300 | [diff] [blame] | 1787 | if (!exts || ok_count) |
| David S. Miller | 9a99dc1 | 2018-06-06 13:55:47 -0400 | [diff] [blame] | 1788 | return ok_count; |
| Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 1789 | ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); |
| 1790 | if (ret < 0) |
| 1791 | return ret; |
| 1792 | ok_count += ret; |
| 1793 | |
| 1794 | return ok_count; |
| Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 1795 | } |
| 1796 | EXPORT_SYMBOL(tc_setup_cb_call); |
| Jiri Pirko | b3f55bd | 2017-10-11 09:41:08 +0200 | [diff] [blame] | 1797 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1798 | static __net_init int tcf_net_init(struct net *net) |
| 1799 | { |
| 1800 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 1801 | |
| 1802 | idr_init(&tn->idr); |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | static void __net_exit tcf_net_exit(struct net *net) |
| 1807 | { |
| 1808 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
| 1809 | |
| 1810 | idr_destroy(&tn->idr); |
| 1811 | } |
| 1812 | |
| 1813 | static struct pernet_operations tcf_net_ops = { |
| 1814 | .init = tcf_net_init, |
| 1815 | .exit = tcf_net_exit, |
| 1816 | .id = &tcf_net_id, |
| 1817 | .size = sizeof(struct tcf_net), |
| 1818 | }; |
| 1819 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | static int __init tc_filter_init(void) |
| 1821 | { |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1822 | int err; |
| 1823 | |
| Cong Wang | 7aa0045 | 2017-10-26 18:24:28 -0700 | [diff] [blame] | 1824 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
| 1825 | if (!tc_filter_wq) |
| 1826 | return -ENOMEM; |
| 1827 | |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1828 | err = register_pernet_subsys(&tcf_net_ops); |
| 1829 | if (err) |
| 1830 | goto err_register_pernet_subsys; |
| 1831 | |
| Vlad Buslov | c431f89 | 2018-05-31 09:52:53 +0300 | [diff] [blame] | 1832 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0); |
| 1833 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0); |
| 1834 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, |
| Florian Westphal | b97bac6 | 2017-08-09 20:41:48 +0200 | [diff] [blame] | 1835 | tc_dump_tfilter, 0); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | return 0; |
| Jiri Pirko | 4861738 | 2018-01-17 11:46:46 +0100 | [diff] [blame] | 1838 | |
| 1839 | err_register_pernet_subsys: |
| 1840 | destroy_workqueue(tc_filter_wq); |
| 1841 | return err; |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | subsys_initcall(tc_filter_init); |