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