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