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