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