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