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