]>
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> | |
ab27cfb8 | 26 | #include <linux/err.h> |
5a0e3ad6 | 27 | #include <linux/slab.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 | |
7aa0045d CW |
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 | ||
1da177e4 LT |
112 | /* Select new prio value from the range, managed by kernel. */ |
113 | ||
aa767bfe | 114 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
1da177e4 | 115 | { |
aa767bfe | 116 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
1da177e4 LT |
117 | |
118 | if (tp) | |
cc7ec456 | 119 | first = tp->prio - 1; |
1da177e4 | 120 | |
7961973a | 121 | return TC_H_MAJ(first); |
1da177e4 LT |
122 | } |
123 | ||
33a48927 | 124 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
6529eaba | 125 | u32 prio, u32 parent, struct Qdisc *q, |
5bc17018 | 126 | struct tcf_chain *chain) |
33a48927 JP |
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; | |
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 | ||
763dbf63 | 176 | static void tcf_proto_destroy(struct tcf_proto *tp) |
cf1facda | 177 | { |
763dbf63 WC |
178 | tp->ops->destroy(tp); |
179 | module_put(tp->ops->owner); | |
180 | kfree_rcu(tp, rcu); | |
cf1facda JP |
181 | } |
182 | ||
5bc17018 JP |
183 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
184 | u32 chain_index) | |
2190d1d0 | 185 | { |
5bc17018 JP |
186 | struct tcf_chain *chain; |
187 | ||
188 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); | |
189 | if (!chain) | |
190 | return NULL; | |
191 | list_add_tail(&chain->list, &block->chain_list); | |
192 | chain->block = block; | |
193 | chain->index = chain_index; | |
e2ef7544 | 194 | chain->refcnt = 1; |
5bc17018 | 195 | return chain; |
2190d1d0 JP |
196 | } |
197 | ||
f93e1cdc | 198 | static void tcf_chain_flush(struct tcf_chain *chain) |
cf1facda JP |
199 | { |
200 | struct tcf_proto *tp; | |
201 | ||
acc8b316 | 202 | if (chain->p_filter_chain) |
f93e1cdc | 203 | RCU_INIT_POINTER(*chain->p_filter_chain, NULL); |
2190d1d0 JP |
204 | while ((tp = rtnl_dereference(chain->filter_chain)) != NULL) { |
205 | RCU_INIT_POINTER(chain->filter_chain, tp->next); | |
e2ef7544 | 206 | tcf_chain_put(chain); |
763dbf63 | 207 | tcf_proto_destroy(tp); |
cf1facda | 208 | } |
f93e1cdc JP |
209 | } |
210 | ||
211 | static void tcf_chain_destroy(struct tcf_chain *chain) | |
212 | { | |
e2ef7544 CW |
213 | list_del(&chain->list); |
214 | kfree(chain); | |
215 | } | |
744a4cf6 | 216 | |
e2ef7544 CW |
217 | static void tcf_chain_hold(struct tcf_chain *chain) |
218 | { | |
219 | ++chain->refcnt; | |
2190d1d0 JP |
220 | } |
221 | ||
367a8ce8 WC |
222 | struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
223 | bool create) | |
5bc17018 JP |
224 | { |
225 | struct tcf_chain *chain; | |
226 | ||
227 | list_for_each_entry(chain, &block->chain_list, list) { | |
e2ef7544 CW |
228 | if (chain->index == chain_index) { |
229 | tcf_chain_hold(chain); | |
230 | return chain; | |
231 | } | |
5bc17018 | 232 | } |
80532384 | 233 | |
e2ef7544 | 234 | return create ? tcf_chain_create(block, chain_index) : NULL; |
5bc17018 JP |
235 | } |
236 | EXPORT_SYMBOL(tcf_chain_get); | |
237 | ||
238 | void tcf_chain_put(struct tcf_chain *chain) | |
239 | { | |
e2ef7544 | 240 | if (--chain->refcnt == 0) |
5bc17018 JP |
241 | tcf_chain_destroy(chain); |
242 | } | |
243 | EXPORT_SYMBOL(tcf_chain_put); | |
244 | ||
2190d1d0 JP |
245 | static void |
246 | tcf_chain_filter_chain_ptr_set(struct tcf_chain *chain, | |
247 | struct tcf_proto __rcu **p_filter_chain) | |
248 | { | |
249 | chain->p_filter_chain = p_filter_chain; | |
cf1facda | 250 | } |
6529eaba | 251 | |
8c4083b3 JP |
252 | static void tcf_block_offload_cmd(struct tcf_block *block, struct Qdisc *q, |
253 | struct tcf_block_ext_info *ei, | |
254 | enum tc_block_command command) | |
255 | { | |
256 | struct net_device *dev = q->dev_queue->dev; | |
257 | struct tc_block_offload bo = {}; | |
258 | ||
44ae12a7 | 259 | if (!dev->netdev_ops->ndo_setup_tc) |
8c4083b3 JP |
260 | return; |
261 | bo.command = command; | |
262 | bo.binder_type = ei->binder_type; | |
263 | bo.block = block; | |
264 | dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); | |
265 | } | |
266 | ||
267 | static void tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, | |
268 | struct tcf_block_ext_info *ei) | |
269 | { | |
270 | tcf_block_offload_cmd(block, q, ei, TC_BLOCK_BIND); | |
271 | } | |
272 | ||
273 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, | |
274 | struct tcf_block_ext_info *ei) | |
275 | { | |
276 | tcf_block_offload_cmd(block, q, ei, TC_BLOCK_UNBIND); | |
277 | } | |
278 | ||
279 | int tcf_block_get_ext(struct tcf_block **p_block, | |
280 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, | |
281 | struct tcf_block_ext_info *ei) | |
6529eaba JP |
282 | { |
283 | struct tcf_block *block = kzalloc(sizeof(*block), GFP_KERNEL); | |
5bc17018 | 284 | struct tcf_chain *chain; |
2190d1d0 | 285 | int err; |
6529eaba JP |
286 | |
287 | if (!block) | |
288 | return -ENOMEM; | |
5bc17018 | 289 | INIT_LIST_HEAD(&block->chain_list); |
acb67442 JP |
290 | INIT_LIST_HEAD(&block->cb_list); |
291 | ||
5bc17018 JP |
292 | /* Create chain 0 by default, it has to be always present. */ |
293 | chain = tcf_chain_create(block, 0); | |
294 | if (!chain) { | |
2190d1d0 JP |
295 | err = -ENOMEM; |
296 | goto err_chain_create; | |
297 | } | |
5bc17018 | 298 | tcf_chain_filter_chain_ptr_set(chain, p_filter_chain); |
855319be | 299 | block->net = qdisc_net(q); |
69d78ef2 | 300 | block->q = q; |
8c4083b3 | 301 | tcf_block_offload_bind(block, q, ei); |
6529eaba JP |
302 | *p_block = block; |
303 | return 0; | |
2190d1d0 JP |
304 | |
305 | err_chain_create: | |
306 | kfree(block); | |
307 | return err; | |
6529eaba | 308 | } |
8c4083b3 JP |
309 | EXPORT_SYMBOL(tcf_block_get_ext); |
310 | ||
311 | int tcf_block_get(struct tcf_block **p_block, | |
312 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q) | |
313 | { | |
314 | struct tcf_block_ext_info ei = {0, }; | |
315 | ||
316 | return tcf_block_get_ext(p_block, p_filter_chain, q, &ei); | |
317 | } | |
6529eaba JP |
318 | EXPORT_SYMBOL(tcf_block_get); |
319 | ||
7aa0045d | 320 | static void tcf_block_put_final(struct work_struct *work) |
6529eaba | 321 | { |
7aa0045d | 322 | struct tcf_block *block = container_of(work, struct tcf_block, work); |
5bc17018 JP |
323 | struct tcf_chain *chain, *tmp; |
324 | ||
7aa0045d | 325 | rtnl_lock(); |
822e86d9 | 326 | /* Only chain 0 should be still here. */ |
7aa0045d CW |
327 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
328 | tcf_chain_put(chain); | |
329 | rtnl_unlock(); | |
330 | kfree(block); | |
331 | } | |
1697c4bb | 332 | |
7aa0045d | 333 | /* XXX: Standalone actions are not allowed to jump to any chain, and bound |
822e86d9 CW |
334 | * actions should be all removed after flushing. However, filters are now |
335 | * destroyed in tc filter workqueue with RTNL lock, they can not race here. | |
7aa0045d | 336 | */ |
e1ea2f98 DM |
337 | void tcf_block_put_ext(struct tcf_block *block, |
338 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, | |
339 | struct tcf_block_ext_info *ei) | |
7aa0045d | 340 | { |
822e86d9 | 341 | struct tcf_chain *chain, *tmp; |
1697c4bb | 342 | |
7aa0045d CW |
343 | if (!block) |
344 | return; | |
345 | ||
e1ea2f98 DM |
346 | tcf_block_offload_unbind(block, q, ei); |
347 | ||
822e86d9 | 348 | list_for_each_entry_safe(chain, tmp, &block->chain_list, list) |
30d65e8f | 349 | tcf_chain_flush(chain); |
e2ef7544 | 350 | |
7aa0045d | 351 | INIT_WORK(&block->work, tcf_block_put_final); |
7aa0045d CW |
352 | /* Wait for existing RCU callbacks to cool down, make sure their works |
353 | * have been queued before this. We can not flush pending works here | |
354 | * because we are holding the RTNL lock. | |
355 | */ | |
356 | rcu_barrier(); | |
357 | tcf_queue_work(&block->work); | |
6529eaba | 358 | } |
8c4083b3 JP |
359 | EXPORT_SYMBOL(tcf_block_put_ext); |
360 | ||
361 | void tcf_block_put(struct tcf_block *block) | |
362 | { | |
363 | struct tcf_block_ext_info ei = {0, }; | |
364 | ||
365 | tcf_block_put_ext(block, NULL, block->q, &ei); | |
366 | } | |
e1ea2f98 | 367 | |
6529eaba | 368 | EXPORT_SYMBOL(tcf_block_put); |
cf1facda | 369 | |
acb67442 JP |
370 | struct tcf_block_cb { |
371 | struct list_head list; | |
372 | tc_setup_cb_t *cb; | |
373 | void *cb_ident; | |
374 | void *cb_priv; | |
375 | unsigned int refcnt; | |
376 | }; | |
377 | ||
378 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) | |
379 | { | |
380 | return block_cb->cb_priv; | |
381 | } | |
382 | EXPORT_SYMBOL(tcf_block_cb_priv); | |
383 | ||
384 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, | |
385 | tc_setup_cb_t *cb, void *cb_ident) | |
386 | { struct tcf_block_cb *block_cb; | |
387 | ||
388 | list_for_each_entry(block_cb, &block->cb_list, list) | |
389 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) | |
390 | return block_cb; | |
391 | return NULL; | |
392 | } | |
393 | EXPORT_SYMBOL(tcf_block_cb_lookup); | |
394 | ||
395 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) | |
396 | { | |
397 | block_cb->refcnt++; | |
398 | } | |
399 | EXPORT_SYMBOL(tcf_block_cb_incref); | |
400 | ||
401 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) | |
402 | { | |
403 | return --block_cb->refcnt; | |
404 | } | |
405 | EXPORT_SYMBOL(tcf_block_cb_decref); | |
406 | ||
407 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, | |
408 | tc_setup_cb_t *cb, void *cb_ident, | |
409 | void *cb_priv) | |
410 | { | |
411 | struct tcf_block_cb *block_cb; | |
412 | ||
413 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); | |
414 | if (!block_cb) | |
415 | return NULL; | |
416 | block_cb->cb = cb; | |
417 | block_cb->cb_ident = cb_ident; | |
418 | block_cb->cb_priv = cb_priv; | |
419 | list_add(&block_cb->list, &block->cb_list); | |
420 | return block_cb; | |
421 | } | |
422 | EXPORT_SYMBOL(__tcf_block_cb_register); | |
423 | ||
424 | int tcf_block_cb_register(struct tcf_block *block, | |
425 | tc_setup_cb_t *cb, void *cb_ident, | |
426 | void *cb_priv) | |
427 | { | |
428 | struct tcf_block_cb *block_cb; | |
429 | ||
430 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv); | |
431 | return block_cb ? 0 : -ENOMEM; | |
432 | } | |
433 | EXPORT_SYMBOL(tcf_block_cb_register); | |
434 | ||
435 | void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb) | |
436 | { | |
437 | list_del(&block_cb->list); | |
438 | kfree(block_cb); | |
439 | } | |
440 | EXPORT_SYMBOL(__tcf_block_cb_unregister); | |
441 | ||
442 | void tcf_block_cb_unregister(struct tcf_block *block, | |
443 | tc_setup_cb_t *cb, void *cb_ident) | |
444 | { | |
445 | struct tcf_block_cb *block_cb; | |
446 | ||
447 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); | |
448 | if (!block_cb) | |
449 | return; | |
450 | __tcf_block_cb_unregister(block_cb); | |
451 | } | |
452 | EXPORT_SYMBOL(tcf_block_cb_unregister); | |
453 | ||
454 | static int tcf_block_cb_call(struct tcf_block *block, enum tc_setup_type type, | |
455 | void *type_data, bool err_stop) | |
456 | { | |
457 | struct tcf_block_cb *block_cb; | |
458 | int ok_count = 0; | |
459 | int err; | |
460 | ||
461 | list_for_each_entry(block_cb, &block->cb_list, list) { | |
462 | err = block_cb->cb(type, type_data, block_cb->cb_priv); | |
463 | if (err) { | |
464 | if (err_stop) | |
465 | return err; | |
466 | } else { | |
467 | ok_count++; | |
468 | } | |
469 | } | |
470 | return ok_count; | |
471 | } | |
472 | ||
87d83093 JP |
473 | /* Main classifier routine: scans classifier chain attached |
474 | * to this qdisc, (optionally) tests for protocol and asks | |
475 | * specific classifiers. | |
476 | */ | |
477 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, | |
478 | struct tcf_result *res, bool compat_mode) | |
479 | { | |
480 | __be16 protocol = tc_skb_protocol(skb); | |
481 | #ifdef CONFIG_NET_CLS_ACT | |
482 | const int max_reclassify_loop = 4; | |
ee538dce JP |
483 | const struct tcf_proto *orig_tp = tp; |
484 | const struct tcf_proto *first_tp; | |
87d83093 JP |
485 | int limit = 0; |
486 | ||
487 | reclassify: | |
488 | #endif | |
489 | for (; tp; tp = rcu_dereference_bh(tp->next)) { | |
490 | int err; | |
491 | ||
492 | if (tp->protocol != protocol && | |
493 | tp->protocol != htons(ETH_P_ALL)) | |
494 | continue; | |
495 | ||
496 | err = tp->classify(skb, tp, res); | |
497 | #ifdef CONFIG_NET_CLS_ACT | |
db50514f | 498 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
ee538dce | 499 | first_tp = orig_tp; |
87d83093 | 500 | goto reset; |
db50514f | 501 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
ee538dce | 502 | first_tp = res->goto_tp; |
db50514f JP |
503 | goto reset; |
504 | } | |
87d83093 JP |
505 | #endif |
506 | if (err >= 0) | |
507 | return err; | |
508 | } | |
509 | ||
510 | return TC_ACT_UNSPEC; /* signal: continue lookup */ | |
511 | #ifdef CONFIG_NET_CLS_ACT | |
512 | reset: | |
513 | if (unlikely(limit++ >= max_reclassify_loop)) { | |
514 | net_notice_ratelimited("%s: reclassify loop, rule prio %u, protocol %02x\n", | |
515 | tp->q->ops->id, tp->prio & 0xffff, | |
516 | ntohs(tp->protocol)); | |
517 | return TC_ACT_SHOT; | |
518 | } | |
519 | ||
ee538dce | 520 | tp = first_tp; |
87d83093 JP |
521 | protocol = tc_skb_protocol(skb); |
522 | goto reclassify; | |
523 | #endif | |
524 | } | |
525 | EXPORT_SYMBOL(tcf_classify); | |
526 | ||
2190d1d0 JP |
527 | struct tcf_chain_info { |
528 | struct tcf_proto __rcu **pprev; | |
529 | struct tcf_proto __rcu *next; | |
530 | }; | |
531 | ||
532 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain_info *chain_info) | |
533 | { | |
534 | return rtnl_dereference(*chain_info->pprev); | |
535 | } | |
536 | ||
537 | static void tcf_chain_tp_insert(struct tcf_chain *chain, | |
538 | struct tcf_chain_info *chain_info, | |
539 | struct tcf_proto *tp) | |
540 | { | |
541 | if (chain->p_filter_chain && | |
542 | *chain_info->pprev == chain->filter_chain) | |
31efcc25 | 543 | rcu_assign_pointer(*chain->p_filter_chain, tp); |
2190d1d0 JP |
544 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain_info)); |
545 | rcu_assign_pointer(*chain_info->pprev, tp); | |
e2ef7544 | 546 | tcf_chain_hold(chain); |
2190d1d0 JP |
547 | } |
548 | ||
549 | static void tcf_chain_tp_remove(struct tcf_chain *chain, | |
550 | struct tcf_chain_info *chain_info, | |
551 | struct tcf_proto *tp) | |
552 | { | |
553 | struct tcf_proto *next = rtnl_dereference(chain_info->next); | |
554 | ||
555 | if (chain->p_filter_chain && tp == chain->filter_chain) | |
31efcc25 | 556 | RCU_INIT_POINTER(*chain->p_filter_chain, next); |
2190d1d0 | 557 | RCU_INIT_POINTER(*chain_info->pprev, next); |
e2ef7544 | 558 | tcf_chain_put(chain); |
2190d1d0 JP |
559 | } |
560 | ||
561 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, | |
562 | struct tcf_chain_info *chain_info, | |
563 | u32 protocol, u32 prio, | |
564 | bool prio_allocate) | |
565 | { | |
566 | struct tcf_proto **pprev; | |
567 | struct tcf_proto *tp; | |
568 | ||
569 | /* Check the chain for existence of proto-tcf with this priority */ | |
570 | for (pprev = &chain->filter_chain; | |
571 | (tp = rtnl_dereference(*pprev)); pprev = &tp->next) { | |
572 | if (tp->prio >= prio) { | |
573 | if (tp->prio == prio) { | |
574 | if (prio_allocate || | |
575 | (tp->protocol != protocol && protocol)) | |
576 | return ERR_PTR(-EINVAL); | |
577 | } else { | |
578 | tp = NULL; | |
579 | } | |
580 | break; | |
581 | } | |
582 | } | |
583 | chain_info->pprev = pprev; | |
584 | chain_info->next = tp ? tp->next : NULL; | |
585 | return tp; | |
586 | } | |
587 | ||
7120371c | 588 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
a10fa201 JP |
589 | struct tcf_proto *tp, struct Qdisc *q, u32 parent, |
590 | void *fh, u32 portid, u32 seq, u16 flags, int event) | |
7120371c WC |
591 | { |
592 | struct tcmsg *tcm; | |
593 | struct nlmsghdr *nlh; | |
594 | unsigned char *b = skb_tail_pointer(skb); | |
595 | ||
596 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); | |
597 | if (!nlh) | |
598 | goto out_nlmsg_trim; | |
599 | tcm = nlmsg_data(nlh); | |
600 | tcm->tcm_family = AF_UNSPEC; | |
601 | tcm->tcm__pad1 = 0; | |
602 | tcm->tcm__pad2 = 0; | |
a10fa201 JP |
603 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; |
604 | tcm->tcm_parent = parent; | |
7120371c WC |
605 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
606 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) | |
607 | goto nla_put_failure; | |
608 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) | |
609 | goto nla_put_failure; | |
610 | if (!fh) { | |
611 | tcm->tcm_handle = 0; | |
612 | } else { | |
613 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) | |
614 | goto nla_put_failure; | |
615 | } | |
616 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; | |
617 | return skb->len; | |
618 | ||
619 | out_nlmsg_trim: | |
620 | nla_put_failure: | |
621 | nlmsg_trim(skb, b); | |
622 | return -1; | |
623 | } | |
624 | ||
625 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, | |
626 | struct nlmsghdr *n, struct tcf_proto *tp, | |
a10fa201 | 627 | struct Qdisc *q, u32 parent, |
7120371c WC |
628 | void *fh, int event, bool unicast) |
629 | { | |
630 | struct sk_buff *skb; | |
631 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
632 | ||
633 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
634 | if (!skb) | |
635 | return -ENOBUFS; | |
636 | ||
a10fa201 | 637 | if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq, |
7120371c WC |
638 | n->nlmsg_flags, event) <= 0) { |
639 | kfree_skb(skb); | |
640 | return -EINVAL; | |
641 | } | |
642 | ||
643 | if (unicast) | |
644 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
645 | ||
646 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
647 | n->nlmsg_flags & NLM_F_ECHO); | |
648 | } | |
649 | ||
650 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, | |
651 | struct nlmsghdr *n, struct tcf_proto *tp, | |
a10fa201 | 652 | struct Qdisc *q, u32 parent, |
7120371c WC |
653 | void *fh, bool unicast, bool *last) |
654 | { | |
655 | struct sk_buff *skb; | |
656 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
657 | int err; | |
658 | ||
659 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
660 | if (!skb) | |
661 | return -ENOBUFS; | |
662 | ||
a10fa201 | 663 | if (tcf_fill_node(net, skb, tp, q, parent, fh, portid, n->nlmsg_seq, |
7120371c WC |
664 | n->nlmsg_flags, RTM_DELTFILTER) <= 0) { |
665 | kfree_skb(skb); | |
666 | return -EINVAL; | |
667 | } | |
668 | ||
669 | err = tp->ops->delete(tp, fh, last); | |
670 | if (err) { | |
671 | kfree_skb(skb); | |
672 | return err; | |
673 | } | |
674 | ||
675 | if (unicast) | |
676 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
677 | ||
678 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
679 | n->nlmsg_flags & NLM_F_ECHO); | |
680 | } | |
681 | ||
682 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, | |
a10fa201 | 683 | struct Qdisc *q, u32 parent, |
7120371c WC |
684 | struct nlmsghdr *n, |
685 | struct tcf_chain *chain, int event) | |
686 | { | |
687 | struct tcf_proto *tp; | |
688 | ||
689 | for (tp = rtnl_dereference(chain->filter_chain); | |
690 | tp; tp = rtnl_dereference(tp->next)) | |
a10fa201 | 691 | tfilter_notify(net, oskb, n, tp, q, parent, 0, event, false); |
7120371c WC |
692 | } |
693 | ||
1da177e4 LT |
694 | /* Add/change/delete/get a filter node */ |
695 | ||
c21ef3e3 DA |
696 | static int tc_ctl_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
697 | struct netlink_ext_ack *extack) | |
1da177e4 | 698 | { |
3b1e0a65 | 699 | struct net *net = sock_net(skb->sk); |
add93b61 | 700 | struct nlattr *tca[TCA_MAX + 1]; |
1da177e4 LT |
701 | struct tcmsg *t; |
702 | u32 protocol; | |
703 | u32 prio; | |
9d36d9e5 | 704 | bool prio_allocate; |
1da177e4 | 705 | u32 parent; |
5bc17018 | 706 | u32 chain_index; |
1da177e4 LT |
707 | struct net_device *dev; |
708 | struct Qdisc *q; | |
2190d1d0 | 709 | struct tcf_chain_info chain_info; |
5bc17018 | 710 | struct tcf_chain *chain = NULL; |
6529eaba | 711 | struct tcf_block *block; |
1da177e4 | 712 | struct tcf_proto *tp; |
20fea08b | 713 | const struct Qdisc_class_ops *cops; |
1da177e4 | 714 | unsigned long cl; |
8113c095 | 715 | void *fh; |
1da177e4 | 716 | int err; |
628185cf | 717 | int tp_created; |
1da177e4 | 718 | |
4e8bbb81 | 719 | if ((n->nlmsg_type != RTM_GETTFILTER) && |
5f013c9b | 720 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
dfc47ef8 | 721 | return -EPERM; |
de179c8c | 722 | |
1da177e4 | 723 | replay: |
628185cf DB |
724 | tp_created = 0; |
725 | ||
c21ef3e3 | 726 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, NULL, extack); |
de179c8c H |
727 | if (err < 0) |
728 | return err; | |
729 | ||
942b8165 | 730 | t = nlmsg_data(n); |
1da177e4 LT |
731 | protocol = TC_H_MIN(t->tcm_info); |
732 | prio = TC_H_MAJ(t->tcm_info); | |
9d36d9e5 | 733 | prio_allocate = false; |
1da177e4 LT |
734 | parent = t->tcm_parent; |
735 | cl = 0; | |
736 | ||
737 | if (prio == 0) { | |
ea7f8277 DB |
738 | switch (n->nlmsg_type) { |
739 | case RTM_DELTFILTER: | |
9f6ed032 | 740 | if (protocol || t->tcm_handle || tca[TCA_KIND]) |
ea7f8277 DB |
741 | return -ENOENT; |
742 | break; | |
743 | case RTM_NEWTFILTER: | |
744 | /* If no priority is provided by the user, | |
745 | * we allocate one. | |
746 | */ | |
747 | if (n->nlmsg_flags & NLM_F_CREATE) { | |
748 | prio = TC_H_MAKE(0x80000000U, 0U); | |
9d36d9e5 | 749 | prio_allocate = true; |
ea7f8277 DB |
750 | break; |
751 | } | |
752 | /* fall-through */ | |
753 | default: | |
1da177e4 | 754 | return -ENOENT; |
ea7f8277 | 755 | } |
1da177e4 LT |
756 | } |
757 | ||
758 | /* Find head of filter chain. */ | |
759 | ||
760 | /* Find link */ | |
7316ae88 | 761 | dev = __dev_get_by_index(net, t->tcm_ifindex); |
aa767bfe | 762 | if (dev == NULL) |
1da177e4 LT |
763 | return -ENODEV; |
764 | ||
765 | /* Find qdisc */ | |
766 | if (!parent) { | |
af356afa | 767 | q = dev->qdisc; |
1da177e4 | 768 | parent = q->handle; |
aa767bfe SH |
769 | } else { |
770 | q = qdisc_lookup(dev, TC_H_MAJ(t->tcm_parent)); | |
771 | if (q == NULL) | |
772 | return -EINVAL; | |
773 | } | |
1da177e4 LT |
774 | |
775 | /* Is it classful? */ | |
cc7ec456 ED |
776 | cops = q->ops->cl_ops; |
777 | if (!cops) | |
1da177e4 LT |
778 | return -EINVAL; |
779 | ||
6529eaba | 780 | if (!cops->tcf_block) |
71ebe5e9 PM |
781 | return -EOPNOTSUPP; |
782 | ||
1da177e4 LT |
783 | /* Do we search for filter, attached to class? */ |
784 | if (TC_H_MIN(parent)) { | |
143976ce | 785 | cl = cops->find(q, parent); |
1da177e4 LT |
786 | if (cl == 0) |
787 | return -ENOENT; | |
788 | } | |
789 | ||
790 | /* And the last stroke */ | |
6529eaba JP |
791 | block = cops->tcf_block(q, cl); |
792 | if (!block) { | |
6bb16e7a | 793 | err = -EINVAL; |
1da177e4 | 794 | goto errout; |
6bb16e7a | 795 | } |
5bc17018 JP |
796 | |
797 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; | |
798 | if (chain_index > TC_ACT_EXT_VAL_MASK) { | |
799 | err = -EINVAL; | |
800 | goto errout; | |
801 | } | |
367a8ce8 WC |
802 | chain = tcf_chain_get(block, chain_index, |
803 | n->nlmsg_type == RTM_NEWTFILTER); | |
5bc17018 | 804 | if (!chain) { |
367a8ce8 | 805 | err = n->nlmsg_type == RTM_NEWTFILTER ? -ENOMEM : -EINVAL; |
5bc17018 JP |
806 | goto errout; |
807 | } | |
6529eaba | 808 | |
ea7f8277 | 809 | if (n->nlmsg_type == RTM_DELTFILTER && prio == 0) { |
a10fa201 JP |
810 | tfilter_notify_chain(net, skb, q, parent, n, |
811 | chain, RTM_DELTFILTER); | |
f93e1cdc | 812 | tcf_chain_flush(chain); |
ea7f8277 DB |
813 | err = 0; |
814 | goto errout; | |
815 | } | |
1da177e4 | 816 | |
2190d1d0 JP |
817 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
818 | prio, prio_allocate); | |
819 | if (IS_ERR(tp)) { | |
820 | err = PTR_ERR(tp); | |
821 | goto errout; | |
1da177e4 LT |
822 | } |
823 | ||
824 | if (tp == NULL) { | |
825 | /* Proto-tcf does not exist, create new one */ | |
826 | ||
6bb16e7a JP |
827 | if (tca[TCA_KIND] == NULL || !protocol) { |
828 | err = -EINVAL; | |
1da177e4 | 829 | goto errout; |
6bb16e7a | 830 | } |
1da177e4 | 831 | |
cc7ec456 | 832 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
833 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
834 | err = -ENOENT; | |
1da177e4 | 835 | goto errout; |
6bb16e7a | 836 | } |
1da177e4 | 837 | |
9d36d9e5 | 838 | if (prio_allocate) |
2190d1d0 | 839 | prio = tcf_auto_prio(tcf_chain_tp_prev(&chain_info)); |
1da177e4 | 840 | |
33a48927 | 841 | tp = tcf_proto_create(nla_data(tca[TCA_KIND]), |
5bc17018 | 842 | protocol, prio, parent, q, chain); |
33a48927 JP |
843 | if (IS_ERR(tp)) { |
844 | err = PTR_ERR(tp); | |
1da177e4 LT |
845 | goto errout; |
846 | } | |
12186be7 | 847 | tp_created = 1; |
6bb16e7a JP |
848 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
849 | err = -EINVAL; | |
1da177e4 | 850 | goto errout; |
6bb16e7a | 851 | } |
1da177e4 LT |
852 | |
853 | fh = tp->ops->get(tp, t->tcm_handle); | |
854 | ||
8113c095 | 855 | if (!fh) { |
1da177e4 | 856 | if (n->nlmsg_type == RTM_DELTFILTER && t->tcm_handle == 0) { |
2190d1d0 | 857 | tcf_chain_tp_remove(chain, &chain_info, tp); |
a10fa201 | 858 | tfilter_notify(net, skb, n, tp, q, parent, fh, |
fa59b27c | 859 | RTM_DELTFILTER, false); |
763dbf63 | 860 | tcf_proto_destroy(tp); |
1da177e4 LT |
861 | err = 0; |
862 | goto errout; | |
863 | } | |
864 | ||
aa767bfe | 865 | if (n->nlmsg_type != RTM_NEWTFILTER || |
6bb16e7a JP |
866 | !(n->nlmsg_flags & NLM_F_CREATE)) { |
867 | err = -ENOENT; | |
1da177e4 | 868 | goto errout; |
6bb16e7a | 869 | } |
1da177e4 | 870 | } else { |
763dbf63 WC |
871 | bool last; |
872 | ||
1da177e4 | 873 | switch (n->nlmsg_type) { |
10297b99 | 874 | case RTM_NEWTFILTER: |
12186be7 MU |
875 | if (n->nlmsg_flags & NLM_F_EXCL) { |
876 | if (tp_created) | |
763dbf63 | 877 | tcf_proto_destroy(tp); |
6bb16e7a | 878 | err = -EEXIST; |
1da177e4 | 879 | goto errout; |
12186be7 | 880 | } |
1da177e4 LT |
881 | break; |
882 | case RTM_DELTFILTER: | |
a10fa201 JP |
883 | err = tfilter_del_notify(net, skb, n, tp, q, parent, |
884 | fh, false, &last); | |
40c81b25 JP |
885 | if (err) |
886 | goto errout; | |
763dbf63 | 887 | if (last) { |
2190d1d0 | 888 | tcf_chain_tp_remove(chain, &chain_info, tp); |
763dbf63 WC |
889 | tcf_proto_destroy(tp); |
890 | } | |
d7cf52c2 | 891 | goto errout; |
1da177e4 | 892 | case RTM_GETTFILTER: |
a10fa201 | 893 | err = tfilter_notify(net, skb, n, tp, q, parent, fh, |
fa59b27c | 894 | RTM_NEWTFILTER, true); |
1da177e4 LT |
895 | goto errout; |
896 | default: | |
897 | err = -EINVAL; | |
898 | goto errout; | |
899 | } | |
900 | } | |
901 | ||
2f7ef2f8 CW |
902 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
903 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE); | |
12186be7 | 904 | if (err == 0) { |
2190d1d0 JP |
905 | if (tp_created) |
906 | tcf_chain_tp_insert(chain, &chain_info, tp); | |
a10fa201 JP |
907 | tfilter_notify(net, skb, n, tp, q, parent, fh, |
908 | RTM_NEWTFILTER, false); | |
12186be7 MU |
909 | } else { |
910 | if (tp_created) | |
763dbf63 | 911 | tcf_proto_destroy(tp); |
12186be7 | 912 | } |
1da177e4 LT |
913 | |
914 | errout: | |
5bc17018 JP |
915 | if (chain) |
916 | tcf_chain_put(chain); | |
1da177e4 LT |
917 | if (err == -EAGAIN) |
918 | /* Replay the request. */ | |
919 | goto replay; | |
920 | return err; | |
921 | } | |
922 | ||
aa767bfe | 923 | struct tcf_dump_args { |
1da177e4 LT |
924 | struct tcf_walker w; |
925 | struct sk_buff *skb; | |
926 | struct netlink_callback *cb; | |
a10fa201 JP |
927 | struct Qdisc *q; |
928 | u32 parent; | |
1da177e4 LT |
929 | }; |
930 | ||
8113c095 | 931 | static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) |
1da177e4 | 932 | { |
aa767bfe | 933 | struct tcf_dump_args *a = (void *)arg; |
832d1d5b | 934 | struct net *net = sock_net(a->skb->sk); |
1da177e4 | 935 | |
a10fa201 JP |
936 | return tcf_fill_node(net, a->skb, tp, a->q, a->parent, |
937 | n, NETLINK_CB(a->cb->skb).portid, | |
5a7a5555 JHS |
938 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
939 | RTM_NEWTFILTER); | |
1da177e4 LT |
940 | } |
941 | ||
a10fa201 JP |
942 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
943 | struct sk_buff *skb, struct netlink_callback *cb, | |
acb31fae JP |
944 | long index_start, long *p_index) |
945 | { | |
946 | struct net *net = sock_net(skb->sk); | |
947 | struct tcmsg *tcm = nlmsg_data(cb->nlh); | |
948 | struct tcf_dump_args arg; | |
949 | struct tcf_proto *tp; | |
950 | ||
951 | for (tp = rtnl_dereference(chain->filter_chain); | |
952 | tp; tp = rtnl_dereference(tp->next), (*p_index)++) { | |
953 | if (*p_index < index_start) | |
954 | continue; | |
955 | if (TC_H_MAJ(tcm->tcm_info) && | |
956 | TC_H_MAJ(tcm->tcm_info) != tp->prio) | |
957 | continue; | |
958 | if (TC_H_MIN(tcm->tcm_info) && | |
959 | TC_H_MIN(tcm->tcm_info) != tp->protocol) | |
960 | continue; | |
961 | if (*p_index > index_start) | |
962 | memset(&cb->args[1], 0, | |
963 | sizeof(cb->args) - sizeof(cb->args[0])); | |
964 | if (cb->args[1] == 0) { | |
a10fa201 | 965 | if (tcf_fill_node(net, skb, tp, q, parent, 0, |
acb31fae JP |
966 | NETLINK_CB(cb->skb).portid, |
967 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
968 | RTM_NEWTFILTER) <= 0) | |
5bc17018 | 969 | return false; |
acb31fae JP |
970 | |
971 | cb->args[1] = 1; | |
972 | } | |
973 | if (!tp->ops->walk) | |
974 | continue; | |
975 | arg.w.fn = tcf_node_dump; | |
976 | arg.skb = skb; | |
977 | arg.cb = cb; | |
a10fa201 JP |
978 | arg.q = q; |
979 | arg.parent = parent; | |
acb31fae JP |
980 | arg.w.stop = 0; |
981 | arg.w.skip = cb->args[1] - 1; | |
982 | arg.w.count = 0; | |
983 | tp->ops->walk(tp, &arg.w); | |
984 | cb->args[1] = arg.w.count + 1; | |
985 | if (arg.w.stop) | |
5bc17018 | 986 | return false; |
acb31fae | 987 | } |
5bc17018 | 988 | return true; |
acb31fae JP |
989 | } |
990 | ||
bd27a875 | 991 | /* called with RTNL */ |
1da177e4 LT |
992 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
993 | { | |
3b1e0a65 | 994 | struct net *net = sock_net(skb->sk); |
5bc17018 | 995 | struct nlattr *tca[TCA_MAX + 1]; |
1da177e4 LT |
996 | struct net_device *dev; |
997 | struct Qdisc *q; | |
6529eaba | 998 | struct tcf_block *block; |
2190d1d0 | 999 | struct tcf_chain *chain; |
942b8165 | 1000 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
1da177e4 | 1001 | unsigned long cl = 0; |
20fea08b | 1002 | const struct Qdisc_class_ops *cops; |
acb31fae JP |
1003 | long index_start; |
1004 | long index; | |
a10fa201 | 1005 | u32 parent; |
5bc17018 | 1006 | int err; |
1da177e4 | 1007 | |
573ce260 | 1008 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
1da177e4 | 1009 | return skb->len; |
5bc17018 JP |
1010 | |
1011 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, NULL); | |
1012 | if (err) | |
1013 | return err; | |
1014 | ||
cc7ec456 ED |
1015 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); |
1016 | if (!dev) | |
1da177e4 LT |
1017 | return skb->len; |
1018 | ||
a10fa201 JP |
1019 | parent = tcm->tcm_parent; |
1020 | if (!parent) { | |
af356afa | 1021 | q = dev->qdisc; |
a10fa201 JP |
1022 | parent = q->handle; |
1023 | } else { | |
1da177e4 | 1024 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); |
a10fa201 | 1025 | } |
1da177e4 LT |
1026 | if (!q) |
1027 | goto out; | |
cc7ec456 ED |
1028 | cops = q->ops->cl_ops; |
1029 | if (!cops) | |
143976ce | 1030 | goto out; |
6529eaba | 1031 | if (!cops->tcf_block) |
143976ce | 1032 | goto out; |
1da177e4 | 1033 | if (TC_H_MIN(tcm->tcm_parent)) { |
143976ce | 1034 | cl = cops->find(q, tcm->tcm_parent); |
1da177e4 | 1035 | if (cl == 0) |
143976ce | 1036 | goto out; |
1da177e4 | 1037 | } |
6529eaba JP |
1038 | block = cops->tcf_block(q, cl); |
1039 | if (!block) | |
143976ce | 1040 | goto out; |
1da177e4 | 1041 | |
acb31fae JP |
1042 | index_start = cb->args[0]; |
1043 | index = 0; | |
5bc17018 JP |
1044 | |
1045 | list_for_each_entry(chain, &block->chain_list, list) { | |
1046 | if (tca[TCA_CHAIN] && | |
1047 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) | |
1048 | continue; | |
a10fa201 JP |
1049 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
1050 | index_start, &index)) | |
5bc17018 JP |
1051 | break; |
1052 | } | |
1053 | ||
acb31fae | 1054 | cb->args[0] = index; |
1da177e4 | 1055 | |
1da177e4 | 1056 | out: |
1da177e4 LT |
1057 | return skb->len; |
1058 | } | |
1059 | ||
18d0264f | 1060 | void tcf_exts_destroy(struct tcf_exts *exts) |
1da177e4 LT |
1061 | { |
1062 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
1063 | LIST_HEAD(actions); |
1064 | ||
2d132eba | 1065 | ASSERT_RTNL(); |
22dc13c8 WC |
1066 | tcf_exts_to_list(exts, &actions); |
1067 | tcf_action_destroy(&actions, TCA_ACT_UNBIND); | |
1068 | kfree(exts->actions); | |
1069 | exts->nr_actions = 0; | |
1da177e4 LT |
1070 | #endif |
1071 | } | |
aa767bfe | 1072 | EXPORT_SYMBOL(tcf_exts_destroy); |
1da177e4 | 1073 | |
c1b52739 | 1074 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
5a7a5555 | 1075 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr) |
1da177e4 | 1076 | { |
1da177e4 LT |
1077 | #ifdef CONFIG_NET_CLS_ACT |
1078 | { | |
1da177e4 LT |
1079 | struct tc_action *act; |
1080 | ||
5da57f42 | 1081 | if (exts->police && tb[exts->police]) { |
9fb9f251 JP |
1082 | act = tcf_action_init_1(net, tp, tb[exts->police], |
1083 | rate_tlv, "police", ovr, | |
1084 | TCA_ACT_BIND); | |
ab27cfb8 PM |
1085 | if (IS_ERR(act)) |
1086 | return PTR_ERR(act); | |
1da177e4 | 1087 | |
33be6271 | 1088 | act->type = exts->type = TCA_OLD_COMPAT; |
22dc13c8 WC |
1089 | exts->actions[0] = act; |
1090 | exts->nr_actions = 1; | |
5da57f42 | 1091 | } else if (exts->action && tb[exts->action]) { |
22dc13c8 WC |
1092 | LIST_HEAD(actions); |
1093 | int err, i = 0; | |
1094 | ||
9fb9f251 JP |
1095 | err = tcf_action_init(net, tp, tb[exts->action], |
1096 | rate_tlv, NULL, ovr, TCA_ACT_BIND, | |
5a7a5555 | 1097 | &actions); |
33be6271 WC |
1098 | if (err) |
1099 | return err; | |
22dc13c8 WC |
1100 | list_for_each_entry(act, &actions, list) |
1101 | exts->actions[i++] = act; | |
1102 | exts->nr_actions = i; | |
1da177e4 LT |
1103 | } |
1104 | } | |
1da177e4 | 1105 | #else |
5da57f42 WC |
1106 | if ((exts->action && tb[exts->action]) || |
1107 | (exts->police && tb[exts->police])) | |
1da177e4 LT |
1108 | return -EOPNOTSUPP; |
1109 | #endif | |
1110 | ||
1111 | return 0; | |
1112 | } | |
aa767bfe | 1113 | EXPORT_SYMBOL(tcf_exts_validate); |
1da177e4 | 1114 | |
9b0d4446 | 1115 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
1da177e4 LT |
1116 | { |
1117 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
1118 | struct tcf_exts old = *dst; |
1119 | ||
9b0d4446 | 1120 | *dst = *src; |
22dc13c8 | 1121 | tcf_exts_destroy(&old); |
1da177e4 LT |
1122 | #endif |
1123 | } | |
aa767bfe | 1124 | EXPORT_SYMBOL(tcf_exts_change); |
1da177e4 | 1125 | |
22dc13c8 WC |
1126 | #ifdef CONFIG_NET_CLS_ACT |
1127 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) | |
1128 | { | |
1129 | if (exts->nr_actions == 0) | |
1130 | return NULL; | |
1131 | else | |
1132 | return exts->actions[0]; | |
1133 | } | |
1134 | #endif | |
33be6271 | 1135 | |
5da57f42 | 1136 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
1137 | { |
1138 | #ifdef CONFIG_NET_CLS_ACT | |
9cc63db5 CW |
1139 | struct nlattr *nest; |
1140 | ||
978dfd8d | 1141 | if (exts->action && tcf_exts_has_actions(exts)) { |
1da177e4 LT |
1142 | /* |
1143 | * again for backward compatible mode - we want | |
1144 | * to work with both old and new modes of entering | |
1145 | * tc data even if iproute2 was newer - jhs | |
1146 | */ | |
33be6271 | 1147 | if (exts->type != TCA_OLD_COMPAT) { |
22dc13c8 WC |
1148 | LIST_HEAD(actions); |
1149 | ||
5da57f42 | 1150 | nest = nla_nest_start(skb, exts->action); |
4b3550ef PM |
1151 | if (nest == NULL) |
1152 | goto nla_put_failure; | |
22dc13c8 WC |
1153 | |
1154 | tcf_exts_to_list(exts, &actions); | |
1155 | if (tcf_action_dump(skb, &actions, 0, 0) < 0) | |
add93b61 | 1156 | goto nla_put_failure; |
4b3550ef | 1157 | nla_nest_end(skb, nest); |
5da57f42 | 1158 | } else if (exts->police) { |
33be6271 | 1159 | struct tc_action *act = tcf_exts_first_act(exts); |
5da57f42 | 1160 | nest = nla_nest_start(skb, exts->police); |
63acd680 | 1161 | if (nest == NULL || !act) |
4b3550ef | 1162 | goto nla_put_failure; |
33be6271 | 1163 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
add93b61 | 1164 | goto nla_put_failure; |
4b3550ef | 1165 | nla_nest_end(skb, nest); |
1da177e4 LT |
1166 | } |
1167 | } | |
1da177e4 | 1168 | return 0; |
9cc63db5 CW |
1169 | |
1170 | nla_put_failure: | |
1171 | nla_nest_cancel(skb, nest); | |
1da177e4 | 1172 | return -1; |
9cc63db5 CW |
1173 | #else |
1174 | return 0; | |
1175 | #endif | |
1da177e4 | 1176 | } |
aa767bfe | 1177 | EXPORT_SYMBOL(tcf_exts_dump); |
1da177e4 | 1178 | |
aa767bfe | 1179 | |
5da57f42 | 1180 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
1181 | { |
1182 | #ifdef CONFIG_NET_CLS_ACT | |
33be6271 | 1183 | struct tc_action *a = tcf_exts_first_act(exts); |
b057df24 | 1184 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
33be6271 | 1185 | return -1; |
1da177e4 LT |
1186 | #endif |
1187 | return 0; | |
1da177e4 | 1188 | } |
aa767bfe | 1189 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
1da177e4 | 1190 | |
717503b9 JP |
1191 | static int tc_exts_setup_cb_egdev_call(struct tcf_exts *exts, |
1192 | enum tc_setup_type type, | |
1193 | void *type_data, bool err_stop) | |
b3f55bdd JP |
1194 | { |
1195 | int ok_count = 0; | |
1196 | #ifdef CONFIG_NET_CLS_ACT | |
1197 | const struct tc_action *a; | |
1198 | struct net_device *dev; | |
9d452ceb | 1199 | int i, ret; |
b3f55bdd JP |
1200 | |
1201 | if (!tcf_exts_has_actions(exts)) | |
1202 | return 0; | |
1203 | ||
9d452ceb OG |
1204 | for (i = 0; i < exts->nr_actions; i++) { |
1205 | a = exts->actions[i]; | |
b3f55bdd JP |
1206 | if (!a->ops->get_dev) |
1207 | continue; | |
1208 | dev = a->ops->get_dev(a); | |
1209 | if (!dev || !tc_can_offload(dev)) | |
1210 | continue; | |
1211 | ret = tc_setup_cb_egdev_call(dev, type, type_data, err_stop); | |
1212 | if (ret < 0) | |
1213 | return ret; | |
1214 | ok_count += ret; | |
1215 | } | |
1216 | #endif | |
1217 | return ok_count; | |
1218 | } | |
717503b9 | 1219 | |
208c0f4b JP |
1220 | int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, |
1221 | enum tc_setup_type type, void *type_data, bool err_stop) | |
717503b9 | 1222 | { |
208c0f4b JP |
1223 | int ok_count; |
1224 | int ret; | |
1225 | ||
1226 | ret = tcf_block_cb_call(block, type, type_data, err_stop); | |
1227 | if (ret < 0) | |
1228 | return ret; | |
1229 | ok_count = ret; | |
1230 | ||
1231 | if (!exts) | |
1232 | return ok_count; | |
1233 | ret = tc_exts_setup_cb_egdev_call(exts, type, type_data, err_stop); | |
1234 | if (ret < 0) | |
1235 | return ret; | |
1236 | ok_count += ret; | |
1237 | ||
1238 | return ok_count; | |
717503b9 JP |
1239 | } |
1240 | EXPORT_SYMBOL(tc_setup_cb_call); | |
b3f55bdd | 1241 | |
1da177e4 LT |
1242 | static int __init tc_filter_init(void) |
1243 | { | |
7aa0045d CW |
1244 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
1245 | if (!tc_filter_wq) | |
1246 | return -ENOMEM; | |
1247 | ||
b97bac64 FW |
1248 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_ctl_tfilter, NULL, 0); |
1249 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_ctl_tfilter, NULL, 0); | |
82623c0d | 1250 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_ctl_tfilter, |
b97bac64 | 1251 | tc_dump_tfilter, 0); |
1da177e4 | 1252 | |
1da177e4 LT |
1253 | return 0; |
1254 | } | |
1255 | ||
1256 | subsys_initcall(tc_filter_init); |