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