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