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