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