]>
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> |
7f76fa36 | 28 | #include <linux/rhashtable.h> |
b854272b DL |
29 | #include <net/net_namespace.h> |
30 | #include <net/sock.h> | |
dc5fc579 | 31 | #include <net/netlink.h> |
1da177e4 LT |
32 | #include <net/pkt_sched.h> |
33 | #include <net/pkt_cls.h> | |
e3ab786b | 34 | #include <net/tc_act/tc_pedit.h> |
3a7b6861 PNA |
35 | #include <net/tc_act/tc_mirred.h> |
36 | #include <net/tc_act/tc_vlan.h> | |
37 | #include <net/tc_act/tc_tunnel_key.h> | |
38 | #include <net/tc_act/tc_csum.h> | |
39 | #include <net/tc_act/tc_gact.h> | |
40 | #include <net/tc_act/tc_skbedit.h> | |
41 | #include <net/tc_act/tc_mirred.h> | |
1da177e4 | 42 | |
e331473f DC |
43 | extern const struct nla_policy rtm_tca_policy[TCA_MAX + 1]; |
44 | ||
1da177e4 | 45 | /* The list of all installed classifier types */ |
36272874 | 46 | static LIST_HEAD(tcf_proto_base); |
1da177e4 LT |
47 | |
48 | /* Protects list of registered TC modules. It is pure SMP lock. */ | |
49 | static DEFINE_RWLOCK(cls_mod_lock); | |
50 | ||
51 | /* Find classifier type by string name */ | |
52 | ||
f34e8bff | 53 | static const struct tcf_proto_ops *__tcf_proto_lookup_ops(const char *kind) |
1da177e4 | 54 | { |
dcd76081 | 55 | const struct tcf_proto_ops *t, *res = NULL; |
1da177e4 LT |
56 | |
57 | if (kind) { | |
58 | read_lock(&cls_mod_lock); | |
36272874 | 59 | list_for_each_entry(t, &tcf_proto_base, head) { |
33a48927 | 60 | if (strcmp(kind, t->kind) == 0) { |
dcd76081 ED |
61 | if (try_module_get(t->owner)) |
62 | res = t; | |
1da177e4 LT |
63 | break; |
64 | } | |
65 | } | |
66 | read_unlock(&cls_mod_lock); | |
67 | } | |
dcd76081 | 68 | return res; |
1da177e4 LT |
69 | } |
70 | ||
f34e8bff JP |
71 | static const struct tcf_proto_ops * |
72 | tcf_proto_lookup_ops(const char *kind, struct netlink_ext_ack *extack) | |
73 | { | |
74 | const struct tcf_proto_ops *ops; | |
75 | ||
76 | ops = __tcf_proto_lookup_ops(kind); | |
77 | if (ops) | |
78 | return ops; | |
79 | #ifdef CONFIG_MODULES | |
80 | rtnl_unlock(); | |
81 | request_module("cls_%s", kind); | |
82 | rtnl_lock(); | |
83 | ops = __tcf_proto_lookup_ops(kind); | |
84 | /* We dropped the RTNL semaphore in order to perform | |
85 | * the module load. So, even if we succeeded in loading | |
86 | * the module we have to replay the request. We indicate | |
87 | * this using -EAGAIN. | |
88 | */ | |
89 | if (ops) { | |
90 | module_put(ops->owner); | |
91 | return ERR_PTR(-EAGAIN); | |
92 | } | |
93 | #endif | |
94 | NL_SET_ERR_MSG(extack, "TC classifier not found"); | |
95 | return ERR_PTR(-ENOENT); | |
96 | } | |
97 | ||
1da177e4 LT |
98 | /* Register(unregister) new classifier type */ |
99 | ||
100 | int register_tcf_proto_ops(struct tcf_proto_ops *ops) | |
101 | { | |
36272874 | 102 | struct tcf_proto_ops *t; |
1da177e4 LT |
103 | int rc = -EEXIST; |
104 | ||
105 | write_lock(&cls_mod_lock); | |
36272874 | 106 | list_for_each_entry(t, &tcf_proto_base, head) |
1da177e4 LT |
107 | if (!strcmp(ops->kind, t->kind)) |
108 | goto out; | |
109 | ||
36272874 | 110 | list_add_tail(&ops->head, &tcf_proto_base); |
1da177e4 LT |
111 | rc = 0; |
112 | out: | |
113 | write_unlock(&cls_mod_lock); | |
114 | return rc; | |
115 | } | |
aa767bfe | 116 | EXPORT_SYMBOL(register_tcf_proto_ops); |
1da177e4 | 117 | |
7aa0045d CW |
118 | static struct workqueue_struct *tc_filter_wq; |
119 | ||
1da177e4 LT |
120 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops) |
121 | { | |
36272874 | 122 | struct tcf_proto_ops *t; |
1da177e4 LT |
123 | int rc = -ENOENT; |
124 | ||
c78e1746 DB |
125 | /* Wait for outstanding call_rcu()s, if any, from a |
126 | * tcf_proto_ops's destroy() handler. | |
127 | */ | |
128 | rcu_barrier(); | |
7aa0045d | 129 | flush_workqueue(tc_filter_wq); |
c78e1746 | 130 | |
1da177e4 | 131 | write_lock(&cls_mod_lock); |
dcd76081 ED |
132 | list_for_each_entry(t, &tcf_proto_base, head) { |
133 | if (t == ops) { | |
134 | list_del(&t->head); | |
135 | rc = 0; | |
1da177e4 | 136 | break; |
dcd76081 ED |
137 | } |
138 | } | |
1da177e4 LT |
139 | write_unlock(&cls_mod_lock); |
140 | return rc; | |
141 | } | |
aa767bfe | 142 | EXPORT_SYMBOL(unregister_tcf_proto_ops); |
1da177e4 | 143 | |
aaa908ff | 144 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func) |
7aa0045d | 145 | { |
aaa908ff CW |
146 | INIT_RCU_WORK(rwork, func); |
147 | return queue_rcu_work(tc_filter_wq, rwork); | |
7aa0045d CW |
148 | } |
149 | EXPORT_SYMBOL(tcf_queue_work); | |
150 | ||
1da177e4 LT |
151 | /* Select new prio value from the range, managed by kernel. */ |
152 | ||
aa767bfe | 153 | static inline u32 tcf_auto_prio(struct tcf_proto *tp) |
1da177e4 | 154 | { |
aa767bfe | 155 | u32 first = TC_H_MAKE(0xC0000000U, 0U); |
1da177e4 LT |
156 | |
157 | if (tp) | |
cc7ec456 | 158 | first = tp->prio - 1; |
1da177e4 | 159 | |
7961973a | 160 | return TC_H_MAJ(first); |
1da177e4 LT |
161 | } |
162 | ||
33a48927 | 163 | static struct tcf_proto *tcf_proto_create(const char *kind, u32 protocol, |
c35a4acc AA |
164 | u32 prio, struct tcf_chain *chain, |
165 | struct netlink_ext_ack *extack) | |
33a48927 JP |
166 | { |
167 | struct tcf_proto *tp; | |
168 | int err; | |
169 | ||
170 | tp = kzalloc(sizeof(*tp), GFP_KERNEL); | |
171 | if (!tp) | |
172 | return ERR_PTR(-ENOBUFS); | |
173 | ||
f34e8bff JP |
174 | tp->ops = tcf_proto_lookup_ops(kind, extack); |
175 | if (IS_ERR(tp->ops)) { | |
176 | err = PTR_ERR(tp->ops); | |
d68d75fd | 177 | goto errout; |
33a48927 JP |
178 | } |
179 | tp->classify = tp->ops->classify; | |
180 | tp->protocol = protocol; | |
181 | tp->prio = prio; | |
5bc17018 | 182 | tp->chain = chain; |
8b64678e | 183 | spin_lock_init(&tp->lock); |
4dbfa766 | 184 | refcount_set(&tp->refcnt, 1); |
33a48927 JP |
185 | |
186 | err = tp->ops->init(tp); | |
187 | if (err) { | |
188 | module_put(tp->ops->owner); | |
189 | goto errout; | |
190 | } | |
191 | return tp; | |
192 | ||
193 | errout: | |
194 | kfree(tp); | |
195 | return ERR_PTR(err); | |
196 | } | |
197 | ||
4dbfa766 VB |
198 | static void tcf_proto_get(struct tcf_proto *tp) |
199 | { | |
200 | refcount_inc(&tp->refcnt); | |
201 | } | |
202 | ||
203 | static void tcf_chain_put(struct tcf_chain *chain); | |
204 | ||
715df5ec JK |
205 | static void tcf_proto_destroy(struct tcf_proto *tp, |
206 | struct netlink_ext_ack *extack) | |
cf1facda | 207 | { |
715df5ec | 208 | tp->ops->destroy(tp, extack); |
4dbfa766 | 209 | tcf_chain_put(tp->chain); |
763dbf63 WC |
210 | module_put(tp->ops->owner); |
211 | kfree_rcu(tp, rcu); | |
cf1facda JP |
212 | } |
213 | ||
4dbfa766 VB |
214 | static void tcf_proto_put(struct tcf_proto *tp, |
215 | struct netlink_ext_ack *extack) | |
216 | { | |
217 | if (refcount_dec_and_test(&tp->refcnt)) | |
218 | tcf_proto_destroy(tp, extack); | |
219 | } | |
220 | ||
8b64678e VB |
221 | static int walker_noop(struct tcf_proto *tp, void *d, struct tcf_walker *arg) |
222 | { | |
223 | return -1; | |
224 | } | |
225 | ||
226 | static bool tcf_proto_is_empty(struct tcf_proto *tp) | |
227 | { | |
228 | struct tcf_walker walker = { .fn = walker_noop, }; | |
229 | ||
230 | if (tp->ops->walk) { | |
231 | tp->ops->walk(tp, &walker); | |
232 | return !walker.stop; | |
233 | } | |
234 | return true; | |
235 | } | |
236 | ||
237 | static bool tcf_proto_check_delete(struct tcf_proto *tp) | |
238 | { | |
239 | spin_lock(&tp->lock); | |
240 | if (tcf_proto_is_empty(tp)) | |
241 | tp->deleting = true; | |
242 | spin_unlock(&tp->lock); | |
243 | return tp->deleting; | |
244 | } | |
245 | ||
246 | static void tcf_proto_mark_delete(struct tcf_proto *tp) | |
247 | { | |
248 | spin_lock(&tp->lock); | |
249 | tp->deleting = true; | |
250 | spin_unlock(&tp->lock); | |
251 | } | |
252 | ||
253 | static bool tcf_proto_is_deleting(struct tcf_proto *tp) | |
254 | { | |
255 | bool deleting; | |
256 | ||
257 | spin_lock(&tp->lock); | |
258 | deleting = tp->deleting; | |
259 | spin_unlock(&tp->lock); | |
260 | ||
261 | return deleting; | |
262 | } | |
263 | ||
c266f64d VB |
264 | #define ASSERT_BLOCK_LOCKED(block) \ |
265 | lockdep_assert_held(&(block)->lock) | |
266 | ||
a9b19443 JP |
267 | struct tcf_filter_chain_list_item { |
268 | struct list_head list; | |
269 | tcf_chain_head_change_t *chain_head_change; | |
270 | void *chain_head_change_priv; | |
271 | }; | |
272 | ||
5bc17018 JP |
273 | static struct tcf_chain *tcf_chain_create(struct tcf_block *block, |
274 | u32 chain_index) | |
2190d1d0 | 275 | { |
5bc17018 JP |
276 | struct tcf_chain *chain; |
277 | ||
c266f64d VB |
278 | ASSERT_BLOCK_LOCKED(block); |
279 | ||
5bc17018 JP |
280 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); |
281 | if (!chain) | |
282 | return NULL; | |
283 | list_add_tail(&chain->list, &block->chain_list); | |
ed76f5ed | 284 | mutex_init(&chain->filter_chain_lock); |
5bc17018 JP |
285 | chain->block = block; |
286 | chain->index = chain_index; | |
e2ef7544 | 287 | chain->refcnt = 1; |
f71e0ca4 JP |
288 | if (!chain->index) |
289 | block->chain0.chain = chain; | |
5bc17018 | 290 | return chain; |
2190d1d0 JP |
291 | } |
292 | ||
a9b19443 JP |
293 | static void tcf_chain_head_change_item(struct tcf_filter_chain_list_item *item, |
294 | struct tcf_proto *tp_head) | |
295 | { | |
296 | if (item->chain_head_change) | |
297 | item->chain_head_change(tp_head, item->chain_head_change_priv); | |
298 | } | |
f71e0ca4 JP |
299 | |
300 | static void tcf_chain0_head_change(struct tcf_chain *chain, | |
301 | struct tcf_proto *tp_head) | |
c7eb7d72 | 302 | { |
a9b19443 | 303 | struct tcf_filter_chain_list_item *item; |
f71e0ca4 | 304 | struct tcf_block *block = chain->block; |
a9b19443 | 305 | |
f71e0ca4 JP |
306 | if (chain->index) |
307 | return; | |
165f0135 VB |
308 | |
309 | mutex_lock(&block->lock); | |
f71e0ca4 | 310 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) |
a9b19443 | 311 | tcf_chain_head_change_item(item, tp_head); |
165f0135 | 312 | mutex_unlock(&block->lock); |
c7eb7d72 JP |
313 | } |
314 | ||
c266f64d VB |
315 | /* Returns true if block can be safely freed. */ |
316 | ||
317 | static bool tcf_chain_detach(struct tcf_chain *chain) | |
f93e1cdc | 318 | { |
efbf7897 CW |
319 | struct tcf_block *block = chain->block; |
320 | ||
c266f64d VB |
321 | ASSERT_BLOCK_LOCKED(block); |
322 | ||
e2ef7544 | 323 | list_del(&chain->list); |
f71e0ca4 JP |
324 | if (!chain->index) |
325 | block->chain0.chain = NULL; | |
c266f64d VB |
326 | |
327 | if (list_empty(&block->chain_list) && | |
328 | refcount_read(&block->refcnt) == 0) | |
329 | return true; | |
330 | ||
331 | return false; | |
332 | } | |
333 | ||
334 | static void tcf_block_destroy(struct tcf_block *block) | |
335 | { | |
336 | mutex_destroy(&block->lock); | |
337 | kfree_rcu(block, rcu); | |
338 | } | |
339 | ||
340 | static void tcf_chain_destroy(struct tcf_chain *chain, bool free_block) | |
341 | { | |
342 | struct tcf_block *block = chain->block; | |
343 | ||
ed76f5ed | 344 | mutex_destroy(&chain->filter_chain_lock); |
e2ef7544 | 345 | kfree(chain); |
c266f64d VB |
346 | if (free_block) |
347 | tcf_block_destroy(block); | |
e2ef7544 | 348 | } |
744a4cf6 | 349 | |
e2ef7544 CW |
350 | static void tcf_chain_hold(struct tcf_chain *chain) |
351 | { | |
c266f64d VB |
352 | ASSERT_BLOCK_LOCKED(chain->block); |
353 | ||
e2ef7544 | 354 | ++chain->refcnt; |
2190d1d0 JP |
355 | } |
356 | ||
3d32f4c5 | 357 | static bool tcf_chain_held_by_acts_only(struct tcf_chain *chain) |
1f3ed383 | 358 | { |
c266f64d VB |
359 | ASSERT_BLOCK_LOCKED(chain->block); |
360 | ||
1f3ed383 | 361 | /* In case all the references are action references, this |
3d32f4c5 | 362 | * chain should not be shown to the user. |
1f3ed383 JP |
363 | */ |
364 | return chain->refcnt == chain->action_refcnt; | |
365 | } | |
366 | ||
32a4f5ec JP |
367 | static struct tcf_chain *tcf_chain_lookup(struct tcf_block *block, |
368 | u32 chain_index) | |
5bc17018 JP |
369 | { |
370 | struct tcf_chain *chain; | |
371 | ||
c266f64d VB |
372 | ASSERT_BLOCK_LOCKED(block); |
373 | ||
5bc17018 | 374 | list_for_each_entry(chain, &block->chain_list, list) { |
32a4f5ec | 375 | if (chain->index == chain_index) |
e2ef7544 | 376 | return chain; |
32a4f5ec JP |
377 | } |
378 | return NULL; | |
379 | } | |
380 | ||
381 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, | |
382 | u32 seq, u16 flags, int event, bool unicast); | |
383 | ||
53681407 JP |
384 | static struct tcf_chain *__tcf_chain_get(struct tcf_block *block, |
385 | u32 chain_index, bool create, | |
386 | bool by_act) | |
32a4f5ec | 387 | { |
c266f64d VB |
388 | struct tcf_chain *chain = NULL; |
389 | bool is_first_reference; | |
32a4f5ec | 390 | |
c266f64d VB |
391 | mutex_lock(&block->lock); |
392 | chain = tcf_chain_lookup(block, chain_index); | |
32a4f5ec JP |
393 | if (chain) { |
394 | tcf_chain_hold(chain); | |
53681407 JP |
395 | } else { |
396 | if (!create) | |
c266f64d | 397 | goto errout; |
53681407 JP |
398 | chain = tcf_chain_create(block, chain_index); |
399 | if (!chain) | |
c266f64d | 400 | goto errout; |
5bc17018 | 401 | } |
80532384 | 402 | |
53681407 JP |
403 | if (by_act) |
404 | ++chain->action_refcnt; | |
c266f64d VB |
405 | is_first_reference = chain->refcnt - chain->action_refcnt == 1; |
406 | mutex_unlock(&block->lock); | |
53681407 JP |
407 | |
408 | /* Send notification only in case we got the first | |
409 | * non-action reference. Until then, the chain acts only as | |
410 | * a placeholder for actions pointing to it and user ought | |
411 | * not know about them. | |
412 | */ | |
c266f64d | 413 | if (is_first_reference && !by_act) |
53681407 JP |
414 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
415 | RTM_NEWCHAIN, false); | |
416 | ||
32a4f5ec | 417 | return chain; |
c266f64d VB |
418 | |
419 | errout: | |
420 | mutex_unlock(&block->lock); | |
421 | return chain; | |
5bc17018 | 422 | } |
53681407 | 423 | |
290b1c8b JP |
424 | static struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, |
425 | bool create) | |
53681407 JP |
426 | { |
427 | return __tcf_chain_get(block, chain_index, create, false); | |
428 | } | |
5bc17018 | 429 | |
1f3ed383 JP |
430 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, u32 chain_index) |
431 | { | |
53681407 | 432 | return __tcf_chain_get(block, chain_index, true, true); |
1f3ed383 JP |
433 | } |
434 | EXPORT_SYMBOL(tcf_chain_get_by_act); | |
435 | ||
a5654820 VB |
436 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, |
437 | void *tmplt_priv); | |
438 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, | |
439 | void *tmplt_priv, u32 chain_index, | |
440 | struct tcf_block *block, struct sk_buff *oskb, | |
441 | u32 seq, u16 flags, bool unicast); | |
9f407f17 | 442 | |
91052fa1 VB |
443 | static void __tcf_chain_put(struct tcf_chain *chain, bool by_act, |
444 | bool explicitly_created) | |
5bc17018 | 445 | { |
c266f64d | 446 | struct tcf_block *block = chain->block; |
a5654820 | 447 | const struct tcf_proto_ops *tmplt_ops; |
c266f64d VB |
448 | bool is_last, free_block = false; |
449 | unsigned int refcnt; | |
a5654820 VB |
450 | void *tmplt_priv; |
451 | u32 chain_index; | |
c266f64d VB |
452 | |
453 | mutex_lock(&block->lock); | |
91052fa1 VB |
454 | if (explicitly_created) { |
455 | if (!chain->explicitly_created) { | |
456 | mutex_unlock(&block->lock); | |
457 | return; | |
458 | } | |
459 | chain->explicitly_created = false; | |
460 | } | |
461 | ||
53681407 JP |
462 | if (by_act) |
463 | chain->action_refcnt--; | |
c266f64d VB |
464 | |
465 | /* tc_chain_notify_delete can't be called while holding block lock. | |
466 | * However, when block is unlocked chain can be changed concurrently, so | |
467 | * save these to temporary variables. | |
468 | */ | |
469 | refcnt = --chain->refcnt; | |
470 | is_last = refcnt - chain->action_refcnt == 0; | |
a5654820 VB |
471 | tmplt_ops = chain->tmplt_ops; |
472 | tmplt_priv = chain->tmplt_priv; | |
473 | chain_index = chain->index; | |
474 | ||
c266f64d VB |
475 | if (refcnt == 0) |
476 | free_block = tcf_chain_detach(chain); | |
477 | mutex_unlock(&block->lock); | |
53681407 JP |
478 | |
479 | /* The last dropped non-action reference will trigger notification. */ | |
c266f64d | 480 | if (is_last && !by_act) |
a5654820 VB |
481 | tc_chain_notify_delete(tmplt_ops, tmplt_priv, chain_index, |
482 | block, NULL, 0, 0, false); | |
53681407 | 483 | |
c266f64d | 484 | if (refcnt == 0) { |
a5654820 | 485 | tc_chain_tmplt_del(tmplt_ops, tmplt_priv); |
c266f64d | 486 | tcf_chain_destroy(chain, free_block); |
32a4f5ec | 487 | } |
5bc17018 | 488 | } |
53681407 | 489 | |
290b1c8b | 490 | static void tcf_chain_put(struct tcf_chain *chain) |
53681407 | 491 | { |
91052fa1 | 492 | __tcf_chain_put(chain, false, false); |
53681407 | 493 | } |
5bc17018 | 494 | |
1f3ed383 JP |
495 | void tcf_chain_put_by_act(struct tcf_chain *chain) |
496 | { | |
91052fa1 | 497 | __tcf_chain_put(chain, true, false); |
1f3ed383 JP |
498 | } |
499 | EXPORT_SYMBOL(tcf_chain_put_by_act); | |
500 | ||
32a4f5ec JP |
501 | static void tcf_chain_put_explicitly_created(struct tcf_chain *chain) |
502 | { | |
91052fa1 | 503 | __tcf_chain_put(chain, false, true); |
32a4f5ec JP |
504 | } |
505 | ||
290b1c8b JP |
506 | static void tcf_chain_flush(struct tcf_chain *chain) |
507 | { | |
4dbfa766 | 508 | struct tcf_proto *tp, *tp_next; |
290b1c8b | 509 | |
ed76f5ed VB |
510 | mutex_lock(&chain->filter_chain_lock); |
511 | tp = tcf_chain_dereference(chain->filter_chain, chain); | |
4dbfa766 | 512 | RCU_INIT_POINTER(chain->filter_chain, NULL); |
290b1c8b | 513 | tcf_chain0_head_change(chain, NULL); |
ed76f5ed VB |
514 | mutex_unlock(&chain->filter_chain_lock); |
515 | ||
290b1c8b | 516 | while (tp) { |
4dbfa766 VB |
517 | tp_next = rcu_dereference_protected(tp->next, 1); |
518 | tcf_proto_put(tp, NULL); | |
519 | tp = tp_next; | |
290b1c8b JP |
520 | } |
521 | } | |
522 | ||
7f76fa36 JH |
523 | static struct tcf_block *tc_dev_ingress_block(struct net_device *dev) |
524 | { | |
525 | const struct Qdisc_class_ops *cops; | |
526 | struct Qdisc *qdisc; | |
527 | ||
528 | if (!dev_ingress_queue(dev)) | |
529 | return NULL; | |
530 | ||
531 | qdisc = dev_ingress_queue(dev)->qdisc_sleeping; | |
532 | if (!qdisc) | |
533 | return NULL; | |
534 | ||
535 | cops = qdisc->ops->cl_ops; | |
536 | if (!cops) | |
537 | return NULL; | |
538 | ||
539 | if (!cops->tcf_block) | |
540 | return NULL; | |
541 | ||
542 | return cops->tcf_block(qdisc, TC_H_MIN_INGRESS, NULL); | |
543 | } | |
544 | ||
545 | static struct rhashtable indr_setup_block_ht; | |
546 | ||
547 | struct tc_indr_block_dev { | |
548 | struct rhash_head ht_node; | |
549 | struct net_device *dev; | |
550 | unsigned int refcnt; | |
551 | struct list_head cb_list; | |
552 | struct tcf_block *block; | |
553 | }; | |
554 | ||
555 | struct tc_indr_block_cb { | |
556 | struct list_head list; | |
557 | void *cb_priv; | |
558 | tc_indr_block_bind_cb_t *cb; | |
559 | void *cb_ident; | |
560 | }; | |
561 | ||
562 | static const struct rhashtable_params tc_indr_setup_block_ht_params = { | |
563 | .key_offset = offsetof(struct tc_indr_block_dev, dev), | |
564 | .head_offset = offsetof(struct tc_indr_block_dev, ht_node), | |
565 | .key_len = sizeof(struct net_device *), | |
566 | }; | |
567 | ||
568 | static struct tc_indr_block_dev * | |
569 | tc_indr_block_dev_lookup(struct net_device *dev) | |
570 | { | |
571 | return rhashtable_lookup_fast(&indr_setup_block_ht, &dev, | |
572 | tc_indr_setup_block_ht_params); | |
573 | } | |
574 | ||
575 | static struct tc_indr_block_dev *tc_indr_block_dev_get(struct net_device *dev) | |
576 | { | |
577 | struct tc_indr_block_dev *indr_dev; | |
578 | ||
579 | indr_dev = tc_indr_block_dev_lookup(dev); | |
580 | if (indr_dev) | |
581 | goto inc_ref; | |
582 | ||
583 | indr_dev = kzalloc(sizeof(*indr_dev), GFP_KERNEL); | |
584 | if (!indr_dev) | |
585 | return NULL; | |
586 | ||
587 | INIT_LIST_HEAD(&indr_dev->cb_list); | |
588 | indr_dev->dev = dev; | |
589 | indr_dev->block = tc_dev_ingress_block(dev); | |
590 | if (rhashtable_insert_fast(&indr_setup_block_ht, &indr_dev->ht_node, | |
591 | tc_indr_setup_block_ht_params)) { | |
592 | kfree(indr_dev); | |
593 | return NULL; | |
594 | } | |
595 | ||
596 | inc_ref: | |
597 | indr_dev->refcnt++; | |
598 | return indr_dev; | |
599 | } | |
600 | ||
601 | static void tc_indr_block_dev_put(struct tc_indr_block_dev *indr_dev) | |
602 | { | |
603 | if (--indr_dev->refcnt) | |
604 | return; | |
605 | ||
606 | rhashtable_remove_fast(&indr_setup_block_ht, &indr_dev->ht_node, | |
607 | tc_indr_setup_block_ht_params); | |
608 | kfree(indr_dev); | |
609 | } | |
610 | ||
611 | static struct tc_indr_block_cb * | |
612 | tc_indr_block_cb_lookup(struct tc_indr_block_dev *indr_dev, | |
613 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
614 | { | |
615 | struct tc_indr_block_cb *indr_block_cb; | |
616 | ||
617 | list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list) | |
618 | if (indr_block_cb->cb == cb && | |
619 | indr_block_cb->cb_ident == cb_ident) | |
620 | return indr_block_cb; | |
621 | return NULL; | |
622 | } | |
623 | ||
624 | static struct tc_indr_block_cb * | |
625 | tc_indr_block_cb_add(struct tc_indr_block_dev *indr_dev, void *cb_priv, | |
626 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
627 | { | |
628 | struct tc_indr_block_cb *indr_block_cb; | |
629 | ||
630 | indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident); | |
631 | if (indr_block_cb) | |
632 | return ERR_PTR(-EEXIST); | |
633 | ||
634 | indr_block_cb = kzalloc(sizeof(*indr_block_cb), GFP_KERNEL); | |
635 | if (!indr_block_cb) | |
636 | return ERR_PTR(-ENOMEM); | |
637 | ||
638 | indr_block_cb->cb_priv = cb_priv; | |
639 | indr_block_cb->cb = cb; | |
640 | indr_block_cb->cb_ident = cb_ident; | |
641 | list_add(&indr_block_cb->list, &indr_dev->cb_list); | |
642 | ||
643 | return indr_block_cb; | |
644 | } | |
645 | ||
646 | static void tc_indr_block_cb_del(struct tc_indr_block_cb *indr_block_cb) | |
647 | { | |
648 | list_del(&indr_block_cb->list); | |
649 | kfree(indr_block_cb); | |
650 | } | |
651 | ||
652 | static void tc_indr_block_ing_cmd(struct tc_indr_block_dev *indr_dev, | |
653 | struct tc_indr_block_cb *indr_block_cb, | |
654 | enum tc_block_command command) | |
655 | { | |
656 | struct tc_block_offload bo = { | |
657 | .command = command, | |
658 | .binder_type = TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS, | |
659 | .block = indr_dev->block, | |
660 | }; | |
661 | ||
662 | if (!indr_dev->block) | |
663 | return; | |
664 | ||
665 | indr_block_cb->cb(indr_dev->dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK, | |
666 | &bo); | |
667 | } | |
668 | ||
669 | int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, | |
670 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
671 | { | |
672 | struct tc_indr_block_cb *indr_block_cb; | |
673 | struct tc_indr_block_dev *indr_dev; | |
674 | int err; | |
675 | ||
676 | indr_dev = tc_indr_block_dev_get(dev); | |
677 | if (!indr_dev) | |
678 | return -ENOMEM; | |
679 | ||
680 | indr_block_cb = tc_indr_block_cb_add(indr_dev, cb_priv, cb, cb_ident); | |
681 | err = PTR_ERR_OR_ZERO(indr_block_cb); | |
682 | if (err) | |
683 | goto err_dev_put; | |
684 | ||
685 | tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_BIND); | |
686 | return 0; | |
687 | ||
688 | err_dev_put: | |
689 | tc_indr_block_dev_put(indr_dev); | |
690 | return err; | |
691 | } | |
692 | EXPORT_SYMBOL_GPL(__tc_indr_block_cb_register); | |
693 | ||
694 | int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, | |
695 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
696 | { | |
697 | int err; | |
698 | ||
699 | rtnl_lock(); | |
700 | err = __tc_indr_block_cb_register(dev, cb_priv, cb, cb_ident); | |
701 | rtnl_unlock(); | |
702 | ||
703 | return err; | |
704 | } | |
705 | EXPORT_SYMBOL_GPL(tc_indr_block_cb_register); | |
706 | ||
707 | void __tc_indr_block_cb_unregister(struct net_device *dev, | |
708 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
709 | { | |
710 | struct tc_indr_block_cb *indr_block_cb; | |
711 | struct tc_indr_block_dev *indr_dev; | |
712 | ||
713 | indr_dev = tc_indr_block_dev_lookup(dev); | |
714 | if (!indr_dev) | |
715 | return; | |
716 | ||
717 | indr_block_cb = tc_indr_block_cb_lookup(indr_dev, cb, cb_ident); | |
718 | if (!indr_block_cb) | |
719 | return; | |
720 | ||
721 | /* Send unbind message if required to free any block cbs. */ | |
722 | tc_indr_block_ing_cmd(indr_dev, indr_block_cb, TC_BLOCK_UNBIND); | |
723 | tc_indr_block_cb_del(indr_block_cb); | |
724 | tc_indr_block_dev_put(indr_dev); | |
725 | } | |
726 | EXPORT_SYMBOL_GPL(__tc_indr_block_cb_unregister); | |
727 | ||
728 | void tc_indr_block_cb_unregister(struct net_device *dev, | |
729 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
730 | { | |
731 | rtnl_lock(); | |
732 | __tc_indr_block_cb_unregister(dev, cb, cb_ident); | |
733 | rtnl_unlock(); | |
734 | } | |
735 | EXPORT_SYMBOL_GPL(tc_indr_block_cb_unregister); | |
736 | ||
737 | static void tc_indr_block_call(struct tcf_block *block, struct net_device *dev, | |
738 | struct tcf_block_ext_info *ei, | |
739 | enum tc_block_command command, | |
740 | struct netlink_ext_ack *extack) | |
741 | { | |
742 | struct tc_indr_block_cb *indr_block_cb; | |
743 | struct tc_indr_block_dev *indr_dev; | |
744 | struct tc_block_offload bo = { | |
745 | .command = command, | |
746 | .binder_type = ei->binder_type, | |
747 | .block = block, | |
748 | .extack = extack, | |
749 | }; | |
750 | ||
751 | indr_dev = tc_indr_block_dev_lookup(dev); | |
752 | if (!indr_dev) | |
753 | return; | |
754 | ||
755 | indr_dev->block = command == TC_BLOCK_BIND ? block : NULL; | |
756 | ||
757 | list_for_each_entry(indr_block_cb, &indr_dev->cb_list, list) | |
758 | indr_block_cb->cb(dev, indr_block_cb->cb_priv, TC_SETUP_BLOCK, | |
759 | &bo); | |
760 | } | |
761 | ||
caa72601 JP |
762 | static bool tcf_block_offload_in_use(struct tcf_block *block) |
763 | { | |
764 | return block->offloadcnt; | |
765 | } | |
766 | ||
767 | static int tcf_block_offload_cmd(struct tcf_block *block, | |
768 | struct net_device *dev, | |
769 | struct tcf_block_ext_info *ei, | |
60513bd8 JH |
770 | enum tc_block_command command, |
771 | struct netlink_ext_ack *extack) | |
8c4083b3 | 772 | { |
8c4083b3 JP |
773 | struct tc_block_offload bo = {}; |
774 | ||
8c4083b3 JP |
775 | bo.command = command; |
776 | bo.binder_type = ei->binder_type; | |
777 | bo.block = block; | |
60513bd8 | 778 | bo.extack = extack; |
caa72601 | 779 | return dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_BLOCK, &bo); |
8c4083b3 JP |
780 | } |
781 | ||
caa72601 | 782 | static int tcf_block_offload_bind(struct tcf_block *block, struct Qdisc *q, |
60513bd8 JH |
783 | struct tcf_block_ext_info *ei, |
784 | struct netlink_ext_ack *extack) | |
8c4083b3 | 785 | { |
caa72601 JP |
786 | struct net_device *dev = q->dev_queue->dev; |
787 | int err; | |
788 | ||
789 | if (!dev->netdev_ops->ndo_setup_tc) | |
790 | goto no_offload_dev_inc; | |
791 | ||
792 | /* If tc offload feature is disabled and the block we try to bind | |
793 | * to already has some offloaded filters, forbid to bind. | |
794 | */ | |
60513bd8 JH |
795 | if (!tc_can_offload(dev) && tcf_block_offload_in_use(block)) { |
796 | NL_SET_ERR_MSG(extack, "Bind to offloaded block failed as dev has offload disabled"); | |
caa72601 | 797 | return -EOPNOTSUPP; |
60513bd8 | 798 | } |
caa72601 | 799 | |
60513bd8 | 800 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_BIND, extack); |
caa72601 JP |
801 | if (err == -EOPNOTSUPP) |
802 | goto no_offload_dev_inc; | |
7f76fa36 JH |
803 | if (err) |
804 | return err; | |
805 | ||
806 | tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack); | |
807 | return 0; | |
caa72601 JP |
808 | |
809 | no_offload_dev_inc: | |
810 | if (tcf_block_offload_in_use(block)) | |
811 | return -EOPNOTSUPP; | |
812 | block->nooffloaddevcnt++; | |
7f76fa36 | 813 | tc_indr_block_call(block, dev, ei, TC_BLOCK_BIND, extack); |
caa72601 | 814 | return 0; |
8c4083b3 JP |
815 | } |
816 | ||
817 | static void tcf_block_offload_unbind(struct tcf_block *block, struct Qdisc *q, | |
818 | struct tcf_block_ext_info *ei) | |
819 | { | |
caa72601 JP |
820 | struct net_device *dev = q->dev_queue->dev; |
821 | int err; | |
822 | ||
7f76fa36 JH |
823 | tc_indr_block_call(block, dev, ei, TC_BLOCK_UNBIND, NULL); |
824 | ||
caa72601 JP |
825 | if (!dev->netdev_ops->ndo_setup_tc) |
826 | goto no_offload_dev_dec; | |
60513bd8 | 827 | err = tcf_block_offload_cmd(block, dev, ei, TC_BLOCK_UNBIND, NULL); |
caa72601 JP |
828 | if (err == -EOPNOTSUPP) |
829 | goto no_offload_dev_dec; | |
830 | return; | |
831 | ||
832 | no_offload_dev_dec: | |
833 | WARN_ON(block->nooffloaddevcnt-- == 0); | |
8c4083b3 JP |
834 | } |
835 | ||
a9b19443 | 836 | static int |
f71e0ca4 JP |
837 | tcf_chain0_head_change_cb_add(struct tcf_block *block, |
838 | struct tcf_block_ext_info *ei, | |
839 | struct netlink_ext_ack *extack) | |
a9b19443 JP |
840 | { |
841 | struct tcf_filter_chain_list_item *item; | |
165f0135 | 842 | struct tcf_chain *chain0; |
a9b19443 JP |
843 | |
844 | item = kmalloc(sizeof(*item), GFP_KERNEL); | |
845 | if (!item) { | |
846 | NL_SET_ERR_MSG(extack, "Memory allocation for head change callback item failed"); | |
847 | return -ENOMEM; | |
848 | } | |
849 | item->chain_head_change = ei->chain_head_change; | |
850 | item->chain_head_change_priv = ei->chain_head_change_priv; | |
165f0135 VB |
851 | |
852 | mutex_lock(&block->lock); | |
853 | chain0 = block->chain0.chain; | |
ed76f5ed VB |
854 | if (chain0) |
855 | tcf_chain_hold(chain0); | |
856 | else | |
857 | list_add(&item->list, &block->chain0.filter_chain_list); | |
165f0135 VB |
858 | mutex_unlock(&block->lock); |
859 | ||
ed76f5ed VB |
860 | if (chain0) { |
861 | struct tcf_proto *tp_head; | |
862 | ||
863 | mutex_lock(&chain0->filter_chain_lock); | |
864 | ||
865 | tp_head = tcf_chain_dereference(chain0->filter_chain, chain0); | |
866 | if (tp_head) | |
867 | tcf_chain_head_change_item(item, tp_head); | |
868 | ||
869 | mutex_lock(&block->lock); | |
870 | list_add(&item->list, &block->chain0.filter_chain_list); | |
871 | mutex_unlock(&block->lock); | |
872 | ||
873 | mutex_unlock(&chain0->filter_chain_lock); | |
874 | tcf_chain_put(chain0); | |
875 | } | |
876 | ||
a9b19443 JP |
877 | return 0; |
878 | } | |
879 | ||
880 | static void | |
f71e0ca4 JP |
881 | tcf_chain0_head_change_cb_del(struct tcf_block *block, |
882 | struct tcf_block_ext_info *ei) | |
a9b19443 JP |
883 | { |
884 | struct tcf_filter_chain_list_item *item; | |
885 | ||
165f0135 | 886 | mutex_lock(&block->lock); |
f71e0ca4 | 887 | list_for_each_entry(item, &block->chain0.filter_chain_list, list) { |
a9b19443 JP |
888 | if ((!ei->chain_head_change && !ei->chain_head_change_priv) || |
889 | (item->chain_head_change == ei->chain_head_change && | |
890 | item->chain_head_change_priv == ei->chain_head_change_priv)) { | |
165f0135 | 891 | if (block->chain0.chain) |
f71e0ca4 | 892 | tcf_chain_head_change_item(item, NULL); |
a9b19443 | 893 | list_del(&item->list); |
165f0135 VB |
894 | mutex_unlock(&block->lock); |
895 | ||
a9b19443 JP |
896 | kfree(item); |
897 | return; | |
898 | } | |
899 | } | |
165f0135 | 900 | mutex_unlock(&block->lock); |
a9b19443 JP |
901 | WARN_ON(1); |
902 | } | |
903 | ||
48617387 | 904 | struct tcf_net { |
ab281629 | 905 | spinlock_t idr_lock; /* Protects idr */ |
48617387 JP |
906 | struct idr idr; |
907 | }; | |
908 | ||
909 | static unsigned int tcf_net_id; | |
910 | ||
911 | static int tcf_block_insert(struct tcf_block *block, struct net *net, | |
bb047ddd | 912 | struct netlink_ext_ack *extack) |
a9b19443 | 913 | { |
48617387 | 914 | struct tcf_net *tn = net_generic(net, tcf_net_id); |
ab281629 VB |
915 | int err; |
916 | ||
917 | idr_preload(GFP_KERNEL); | |
918 | spin_lock(&tn->idr_lock); | |
919 | err = idr_alloc_u32(&tn->idr, block, &block->index, block->index, | |
920 | GFP_NOWAIT); | |
921 | spin_unlock(&tn->idr_lock); | |
922 | idr_preload_end(); | |
48617387 | 923 | |
ab281629 | 924 | return err; |
a9b19443 JP |
925 | } |
926 | ||
48617387 JP |
927 | static void tcf_block_remove(struct tcf_block *block, struct net *net) |
928 | { | |
929 | struct tcf_net *tn = net_generic(net, tcf_net_id); | |
930 | ||
ab281629 | 931 | spin_lock(&tn->idr_lock); |
9c160941 | 932 | idr_remove(&tn->idr, block->index); |
ab281629 | 933 | spin_unlock(&tn->idr_lock); |
48617387 JP |
934 | } |
935 | ||
936 | static struct tcf_block *tcf_block_create(struct net *net, struct Qdisc *q, | |
bb047ddd | 937 | u32 block_index, |
48617387 | 938 | struct netlink_ext_ack *extack) |
6529eaba | 939 | { |
48617387 | 940 | struct tcf_block *block; |
6529eaba | 941 | |
48617387 | 942 | block = kzalloc(sizeof(*block), GFP_KERNEL); |
8d1a77f9 AA |
943 | if (!block) { |
944 | NL_SET_ERR_MSG(extack, "Memory allocation for block failed"); | |
48617387 | 945 | return ERR_PTR(-ENOMEM); |
8d1a77f9 | 946 | } |
c266f64d | 947 | mutex_init(&block->lock); |
5bc17018 | 948 | INIT_LIST_HEAD(&block->chain_list); |
acb67442 | 949 | INIT_LIST_HEAD(&block->cb_list); |
f36fe1c4 | 950 | INIT_LIST_HEAD(&block->owner_list); |
f71e0ca4 | 951 | INIT_LIST_HEAD(&block->chain0.filter_chain_list); |
acb67442 | 952 | |
cfebd7e2 | 953 | refcount_set(&block->refcnt, 1); |
48617387 | 954 | block->net = net; |
bb047ddd JP |
955 | block->index = block_index; |
956 | ||
957 | /* Don't store q pointer for blocks which are shared */ | |
958 | if (!tcf_block_shared(block)) | |
959 | block->q = q; | |
48617387 | 960 | return block; |
48617387 JP |
961 | } |
962 | ||
963 | static struct tcf_block *tcf_block_lookup(struct net *net, u32 block_index) | |
964 | { | |
965 | struct tcf_net *tn = net_generic(net, tcf_net_id); | |
966 | ||
322d884b | 967 | return idr_find(&tn->idr, block_index); |
48617387 JP |
968 | } |
969 | ||
0607e439 VB |
970 | static struct tcf_block *tcf_block_refcnt_get(struct net *net, u32 block_index) |
971 | { | |
972 | struct tcf_block *block; | |
973 | ||
974 | rcu_read_lock(); | |
975 | block = tcf_block_lookup(net, block_index); | |
976 | if (block && !refcount_inc_not_zero(&block->refcnt)) | |
977 | block = NULL; | |
978 | rcu_read_unlock(); | |
979 | ||
980 | return block; | |
981 | } | |
982 | ||
bbf73830 VB |
983 | static struct tcf_chain * |
984 | __tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) | |
f0023436 | 985 | { |
bbf73830 VB |
986 | mutex_lock(&block->lock); |
987 | if (chain) | |
988 | chain = list_is_last(&chain->list, &block->chain_list) ? | |
989 | NULL : list_next_entry(chain, list); | |
990 | else | |
991 | chain = list_first_entry_or_null(&block->chain_list, | |
992 | struct tcf_chain, list); | |
f0023436 | 993 | |
bbf73830 VB |
994 | /* skip all action-only chains */ |
995 | while (chain && tcf_chain_held_by_acts_only(chain)) | |
996 | chain = list_is_last(&chain->list, &block->chain_list) ? | |
997 | NULL : list_next_entry(chain, list); | |
998 | ||
999 | if (chain) | |
f0023436 | 1000 | tcf_chain_hold(chain); |
bbf73830 | 1001 | mutex_unlock(&block->lock); |
f0023436 | 1002 | |
bbf73830 | 1003 | return chain; |
f0023436 VB |
1004 | } |
1005 | ||
bbf73830 VB |
1006 | /* Function to be used by all clients that want to iterate over all chains on |
1007 | * block. It properly obtains block->lock and takes reference to chain before | |
1008 | * returning it. Users of this function must be tolerant to concurrent chain | |
1009 | * insertion/deletion or ensure that no concurrent chain modification is | |
1010 | * possible. Note that all netlink dump callbacks cannot guarantee to provide | |
1011 | * consistent dump because rtnl lock is released each time skb is filled with | |
1012 | * data and sent to user-space. | |
1013 | */ | |
1014 | ||
1015 | struct tcf_chain * | |
1016 | tcf_get_next_chain(struct tcf_block *block, struct tcf_chain *chain) | |
f0023436 | 1017 | { |
bbf73830 | 1018 | struct tcf_chain *chain_next = __tcf_get_next_chain(block, chain); |
f0023436 | 1019 | |
bbf73830 | 1020 | if (chain) |
f0023436 | 1021 | tcf_chain_put(chain); |
bbf73830 VB |
1022 | |
1023 | return chain_next; | |
1024 | } | |
1025 | EXPORT_SYMBOL(tcf_get_next_chain); | |
1026 | ||
fe2923af VB |
1027 | static struct tcf_proto * |
1028 | __tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) | |
1029 | { | |
8b64678e VB |
1030 | u32 prio = 0; |
1031 | ||
fe2923af VB |
1032 | ASSERT_RTNL(); |
1033 | mutex_lock(&chain->filter_chain_lock); | |
1034 | ||
8b64678e | 1035 | if (!tp) { |
fe2923af | 1036 | tp = tcf_chain_dereference(chain->filter_chain, chain); |
8b64678e VB |
1037 | } else if (tcf_proto_is_deleting(tp)) { |
1038 | /* 'deleting' flag is set and chain->filter_chain_lock was | |
1039 | * unlocked, which means next pointer could be invalid. Restart | |
1040 | * search. | |
1041 | */ | |
1042 | prio = tp->prio + 1; | |
1043 | tp = tcf_chain_dereference(chain->filter_chain, chain); | |
1044 | ||
1045 | for (; tp; tp = tcf_chain_dereference(tp->next, chain)) | |
1046 | if (!tp->deleting && tp->prio >= prio) | |
1047 | break; | |
1048 | } else { | |
fe2923af | 1049 | tp = tcf_chain_dereference(tp->next, chain); |
8b64678e | 1050 | } |
fe2923af VB |
1051 | |
1052 | if (tp) | |
1053 | tcf_proto_get(tp); | |
1054 | ||
1055 | mutex_unlock(&chain->filter_chain_lock); | |
1056 | ||
1057 | return tp; | |
1058 | } | |
1059 | ||
1060 | /* Function to be used by all clients that want to iterate over all tp's on | |
1061 | * chain. Users of this function must be tolerant to concurrent tp | |
1062 | * insertion/deletion or ensure that no concurrent chain modification is | |
1063 | * possible. Note that all netlink dump callbacks cannot guarantee to provide | |
1064 | * consistent dump because rtnl lock is released each time skb is filled with | |
1065 | * data and sent to user-space. | |
1066 | */ | |
1067 | ||
1068 | struct tcf_proto * | |
1069 | tcf_get_next_proto(struct tcf_chain *chain, struct tcf_proto *tp) | |
1070 | { | |
1071 | struct tcf_proto *tp_next = __tcf_get_next_proto(chain, tp); | |
1072 | ||
1073 | if (tp) | |
1074 | tcf_proto_put(tp, NULL); | |
1075 | ||
1076 | return tp_next; | |
1077 | } | |
1078 | EXPORT_SYMBOL(tcf_get_next_proto); | |
1079 | ||
bbf73830 VB |
1080 | static void tcf_block_flush_all_chains(struct tcf_block *block) |
1081 | { | |
1082 | struct tcf_chain *chain; | |
1083 | ||
1084 | /* Last reference to block. At this point chains cannot be added or | |
1085 | * removed concurrently. | |
1086 | */ | |
1087 | for (chain = tcf_get_next_chain(block, NULL); | |
1088 | chain; | |
1089 | chain = tcf_get_next_chain(block, chain)) { | |
1090 | tcf_chain_put_explicitly_created(chain); | |
1091 | tcf_chain_flush(chain); | |
f0023436 VB |
1092 | } |
1093 | } | |
1094 | ||
0607e439 VB |
1095 | static void __tcf_block_put(struct tcf_block *block, struct Qdisc *q, |
1096 | struct tcf_block_ext_info *ei) | |
1097 | { | |
c266f64d | 1098 | if (refcount_dec_and_mutex_lock(&block->refcnt, &block->lock)) { |
0607e439 VB |
1099 | /* Flushing/putting all chains will cause the block to be |
1100 | * deallocated when last chain is freed. However, if chain_list | |
1101 | * is empty, block has to be manually deallocated. After block | |
1102 | * reference counter reached 0, it is no longer possible to | |
1103 | * increment it or add new chains to block. | |
1104 | */ | |
1105 | bool free_block = list_empty(&block->chain_list); | |
1106 | ||
c266f64d | 1107 | mutex_unlock(&block->lock); |
0607e439 VB |
1108 | if (tcf_block_shared(block)) |
1109 | tcf_block_remove(block, block->net); | |
0607e439 VB |
1110 | |
1111 | if (q) | |
1112 | tcf_block_offload_unbind(block, q, ei); | |
1113 | ||
1114 | if (free_block) | |
c266f64d | 1115 | tcf_block_destroy(block); |
0607e439 | 1116 | else |
bbf73830 | 1117 | tcf_block_flush_all_chains(block); |
0607e439 VB |
1118 | } else if (q) { |
1119 | tcf_block_offload_unbind(block, q, ei); | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | static void tcf_block_refcnt_put(struct tcf_block *block) | |
1124 | { | |
1125 | __tcf_block_put(block, NULL, NULL); | |
1126 | } | |
1127 | ||
c431f89b VB |
1128 | /* Find tcf block. |
1129 | * Set q, parent, cl when appropriate. | |
1130 | */ | |
1131 | ||
1132 | static struct tcf_block *tcf_block_find(struct net *net, struct Qdisc **q, | |
1133 | u32 *parent, unsigned long *cl, | |
1134 | int ifindex, u32 block_index, | |
1135 | struct netlink_ext_ack *extack) | |
1136 | { | |
1137 | struct tcf_block *block; | |
e368fdb6 | 1138 | int err = 0; |
c431f89b VB |
1139 | |
1140 | if (ifindex == TCM_IFINDEX_MAGIC_BLOCK) { | |
787ce6d0 | 1141 | block = tcf_block_refcnt_get(net, block_index); |
c431f89b VB |
1142 | if (!block) { |
1143 | NL_SET_ERR_MSG(extack, "Block of given index was not found"); | |
1144 | return ERR_PTR(-EINVAL); | |
1145 | } | |
1146 | } else { | |
1147 | const struct Qdisc_class_ops *cops; | |
1148 | struct net_device *dev; | |
1149 | ||
e368fdb6 VB |
1150 | rcu_read_lock(); |
1151 | ||
c431f89b | 1152 | /* Find link */ |
e368fdb6 VB |
1153 | dev = dev_get_by_index_rcu(net, ifindex); |
1154 | if (!dev) { | |
1155 | rcu_read_unlock(); | |
c431f89b | 1156 | return ERR_PTR(-ENODEV); |
e368fdb6 | 1157 | } |
c431f89b VB |
1158 | |
1159 | /* Find qdisc */ | |
1160 | if (!*parent) { | |
1161 | *q = dev->qdisc; | |
1162 | *parent = (*q)->handle; | |
1163 | } else { | |
e368fdb6 | 1164 | *q = qdisc_lookup_rcu(dev, TC_H_MAJ(*parent)); |
c431f89b VB |
1165 | if (!*q) { |
1166 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); | |
e368fdb6 VB |
1167 | err = -EINVAL; |
1168 | goto errout_rcu; | |
c431f89b VB |
1169 | } |
1170 | } | |
1171 | ||
e368fdb6 VB |
1172 | *q = qdisc_refcount_inc_nz(*q); |
1173 | if (!*q) { | |
1174 | NL_SET_ERR_MSG(extack, "Parent Qdisc doesn't exists"); | |
1175 | err = -EINVAL; | |
1176 | goto errout_rcu; | |
1177 | } | |
1178 | ||
c431f89b VB |
1179 | /* Is it classful? */ |
1180 | cops = (*q)->ops->cl_ops; | |
1181 | if (!cops) { | |
1182 | NL_SET_ERR_MSG(extack, "Qdisc not classful"); | |
e368fdb6 VB |
1183 | err = -EINVAL; |
1184 | goto errout_rcu; | |
c431f89b VB |
1185 | } |
1186 | ||
1187 | if (!cops->tcf_block) { | |
1188 | NL_SET_ERR_MSG(extack, "Class doesn't support blocks"); | |
e368fdb6 VB |
1189 | err = -EOPNOTSUPP; |
1190 | goto errout_rcu; | |
c431f89b VB |
1191 | } |
1192 | ||
e368fdb6 VB |
1193 | /* At this point we know that qdisc is not noop_qdisc, |
1194 | * which means that qdisc holds a reference to net_device | |
1195 | * and we hold a reference to qdisc, so it is safe to release | |
1196 | * rcu read lock. | |
1197 | */ | |
1198 | rcu_read_unlock(); | |
1199 | ||
c431f89b VB |
1200 | /* Do we search for filter, attached to class? */ |
1201 | if (TC_H_MIN(*parent)) { | |
1202 | *cl = cops->find(*q, *parent); | |
1203 | if (*cl == 0) { | |
1204 | NL_SET_ERR_MSG(extack, "Specified class doesn't exist"); | |
e368fdb6 VB |
1205 | err = -ENOENT; |
1206 | goto errout_qdisc; | |
c431f89b VB |
1207 | } |
1208 | } | |
1209 | ||
1210 | /* And the last stroke */ | |
1211 | block = cops->tcf_block(*q, *cl, extack); | |
e368fdb6 VB |
1212 | if (!block) { |
1213 | err = -EINVAL; | |
1214 | goto errout_qdisc; | |
1215 | } | |
c431f89b VB |
1216 | if (tcf_block_shared(block)) { |
1217 | NL_SET_ERR_MSG(extack, "This filter block is shared. Please use the block index to manipulate the filters"); | |
e368fdb6 VB |
1218 | err = -EOPNOTSUPP; |
1219 | goto errout_qdisc; | |
c431f89b | 1220 | } |
787ce6d0 VB |
1221 | |
1222 | /* Always take reference to block in order to support execution | |
1223 | * of rules update path of cls API without rtnl lock. Caller | |
1224 | * must release block when it is finished using it. 'if' block | |
1225 | * of this conditional obtain reference to block by calling | |
1226 | * tcf_block_refcnt_get(). | |
1227 | */ | |
1228 | refcount_inc(&block->refcnt); | |
c431f89b VB |
1229 | } |
1230 | ||
1231 | return block; | |
e368fdb6 VB |
1232 | |
1233 | errout_rcu: | |
1234 | rcu_read_unlock(); | |
1235 | errout_qdisc: | |
460b3601 | 1236 | if (*q) { |
e368fdb6 | 1237 | qdisc_put(*q); |
460b3601 CW |
1238 | *q = NULL; |
1239 | } | |
e368fdb6 VB |
1240 | return ERR_PTR(err); |
1241 | } | |
1242 | ||
1243 | static void tcf_block_release(struct Qdisc *q, struct tcf_block *block) | |
1244 | { | |
787ce6d0 VB |
1245 | if (!IS_ERR_OR_NULL(block)) |
1246 | tcf_block_refcnt_put(block); | |
1247 | ||
e368fdb6 VB |
1248 | if (q) |
1249 | qdisc_put(q); | |
c431f89b VB |
1250 | } |
1251 | ||
f36fe1c4 JP |
1252 | struct tcf_block_owner_item { |
1253 | struct list_head list; | |
1254 | struct Qdisc *q; | |
1255 | enum tcf_block_binder_type binder_type; | |
1256 | }; | |
1257 | ||
1258 | static void | |
1259 | tcf_block_owner_netif_keep_dst(struct tcf_block *block, | |
1260 | struct Qdisc *q, | |
1261 | enum tcf_block_binder_type binder_type) | |
1262 | { | |
1263 | if (block->keep_dst && | |
1264 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS && | |
1265 | binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) | |
1266 | netif_keep_dst(qdisc_dev(q)); | |
1267 | } | |
1268 | ||
1269 | void tcf_block_netif_keep_dst(struct tcf_block *block) | |
1270 | { | |
1271 | struct tcf_block_owner_item *item; | |
1272 | ||
1273 | block->keep_dst = true; | |
1274 | list_for_each_entry(item, &block->owner_list, list) | |
1275 | tcf_block_owner_netif_keep_dst(block, item->q, | |
1276 | item->binder_type); | |
1277 | } | |
1278 | EXPORT_SYMBOL(tcf_block_netif_keep_dst); | |
1279 | ||
1280 | static int tcf_block_owner_add(struct tcf_block *block, | |
1281 | struct Qdisc *q, | |
1282 | enum tcf_block_binder_type binder_type) | |
1283 | { | |
1284 | struct tcf_block_owner_item *item; | |
1285 | ||
1286 | item = kmalloc(sizeof(*item), GFP_KERNEL); | |
1287 | if (!item) | |
1288 | return -ENOMEM; | |
1289 | item->q = q; | |
1290 | item->binder_type = binder_type; | |
1291 | list_add(&item->list, &block->owner_list); | |
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | static void tcf_block_owner_del(struct tcf_block *block, | |
1296 | struct Qdisc *q, | |
1297 | enum tcf_block_binder_type binder_type) | |
1298 | { | |
1299 | struct tcf_block_owner_item *item; | |
1300 | ||
1301 | list_for_each_entry(item, &block->owner_list, list) { | |
1302 | if (item->q == q && item->binder_type == binder_type) { | |
1303 | list_del(&item->list); | |
1304 | kfree(item); | |
1305 | return; | |
1306 | } | |
1307 | } | |
1308 | WARN_ON(1); | |
1309 | } | |
1310 | ||
48617387 JP |
1311 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
1312 | struct tcf_block_ext_info *ei, | |
1313 | struct netlink_ext_ack *extack) | |
1314 | { | |
1315 | struct net *net = qdisc_net(q); | |
1316 | struct tcf_block *block = NULL; | |
48617387 JP |
1317 | int err; |
1318 | ||
787ce6d0 | 1319 | if (ei->block_index) |
48617387 | 1320 | /* block_index not 0 means the shared block is requested */ |
787ce6d0 | 1321 | block = tcf_block_refcnt_get(net, ei->block_index); |
48617387 JP |
1322 | |
1323 | if (!block) { | |
bb047ddd | 1324 | block = tcf_block_create(net, q, ei->block_index, extack); |
48617387 JP |
1325 | if (IS_ERR(block)) |
1326 | return PTR_ERR(block); | |
bb047ddd JP |
1327 | if (tcf_block_shared(block)) { |
1328 | err = tcf_block_insert(block, net, extack); | |
48617387 JP |
1329 | if (err) |
1330 | goto err_block_insert; | |
1331 | } | |
1332 | } | |
1333 | ||
f36fe1c4 JP |
1334 | err = tcf_block_owner_add(block, q, ei->binder_type); |
1335 | if (err) | |
1336 | goto err_block_owner_add; | |
1337 | ||
1338 | tcf_block_owner_netif_keep_dst(block, q, ei->binder_type); | |
1339 | ||
f71e0ca4 | 1340 | err = tcf_chain0_head_change_cb_add(block, ei, extack); |
a9b19443 | 1341 | if (err) |
f71e0ca4 | 1342 | goto err_chain0_head_change_cb_add; |
caa72601 | 1343 | |
60513bd8 | 1344 | err = tcf_block_offload_bind(block, q, ei, extack); |
caa72601 JP |
1345 | if (err) |
1346 | goto err_block_offload_bind; | |
1347 | ||
6529eaba JP |
1348 | *p_block = block; |
1349 | return 0; | |
2190d1d0 | 1350 | |
caa72601 | 1351 | err_block_offload_bind: |
f71e0ca4 JP |
1352 | tcf_chain0_head_change_cb_del(block, ei); |
1353 | err_chain0_head_change_cb_add: | |
f36fe1c4 JP |
1354 | tcf_block_owner_del(block, q, ei->binder_type); |
1355 | err_block_owner_add: | |
48617387 | 1356 | err_block_insert: |
787ce6d0 | 1357 | tcf_block_refcnt_put(block); |
2190d1d0 | 1358 | return err; |
6529eaba | 1359 | } |
8c4083b3 JP |
1360 | EXPORT_SYMBOL(tcf_block_get_ext); |
1361 | ||
c7eb7d72 JP |
1362 | static void tcf_chain_head_change_dflt(struct tcf_proto *tp_head, void *priv) |
1363 | { | |
1364 | struct tcf_proto __rcu **p_filter_chain = priv; | |
1365 | ||
1366 | rcu_assign_pointer(*p_filter_chain, tp_head); | |
1367 | } | |
1368 | ||
8c4083b3 | 1369 | int tcf_block_get(struct tcf_block **p_block, |
8d1a77f9 AA |
1370 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
1371 | struct netlink_ext_ack *extack) | |
8c4083b3 | 1372 | { |
c7eb7d72 JP |
1373 | struct tcf_block_ext_info ei = { |
1374 | .chain_head_change = tcf_chain_head_change_dflt, | |
1375 | .chain_head_change_priv = p_filter_chain, | |
1376 | }; | |
8c4083b3 | 1377 | |
c7eb7d72 | 1378 | WARN_ON(!p_filter_chain); |
8d1a77f9 | 1379 | return tcf_block_get_ext(p_block, q, &ei, extack); |
8c4083b3 | 1380 | } |
6529eaba JP |
1381 | EXPORT_SYMBOL(tcf_block_get); |
1382 | ||
7aa0045d | 1383 | /* XXX: Standalone actions are not allowed to jump to any chain, and bound |
a60b3f51 | 1384 | * actions should be all removed after flushing. |
7aa0045d | 1385 | */ |
c7eb7d72 | 1386 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, |
e1ea2f98 | 1387 | struct tcf_block_ext_info *ei) |
7aa0045d | 1388 | { |
c30abd5e DM |
1389 | if (!block) |
1390 | return; | |
f71e0ca4 | 1391 | tcf_chain0_head_change_cb_del(block, ei); |
f36fe1c4 | 1392 | tcf_block_owner_del(block, q, ei->binder_type); |
a60b3f51 | 1393 | |
0607e439 | 1394 | __tcf_block_put(block, q, ei); |
6529eaba | 1395 | } |
8c4083b3 JP |
1396 | EXPORT_SYMBOL(tcf_block_put_ext); |
1397 | ||
1398 | void tcf_block_put(struct tcf_block *block) | |
1399 | { | |
1400 | struct tcf_block_ext_info ei = {0, }; | |
1401 | ||
4853f128 JP |
1402 | if (!block) |
1403 | return; | |
c7eb7d72 | 1404 | tcf_block_put_ext(block, block->q, &ei); |
8c4083b3 | 1405 | } |
e1ea2f98 | 1406 | |
6529eaba | 1407 | EXPORT_SYMBOL(tcf_block_put); |
cf1facda | 1408 | |
acb67442 JP |
1409 | struct tcf_block_cb { |
1410 | struct list_head list; | |
1411 | tc_setup_cb_t *cb; | |
1412 | void *cb_ident; | |
1413 | void *cb_priv; | |
1414 | unsigned int refcnt; | |
1415 | }; | |
1416 | ||
1417 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) | |
1418 | { | |
1419 | return block_cb->cb_priv; | |
1420 | } | |
1421 | EXPORT_SYMBOL(tcf_block_cb_priv); | |
1422 | ||
1423 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, | |
1424 | tc_setup_cb_t *cb, void *cb_ident) | |
1425 | { struct tcf_block_cb *block_cb; | |
1426 | ||
1427 | list_for_each_entry(block_cb, &block->cb_list, list) | |
1428 | if (block_cb->cb == cb && block_cb->cb_ident == cb_ident) | |
1429 | return block_cb; | |
1430 | return NULL; | |
1431 | } | |
1432 | EXPORT_SYMBOL(tcf_block_cb_lookup); | |
1433 | ||
1434 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) | |
1435 | { | |
1436 | block_cb->refcnt++; | |
1437 | } | |
1438 | EXPORT_SYMBOL(tcf_block_cb_incref); | |
1439 | ||
1440 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) | |
1441 | { | |
1442 | return --block_cb->refcnt; | |
1443 | } | |
1444 | EXPORT_SYMBOL(tcf_block_cb_decref); | |
1445 | ||
32636742 JH |
1446 | static int |
1447 | tcf_block_playback_offloads(struct tcf_block *block, tc_setup_cb_t *cb, | |
1448 | void *cb_priv, bool add, bool offload_in_use, | |
1449 | struct netlink_ext_ack *extack) | |
1450 | { | |
bbf73830 | 1451 | struct tcf_chain *chain, *chain_prev; |
fe2923af | 1452 | struct tcf_proto *tp, *tp_prev; |
32636742 JH |
1453 | int err; |
1454 | ||
bbf73830 VB |
1455 | for (chain = __tcf_get_next_chain(block, NULL); |
1456 | chain; | |
1457 | chain_prev = chain, | |
1458 | chain = __tcf_get_next_chain(block, chain), | |
1459 | tcf_chain_put(chain_prev)) { | |
fe2923af VB |
1460 | for (tp = __tcf_get_next_proto(chain, NULL); tp; |
1461 | tp_prev = tp, | |
1462 | tp = __tcf_get_next_proto(chain, tp), | |
1463 | tcf_proto_put(tp_prev, NULL)) { | |
32636742 JH |
1464 | if (tp->ops->reoffload) { |
1465 | err = tp->ops->reoffload(tp, add, cb, cb_priv, | |
1466 | extack); | |
1467 | if (err && add) | |
1468 | goto err_playback_remove; | |
1469 | } else if (add && offload_in_use) { | |
1470 | err = -EOPNOTSUPP; | |
1471 | NL_SET_ERR_MSG(extack, "Filter HW offload failed - classifier without re-offloading support"); | |
1472 | goto err_playback_remove; | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | return 0; | |
1478 | ||
1479 | err_playback_remove: | |
fe2923af | 1480 | tcf_proto_put(tp, NULL); |
bbf73830 | 1481 | tcf_chain_put(chain); |
32636742 JH |
1482 | tcf_block_playback_offloads(block, cb, cb_priv, false, offload_in_use, |
1483 | extack); | |
1484 | return err; | |
1485 | } | |
1486 | ||
acb67442 JP |
1487 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, |
1488 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 JH |
1489 | void *cb_priv, |
1490 | struct netlink_ext_ack *extack) | |
acb67442 JP |
1491 | { |
1492 | struct tcf_block_cb *block_cb; | |
32636742 | 1493 | int err; |
acb67442 | 1494 | |
32636742 JH |
1495 | /* Replay any already present rules */ |
1496 | err = tcf_block_playback_offloads(block, cb, cb_priv, true, | |
1497 | tcf_block_offload_in_use(block), | |
1498 | extack); | |
1499 | if (err) | |
1500 | return ERR_PTR(err); | |
caa72601 | 1501 | |
acb67442 JP |
1502 | block_cb = kzalloc(sizeof(*block_cb), GFP_KERNEL); |
1503 | if (!block_cb) | |
caa72601 | 1504 | return ERR_PTR(-ENOMEM); |
acb67442 JP |
1505 | block_cb->cb = cb; |
1506 | block_cb->cb_ident = cb_ident; | |
1507 | block_cb->cb_priv = cb_priv; | |
1508 | list_add(&block_cb->list, &block->cb_list); | |
1509 | return block_cb; | |
1510 | } | |
1511 | EXPORT_SYMBOL(__tcf_block_cb_register); | |
1512 | ||
1513 | int tcf_block_cb_register(struct tcf_block *block, | |
1514 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 | 1515 | void *cb_priv, struct netlink_ext_ack *extack) |
acb67442 JP |
1516 | { |
1517 | struct tcf_block_cb *block_cb; | |
1518 | ||
60513bd8 JH |
1519 | block_cb = __tcf_block_cb_register(block, cb, cb_ident, cb_priv, |
1520 | extack); | |
baa2d2b1 | 1521 | return PTR_ERR_OR_ZERO(block_cb); |
acb67442 JP |
1522 | } |
1523 | EXPORT_SYMBOL(tcf_block_cb_register); | |
1524 | ||
32636742 JH |
1525 | void __tcf_block_cb_unregister(struct tcf_block *block, |
1526 | struct tcf_block_cb *block_cb) | |
acb67442 | 1527 | { |
32636742 JH |
1528 | tcf_block_playback_offloads(block, block_cb->cb, block_cb->cb_priv, |
1529 | false, tcf_block_offload_in_use(block), | |
1530 | NULL); | |
acb67442 JP |
1531 | list_del(&block_cb->list); |
1532 | kfree(block_cb); | |
1533 | } | |
1534 | EXPORT_SYMBOL(__tcf_block_cb_unregister); | |
1535 | ||
1536 | void tcf_block_cb_unregister(struct tcf_block *block, | |
1537 | tc_setup_cb_t *cb, void *cb_ident) | |
1538 | { | |
1539 | struct tcf_block_cb *block_cb; | |
1540 | ||
1541 | block_cb = tcf_block_cb_lookup(block, cb, cb_ident); | |
1542 | if (!block_cb) | |
1543 | return; | |
32636742 | 1544 | __tcf_block_cb_unregister(block, block_cb); |
acb67442 JP |
1545 | } |
1546 | EXPORT_SYMBOL(tcf_block_cb_unregister); | |
1547 | ||
87d83093 JP |
1548 | /* Main classifier routine: scans classifier chain attached |
1549 | * to this qdisc, (optionally) tests for protocol and asks | |
1550 | * specific classifiers. | |
1551 | */ | |
1552 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, | |
1553 | struct tcf_result *res, bool compat_mode) | |
1554 | { | |
87d83093 JP |
1555 | #ifdef CONFIG_NET_CLS_ACT |
1556 | const int max_reclassify_loop = 4; | |
ee538dce JP |
1557 | const struct tcf_proto *orig_tp = tp; |
1558 | const struct tcf_proto *first_tp; | |
87d83093 JP |
1559 | int limit = 0; |
1560 | ||
1561 | reclassify: | |
1562 | #endif | |
1563 | for (; tp; tp = rcu_dereference_bh(tp->next)) { | |
cd0c4e70 | 1564 | __be16 protocol = tc_skb_protocol(skb); |
87d83093 JP |
1565 | int err; |
1566 | ||
1567 | if (tp->protocol != protocol && | |
1568 | tp->protocol != htons(ETH_P_ALL)) | |
1569 | continue; | |
1570 | ||
1571 | err = tp->classify(skb, tp, res); | |
1572 | #ifdef CONFIG_NET_CLS_ACT | |
db50514f | 1573 | if (unlikely(err == TC_ACT_RECLASSIFY && !compat_mode)) { |
ee538dce | 1574 | first_tp = orig_tp; |
87d83093 | 1575 | goto reset; |
db50514f | 1576 | } else if (unlikely(TC_ACT_EXT_CMP(err, TC_ACT_GOTO_CHAIN))) { |
ee538dce | 1577 | first_tp = res->goto_tp; |
db50514f JP |
1578 | goto reset; |
1579 | } | |
87d83093 JP |
1580 | #endif |
1581 | if (err >= 0) | |
1582 | return err; | |
1583 | } | |
1584 | ||
1585 | return TC_ACT_UNSPEC; /* signal: continue lookup */ | |
1586 | #ifdef CONFIG_NET_CLS_ACT | |
1587 | reset: | |
1588 | if (unlikely(limit++ >= max_reclassify_loop)) { | |
9d3aaff3 JP |
1589 | net_notice_ratelimited("%u: reclassify loop, rule prio %u, protocol %02x\n", |
1590 | tp->chain->block->index, | |
1591 | tp->prio & 0xffff, | |
87d83093 JP |
1592 | ntohs(tp->protocol)); |
1593 | return TC_ACT_SHOT; | |
1594 | } | |
1595 | ||
ee538dce | 1596 | tp = first_tp; |
87d83093 JP |
1597 | goto reclassify; |
1598 | #endif | |
1599 | } | |
1600 | EXPORT_SYMBOL(tcf_classify); | |
1601 | ||
2190d1d0 JP |
1602 | struct tcf_chain_info { |
1603 | struct tcf_proto __rcu **pprev; | |
1604 | struct tcf_proto __rcu *next; | |
1605 | }; | |
1606 | ||
ed76f5ed VB |
1607 | static struct tcf_proto *tcf_chain_tp_prev(struct tcf_chain *chain, |
1608 | struct tcf_chain_info *chain_info) | |
2190d1d0 | 1609 | { |
ed76f5ed | 1610 | return tcf_chain_dereference(*chain_info->pprev, chain); |
2190d1d0 JP |
1611 | } |
1612 | ||
1613 | static void tcf_chain_tp_insert(struct tcf_chain *chain, | |
1614 | struct tcf_chain_info *chain_info, | |
1615 | struct tcf_proto *tp) | |
1616 | { | |
c7eb7d72 | 1617 | if (*chain_info->pprev == chain->filter_chain) |
f71e0ca4 | 1618 | tcf_chain0_head_change(chain, tp); |
4dbfa766 | 1619 | tcf_proto_get(tp); |
ed76f5ed | 1620 | RCU_INIT_POINTER(tp->next, tcf_chain_tp_prev(chain, chain_info)); |
2190d1d0 JP |
1621 | rcu_assign_pointer(*chain_info->pprev, tp); |
1622 | } | |
1623 | ||
1624 | static void tcf_chain_tp_remove(struct tcf_chain *chain, | |
1625 | struct tcf_chain_info *chain_info, | |
1626 | struct tcf_proto *tp) | |
1627 | { | |
ed76f5ed | 1628 | struct tcf_proto *next = tcf_chain_dereference(chain_info->next, chain); |
2190d1d0 | 1629 | |
8b64678e | 1630 | tcf_proto_mark_delete(tp); |
c7eb7d72 | 1631 | if (tp == chain->filter_chain) |
f71e0ca4 | 1632 | tcf_chain0_head_change(chain, next); |
2190d1d0 JP |
1633 | RCU_INIT_POINTER(*chain_info->pprev, next); |
1634 | } | |
1635 | ||
8b64678e VB |
1636 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
1637 | struct tcf_chain_info *chain_info, | |
1638 | u32 protocol, u32 prio, | |
1639 | bool prio_allocate); | |
1640 | ||
1641 | /* Try to insert new proto. | |
1642 | * If proto with specified priority already exists, free new proto | |
1643 | * and return existing one. | |
1644 | */ | |
1645 | ||
1646 | static struct tcf_proto *tcf_chain_tp_insert_unique(struct tcf_chain *chain, | |
1647 | struct tcf_proto *tp_new, | |
1648 | u32 protocol, u32 prio) | |
1649 | { | |
1650 | struct tcf_chain_info chain_info; | |
1651 | struct tcf_proto *tp; | |
1652 | ||
1653 | mutex_lock(&chain->filter_chain_lock); | |
1654 | ||
1655 | tp = tcf_chain_tp_find(chain, &chain_info, | |
1656 | protocol, prio, false); | |
1657 | if (!tp) | |
1658 | tcf_chain_tp_insert(chain, &chain_info, tp_new); | |
1659 | mutex_unlock(&chain->filter_chain_lock); | |
1660 | ||
1661 | if (tp) { | |
1662 | tcf_proto_destroy(tp_new, NULL); | |
1663 | tp_new = tp; | |
1664 | } | |
1665 | ||
1666 | return tp_new; | |
1667 | } | |
1668 | ||
1669 | static void tcf_chain_tp_delete_empty(struct tcf_chain *chain, | |
1670 | struct tcf_proto *tp, | |
1671 | struct netlink_ext_ack *extack) | |
1672 | { | |
1673 | struct tcf_chain_info chain_info; | |
1674 | struct tcf_proto *tp_iter; | |
1675 | struct tcf_proto **pprev; | |
1676 | struct tcf_proto *next; | |
1677 | ||
1678 | mutex_lock(&chain->filter_chain_lock); | |
1679 | ||
1680 | /* Atomically find and remove tp from chain. */ | |
1681 | for (pprev = &chain->filter_chain; | |
1682 | (tp_iter = tcf_chain_dereference(*pprev, chain)); | |
1683 | pprev = &tp_iter->next) { | |
1684 | if (tp_iter == tp) { | |
1685 | chain_info.pprev = pprev; | |
1686 | chain_info.next = tp_iter->next; | |
1687 | WARN_ON(tp_iter->deleting); | |
1688 | break; | |
1689 | } | |
1690 | } | |
1691 | /* Verify that tp still exists and no new filters were inserted | |
1692 | * concurrently. | |
1693 | * Mark tp for deletion if it is empty. | |
1694 | */ | |
1695 | if (!tp_iter || !tcf_proto_check_delete(tp)) { | |
1696 | mutex_unlock(&chain->filter_chain_lock); | |
1697 | return; | |
1698 | } | |
1699 | ||
1700 | next = tcf_chain_dereference(chain_info.next, chain); | |
1701 | if (tp == chain->filter_chain) | |
1702 | tcf_chain0_head_change(chain, next); | |
1703 | RCU_INIT_POINTER(*chain_info.pprev, next); | |
1704 | mutex_unlock(&chain->filter_chain_lock); | |
1705 | ||
1706 | tcf_proto_put(tp, extack); | |
1707 | } | |
1708 | ||
2190d1d0 JP |
1709 | static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain, |
1710 | struct tcf_chain_info *chain_info, | |
1711 | u32 protocol, u32 prio, | |
1712 | bool prio_allocate) | |
1713 | { | |
1714 | struct tcf_proto **pprev; | |
1715 | struct tcf_proto *tp; | |
1716 | ||
1717 | /* Check the chain for existence of proto-tcf with this priority */ | |
1718 | for (pprev = &chain->filter_chain; | |
ed76f5ed VB |
1719 | (tp = tcf_chain_dereference(*pprev, chain)); |
1720 | pprev = &tp->next) { | |
2190d1d0 JP |
1721 | if (tp->prio >= prio) { |
1722 | if (tp->prio == prio) { | |
1723 | if (prio_allocate || | |
1724 | (tp->protocol != protocol && protocol)) | |
1725 | return ERR_PTR(-EINVAL); | |
1726 | } else { | |
1727 | tp = NULL; | |
1728 | } | |
1729 | break; | |
1730 | } | |
1731 | } | |
1732 | chain_info->pprev = pprev; | |
4dbfa766 VB |
1733 | if (tp) { |
1734 | chain_info->next = tp->next; | |
1735 | tcf_proto_get(tp); | |
1736 | } else { | |
1737 | chain_info->next = NULL; | |
1738 | } | |
2190d1d0 JP |
1739 | return tp; |
1740 | } | |
1741 | ||
7120371c | 1742 | static int tcf_fill_node(struct net *net, struct sk_buff *skb, |
7960d1da JP |
1743 | struct tcf_proto *tp, struct tcf_block *block, |
1744 | struct Qdisc *q, u32 parent, void *fh, | |
1745 | u32 portid, u32 seq, u16 flags, int event) | |
7120371c WC |
1746 | { |
1747 | struct tcmsg *tcm; | |
1748 | struct nlmsghdr *nlh; | |
1749 | unsigned char *b = skb_tail_pointer(skb); | |
1750 | ||
1751 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); | |
1752 | if (!nlh) | |
1753 | goto out_nlmsg_trim; | |
1754 | tcm = nlmsg_data(nlh); | |
1755 | tcm->tcm_family = AF_UNSPEC; | |
1756 | tcm->tcm__pad1 = 0; | |
1757 | tcm->tcm__pad2 = 0; | |
7960d1da JP |
1758 | if (q) { |
1759 | tcm->tcm_ifindex = qdisc_dev(q)->ifindex; | |
1760 | tcm->tcm_parent = parent; | |
1761 | } else { | |
1762 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; | |
1763 | tcm->tcm_block_index = block->index; | |
1764 | } | |
7120371c WC |
1765 | tcm->tcm_info = TC_H_MAKE(tp->prio, tp->protocol); |
1766 | if (nla_put_string(skb, TCA_KIND, tp->ops->kind)) | |
1767 | goto nla_put_failure; | |
1768 | if (nla_put_u32(skb, TCA_CHAIN, tp->chain->index)) | |
1769 | goto nla_put_failure; | |
1770 | if (!fh) { | |
1771 | tcm->tcm_handle = 0; | |
1772 | } else { | |
1773 | if (tp->ops->dump && tp->ops->dump(net, tp, fh, skb, tcm) < 0) | |
1774 | goto nla_put_failure; | |
1775 | } | |
1776 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; | |
1777 | return skb->len; | |
1778 | ||
1779 | out_nlmsg_trim: | |
1780 | nla_put_failure: | |
1781 | nlmsg_trim(skb, b); | |
1782 | return -1; | |
1783 | } | |
1784 | ||
1785 | static int tfilter_notify(struct net *net, struct sk_buff *oskb, | |
1786 | struct nlmsghdr *n, struct tcf_proto *tp, | |
7960d1da JP |
1787 | struct tcf_block *block, struct Qdisc *q, |
1788 | u32 parent, void *fh, int event, bool unicast) | |
7120371c WC |
1789 | { |
1790 | struct sk_buff *skb; | |
1791 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
1792 | ||
1793 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1794 | if (!skb) | |
1795 | return -ENOBUFS; | |
1796 | ||
7960d1da JP |
1797 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
1798 | n->nlmsg_seq, n->nlmsg_flags, event) <= 0) { | |
7120371c WC |
1799 | kfree_skb(skb); |
1800 | return -EINVAL; | |
1801 | } | |
1802 | ||
1803 | if (unicast) | |
1804 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
1805 | ||
1806 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
1807 | n->nlmsg_flags & NLM_F_ECHO); | |
1808 | } | |
1809 | ||
1810 | static int tfilter_del_notify(struct net *net, struct sk_buff *oskb, | |
1811 | struct nlmsghdr *n, struct tcf_proto *tp, | |
7960d1da | 1812 | struct tcf_block *block, struct Qdisc *q, |
c35a4acc AA |
1813 | u32 parent, void *fh, bool unicast, bool *last, |
1814 | struct netlink_ext_ack *extack) | |
7120371c WC |
1815 | { |
1816 | struct sk_buff *skb; | |
1817 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
1818 | int err; | |
1819 | ||
1820 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1821 | if (!skb) | |
1822 | return -ENOBUFS; | |
1823 | ||
7960d1da JP |
1824 | if (tcf_fill_node(net, skb, tp, block, q, parent, fh, portid, |
1825 | n->nlmsg_seq, n->nlmsg_flags, RTM_DELTFILTER) <= 0) { | |
c35a4acc | 1826 | NL_SET_ERR_MSG(extack, "Failed to build del event notification"); |
7120371c WC |
1827 | kfree_skb(skb); |
1828 | return -EINVAL; | |
1829 | } | |
1830 | ||
571acf21 | 1831 | err = tp->ops->delete(tp, fh, last, extack); |
7120371c WC |
1832 | if (err) { |
1833 | kfree_skb(skb); | |
1834 | return err; | |
1835 | } | |
1836 | ||
1837 | if (unicast) | |
1838 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
1839 | ||
c35a4acc AA |
1840 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
1841 | n->nlmsg_flags & NLM_F_ECHO); | |
1842 | if (err < 0) | |
1843 | NL_SET_ERR_MSG(extack, "Failed to send filter delete notification"); | |
1844 | return err; | |
7120371c WC |
1845 | } |
1846 | ||
1847 | static void tfilter_notify_chain(struct net *net, struct sk_buff *oskb, | |
7960d1da JP |
1848 | struct tcf_block *block, struct Qdisc *q, |
1849 | u32 parent, struct nlmsghdr *n, | |
7120371c WC |
1850 | struct tcf_chain *chain, int event) |
1851 | { | |
1852 | struct tcf_proto *tp; | |
1853 | ||
fe2923af VB |
1854 | for (tp = tcf_get_next_proto(chain, NULL); |
1855 | tp; tp = tcf_get_next_proto(chain, tp)) | |
7960d1da | 1856 | tfilter_notify(net, oskb, n, tp, block, |
53189183 | 1857 | q, parent, NULL, event, false); |
7120371c WC |
1858 | } |
1859 | ||
c431f89b | 1860 | static int tc_new_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
c21ef3e3 | 1861 | struct netlink_ext_ack *extack) |
1da177e4 | 1862 | { |
3b1e0a65 | 1863 | struct net *net = sock_net(skb->sk); |
add93b61 | 1864 | struct nlattr *tca[TCA_MAX + 1]; |
1da177e4 LT |
1865 | struct tcmsg *t; |
1866 | u32 protocol; | |
1867 | u32 prio; | |
9d36d9e5 | 1868 | bool prio_allocate; |
1da177e4 | 1869 | u32 parent; |
5bc17018 | 1870 | u32 chain_index; |
7960d1da | 1871 | struct Qdisc *q = NULL; |
2190d1d0 | 1872 | struct tcf_chain_info chain_info; |
5bc17018 | 1873 | struct tcf_chain *chain = NULL; |
6529eaba | 1874 | struct tcf_block *block; |
1da177e4 | 1875 | struct tcf_proto *tp; |
1da177e4 | 1876 | unsigned long cl; |
8113c095 | 1877 | void *fh; |
1da177e4 | 1878 | int err; |
628185cf | 1879 | int tp_created; |
1da177e4 | 1880 | |
c431f89b | 1881 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) |
dfc47ef8 | 1882 | return -EPERM; |
de179c8c | 1883 | |
1da177e4 | 1884 | replay: |
628185cf DB |
1885 | tp_created = 0; |
1886 | ||
e331473f | 1887 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack); |
de179c8c H |
1888 | if (err < 0) |
1889 | return err; | |
1890 | ||
942b8165 | 1891 | t = nlmsg_data(n); |
1da177e4 LT |
1892 | protocol = TC_H_MIN(t->tcm_info); |
1893 | prio = TC_H_MAJ(t->tcm_info); | |
9d36d9e5 | 1894 | prio_allocate = false; |
1da177e4 | 1895 | parent = t->tcm_parent; |
4dbfa766 | 1896 | tp = NULL; |
1da177e4 LT |
1897 | cl = 0; |
1898 | ||
1899 | if (prio == 0) { | |
c431f89b VB |
1900 | /* If no priority is provided by the user, |
1901 | * we allocate one. | |
1902 | */ | |
1903 | if (n->nlmsg_flags & NLM_F_CREATE) { | |
1904 | prio = TC_H_MAKE(0x80000000U, 0U); | |
1905 | prio_allocate = true; | |
1906 | } else { | |
c35a4acc | 1907 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); |
1da177e4 | 1908 | return -ENOENT; |
ea7f8277 | 1909 | } |
1da177e4 LT |
1910 | } |
1911 | ||
1912 | /* Find head of filter chain. */ | |
1913 | ||
c431f89b VB |
1914 | block = tcf_block_find(net, &q, &parent, &cl, |
1915 | t->tcm_ifindex, t->tcm_block_index, extack); | |
1916 | if (IS_ERR(block)) { | |
1917 | err = PTR_ERR(block); | |
1918 | goto errout; | |
6bb16e7a | 1919 | } |
5bc17018 JP |
1920 | |
1921 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; | |
1922 | if (chain_index > TC_ACT_EXT_VAL_MASK) { | |
c35a4acc | 1923 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); |
5bc17018 JP |
1924 | err = -EINVAL; |
1925 | goto errout; | |
1926 | } | |
c431f89b | 1927 | chain = tcf_chain_get(block, chain_index, true); |
5bc17018 | 1928 | if (!chain) { |
d5ed72a5 | 1929 | NL_SET_ERR_MSG(extack, "Cannot create specified filter chain"); |
c431f89b | 1930 | err = -ENOMEM; |
ea7f8277 DB |
1931 | goto errout; |
1932 | } | |
1da177e4 | 1933 | |
ed76f5ed | 1934 | mutex_lock(&chain->filter_chain_lock); |
2190d1d0 JP |
1935 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
1936 | prio, prio_allocate); | |
1937 | if (IS_ERR(tp)) { | |
c35a4acc | 1938 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); |
2190d1d0 | 1939 | err = PTR_ERR(tp); |
ed76f5ed | 1940 | goto errout_locked; |
1da177e4 LT |
1941 | } |
1942 | ||
1943 | if (tp == NULL) { | |
8b64678e VB |
1944 | struct tcf_proto *tp_new = NULL; |
1945 | ||
1da177e4 LT |
1946 | /* Proto-tcf does not exist, create new one */ |
1947 | ||
6bb16e7a | 1948 | if (tca[TCA_KIND] == NULL || !protocol) { |
c35a4acc | 1949 | NL_SET_ERR_MSG(extack, "Filter kind and protocol must be specified"); |
6bb16e7a | 1950 | err = -EINVAL; |
ed76f5ed | 1951 | goto errout_locked; |
6bb16e7a | 1952 | } |
1da177e4 | 1953 | |
c431f89b | 1954 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
c35a4acc | 1955 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
6bb16e7a | 1956 | err = -ENOENT; |
ed76f5ed | 1957 | goto errout_locked; |
6bb16e7a | 1958 | } |
1da177e4 | 1959 | |
9d36d9e5 | 1960 | if (prio_allocate) |
ed76f5ed VB |
1961 | prio = tcf_auto_prio(tcf_chain_tp_prev(chain, |
1962 | &chain_info)); | |
1da177e4 | 1963 | |
ed76f5ed | 1964 | mutex_unlock(&chain->filter_chain_lock); |
8b64678e VB |
1965 | tp_new = tcf_proto_create(nla_data(tca[TCA_KIND]), |
1966 | protocol, prio, chain, extack); | |
1967 | if (IS_ERR(tp_new)) { | |
1968 | err = PTR_ERR(tp_new); | |
1da177e4 LT |
1969 | goto errout; |
1970 | } | |
ed76f5ed | 1971 | |
12186be7 | 1972 | tp_created = 1; |
8b64678e | 1973 | tp = tcf_chain_tp_insert_unique(chain, tp_new, protocol, prio); |
ed76f5ed VB |
1974 | } else { |
1975 | mutex_unlock(&chain->filter_chain_lock); | |
6bb16e7a | 1976 | } |
1da177e4 | 1977 | |
8b64678e VB |
1978 | if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
1979 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); | |
1980 | err = -EINVAL; | |
1981 | goto errout; | |
1982 | } | |
1983 | ||
1da177e4 LT |
1984 | fh = tp->ops->get(tp, t->tcm_handle); |
1985 | ||
8113c095 | 1986 | if (!fh) { |
c431f89b | 1987 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
c35a4acc | 1988 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWTFILTER and NLM_F_CREATE to create a new filter"); |
6bb16e7a | 1989 | err = -ENOENT; |
1da177e4 | 1990 | goto errout; |
6bb16e7a | 1991 | } |
c431f89b VB |
1992 | } else if (n->nlmsg_flags & NLM_F_EXCL) { |
1993 | NL_SET_ERR_MSG(extack, "Filter already exists"); | |
1994 | err = -EEXIST; | |
1995 | goto errout; | |
1da177e4 LT |
1996 | } |
1997 | ||
9f407f17 JP |
1998 | if (chain->tmplt_ops && chain->tmplt_ops != tp->ops) { |
1999 | NL_SET_ERR_MSG(extack, "Chain template is set to a different filter kind"); | |
2000 | err = -EINVAL; | |
2001 | goto errout; | |
2002 | } | |
2003 | ||
2f7ef2f8 | 2004 | err = tp->ops->change(net, skb, tp, cl, t->tcm_handle, tca, &fh, |
7306db38 AA |
2005 | n->nlmsg_flags & NLM_F_CREATE ? TCA_ACT_NOREPLACE : TCA_ACT_REPLACE, |
2006 | extack); | |
ed76f5ed | 2007 | if (err == 0) |
7960d1da | 2008 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
a10fa201 | 2009 | RTM_NEWTFILTER, false); |
1da177e4 LT |
2010 | |
2011 | errout: | |
8b64678e VB |
2012 | if (err && tp_created) |
2013 | tcf_chain_tp_delete_empty(chain, tp, NULL); | |
4dbfa766 VB |
2014 | if (chain) { |
2015 | if (tp && !IS_ERR(tp)) | |
2016 | tcf_proto_put(tp, NULL); | |
2017 | if (!tp_created) | |
2018 | tcf_chain_put(chain); | |
2019 | } | |
e368fdb6 | 2020 | tcf_block_release(q, block); |
1da177e4 LT |
2021 | if (err == -EAGAIN) |
2022 | /* Replay the request. */ | |
2023 | goto replay; | |
2024 | return err; | |
ed76f5ed VB |
2025 | |
2026 | errout_locked: | |
2027 | mutex_unlock(&chain->filter_chain_lock); | |
2028 | goto errout; | |
1da177e4 LT |
2029 | } |
2030 | ||
c431f89b VB |
2031 | static int tc_del_tfilter(struct sk_buff *skb, struct nlmsghdr *n, |
2032 | struct netlink_ext_ack *extack) | |
2033 | { | |
2034 | struct net *net = sock_net(skb->sk); | |
2035 | struct nlattr *tca[TCA_MAX + 1]; | |
2036 | struct tcmsg *t; | |
2037 | u32 protocol; | |
2038 | u32 prio; | |
2039 | u32 parent; | |
2040 | u32 chain_index; | |
2041 | struct Qdisc *q = NULL; | |
2042 | struct tcf_chain_info chain_info; | |
2043 | struct tcf_chain *chain = NULL; | |
2044 | struct tcf_block *block; | |
2045 | struct tcf_proto *tp = NULL; | |
2046 | unsigned long cl = 0; | |
2047 | void *fh = NULL; | |
2048 | int err; | |
2049 | ||
2050 | if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) | |
2051 | return -EPERM; | |
2052 | ||
e331473f | 2053 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack); |
c431f89b VB |
2054 | if (err < 0) |
2055 | return err; | |
2056 | ||
2057 | t = nlmsg_data(n); | |
2058 | protocol = TC_H_MIN(t->tcm_info); | |
2059 | prio = TC_H_MAJ(t->tcm_info); | |
2060 | parent = t->tcm_parent; | |
2061 | ||
2062 | if (prio == 0 && (protocol || t->tcm_handle || tca[TCA_KIND])) { | |
2063 | NL_SET_ERR_MSG(extack, "Cannot flush filters with protocol, handle or kind set"); | |
2064 | return -ENOENT; | |
2065 | } | |
2066 | ||
2067 | /* Find head of filter chain. */ | |
2068 | ||
2069 | block = tcf_block_find(net, &q, &parent, &cl, | |
2070 | t->tcm_ifindex, t->tcm_block_index, extack); | |
2071 | if (IS_ERR(block)) { | |
2072 | err = PTR_ERR(block); | |
2073 | goto errout; | |
2074 | } | |
2075 | ||
2076 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; | |
2077 | if (chain_index > TC_ACT_EXT_VAL_MASK) { | |
2078 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); | |
2079 | err = -EINVAL; | |
2080 | goto errout; | |
2081 | } | |
2082 | chain = tcf_chain_get(block, chain_index, false); | |
2083 | if (!chain) { | |
5ca8a25c JP |
2084 | /* User requested flush on non-existent chain. Nothing to do, |
2085 | * so just return success. | |
2086 | */ | |
2087 | if (prio == 0) { | |
2088 | err = 0; | |
2089 | goto errout; | |
2090 | } | |
c431f89b | 2091 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
b7b4247d | 2092 | err = -ENOENT; |
c431f89b VB |
2093 | goto errout; |
2094 | } | |
2095 | ||
2096 | if (prio == 0) { | |
2097 | tfilter_notify_chain(net, skb, block, q, parent, n, | |
2098 | chain, RTM_DELTFILTER); | |
2099 | tcf_chain_flush(chain); | |
2100 | err = 0; | |
2101 | goto errout; | |
2102 | } | |
2103 | ||
ed76f5ed | 2104 | mutex_lock(&chain->filter_chain_lock); |
c431f89b VB |
2105 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
2106 | prio, false); | |
2107 | if (!tp || IS_ERR(tp)) { | |
2108 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); | |
0e399035 | 2109 | err = tp ? PTR_ERR(tp) : -ENOENT; |
ed76f5ed | 2110 | goto errout_locked; |
c431f89b VB |
2111 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { |
2112 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); | |
2113 | err = -EINVAL; | |
ed76f5ed VB |
2114 | goto errout_locked; |
2115 | } else if (t->tcm_handle == 0) { | |
2116 | tcf_chain_tp_remove(chain, &chain_info, tp); | |
2117 | mutex_unlock(&chain->filter_chain_lock); | |
2118 | ||
8b64678e | 2119 | tcf_proto_put(tp, NULL); |
ed76f5ed VB |
2120 | tfilter_notify(net, skb, n, tp, block, q, parent, fh, |
2121 | RTM_DELTFILTER, false); | |
ed76f5ed | 2122 | err = 0; |
c431f89b VB |
2123 | goto errout; |
2124 | } | |
ed76f5ed | 2125 | mutex_unlock(&chain->filter_chain_lock); |
c431f89b VB |
2126 | |
2127 | fh = tp->ops->get(tp, t->tcm_handle); | |
2128 | ||
2129 | if (!fh) { | |
ed76f5ed VB |
2130 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); |
2131 | err = -ENOENT; | |
c431f89b VB |
2132 | } else { |
2133 | bool last; | |
2134 | ||
2135 | err = tfilter_del_notify(net, skb, n, tp, block, | |
2136 | q, parent, fh, false, &last, | |
2137 | extack); | |
2138 | if (err) | |
2139 | goto errout; | |
8b64678e VB |
2140 | if (last) |
2141 | tcf_chain_tp_delete_empty(chain, tp, extack); | |
c431f89b VB |
2142 | } |
2143 | ||
2144 | errout: | |
4dbfa766 VB |
2145 | if (chain) { |
2146 | if (tp && !IS_ERR(tp)) | |
2147 | tcf_proto_put(tp, NULL); | |
c431f89b | 2148 | tcf_chain_put(chain); |
4dbfa766 | 2149 | } |
e368fdb6 | 2150 | tcf_block_release(q, block); |
c431f89b | 2151 | return err; |
ed76f5ed VB |
2152 | |
2153 | errout_locked: | |
2154 | mutex_unlock(&chain->filter_chain_lock); | |
2155 | goto errout; | |
c431f89b VB |
2156 | } |
2157 | ||
2158 | static int tc_get_tfilter(struct sk_buff *skb, struct nlmsghdr *n, | |
2159 | struct netlink_ext_ack *extack) | |
2160 | { | |
2161 | struct net *net = sock_net(skb->sk); | |
2162 | struct nlattr *tca[TCA_MAX + 1]; | |
2163 | struct tcmsg *t; | |
2164 | u32 protocol; | |
2165 | u32 prio; | |
2166 | u32 parent; | |
2167 | u32 chain_index; | |
2168 | struct Qdisc *q = NULL; | |
2169 | struct tcf_chain_info chain_info; | |
2170 | struct tcf_chain *chain = NULL; | |
2171 | struct tcf_block *block; | |
2172 | struct tcf_proto *tp = NULL; | |
2173 | unsigned long cl = 0; | |
2174 | void *fh = NULL; | |
2175 | int err; | |
2176 | ||
e331473f | 2177 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack); |
c431f89b VB |
2178 | if (err < 0) |
2179 | return err; | |
2180 | ||
2181 | t = nlmsg_data(n); | |
2182 | protocol = TC_H_MIN(t->tcm_info); | |
2183 | prio = TC_H_MAJ(t->tcm_info); | |
2184 | parent = t->tcm_parent; | |
2185 | ||
2186 | if (prio == 0) { | |
2187 | NL_SET_ERR_MSG(extack, "Invalid filter command with priority of zero"); | |
2188 | return -ENOENT; | |
2189 | } | |
2190 | ||
2191 | /* Find head of filter chain. */ | |
2192 | ||
2193 | block = tcf_block_find(net, &q, &parent, &cl, | |
2194 | t->tcm_ifindex, t->tcm_block_index, extack); | |
2195 | if (IS_ERR(block)) { | |
2196 | err = PTR_ERR(block); | |
2197 | goto errout; | |
2198 | } | |
2199 | ||
2200 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; | |
2201 | if (chain_index > TC_ACT_EXT_VAL_MASK) { | |
2202 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); | |
2203 | err = -EINVAL; | |
2204 | goto errout; | |
2205 | } | |
2206 | chain = tcf_chain_get(block, chain_index, false); | |
2207 | if (!chain) { | |
2208 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); | |
2209 | err = -EINVAL; | |
2210 | goto errout; | |
2211 | } | |
2212 | ||
ed76f5ed | 2213 | mutex_lock(&chain->filter_chain_lock); |
c431f89b VB |
2214 | tp = tcf_chain_tp_find(chain, &chain_info, protocol, |
2215 | prio, false); | |
ed76f5ed | 2216 | mutex_unlock(&chain->filter_chain_lock); |
c431f89b VB |
2217 | if (!tp || IS_ERR(tp)) { |
2218 | NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found"); | |
0e399035 | 2219 | err = tp ? PTR_ERR(tp) : -ENOENT; |
c431f89b VB |
2220 | goto errout; |
2221 | } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) { | |
2222 | NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one"); | |
2223 | err = -EINVAL; | |
2224 | goto errout; | |
2225 | } | |
2226 | ||
2227 | fh = tp->ops->get(tp, t->tcm_handle); | |
2228 | ||
2229 | if (!fh) { | |
2230 | NL_SET_ERR_MSG(extack, "Specified filter handle not found"); | |
2231 | err = -ENOENT; | |
2232 | } else { | |
2233 | err = tfilter_notify(net, skb, n, tp, block, q, parent, | |
2234 | fh, RTM_NEWTFILTER, true); | |
2235 | if (err < 0) | |
2236 | NL_SET_ERR_MSG(extack, "Failed to send filter notify message"); | |
2237 | } | |
2238 | ||
2239 | errout: | |
4dbfa766 VB |
2240 | if (chain) { |
2241 | if (tp && !IS_ERR(tp)) | |
2242 | tcf_proto_put(tp, NULL); | |
c431f89b | 2243 | tcf_chain_put(chain); |
4dbfa766 | 2244 | } |
e368fdb6 | 2245 | tcf_block_release(q, block); |
c431f89b VB |
2246 | return err; |
2247 | } | |
2248 | ||
aa767bfe | 2249 | struct tcf_dump_args { |
1da177e4 LT |
2250 | struct tcf_walker w; |
2251 | struct sk_buff *skb; | |
2252 | struct netlink_callback *cb; | |
7960d1da | 2253 | struct tcf_block *block; |
a10fa201 JP |
2254 | struct Qdisc *q; |
2255 | u32 parent; | |
1da177e4 LT |
2256 | }; |
2257 | ||
8113c095 | 2258 | static int tcf_node_dump(struct tcf_proto *tp, void *n, struct tcf_walker *arg) |
1da177e4 | 2259 | { |
aa767bfe | 2260 | struct tcf_dump_args *a = (void *)arg; |
832d1d5b | 2261 | struct net *net = sock_net(a->skb->sk); |
1da177e4 | 2262 | |
7960d1da | 2263 | return tcf_fill_node(net, a->skb, tp, a->block, a->q, a->parent, |
a10fa201 | 2264 | n, NETLINK_CB(a->cb->skb).portid, |
5a7a5555 JHS |
2265 | a->cb->nlh->nlmsg_seq, NLM_F_MULTI, |
2266 | RTM_NEWTFILTER); | |
1da177e4 LT |
2267 | } |
2268 | ||
a10fa201 JP |
2269 | static bool tcf_chain_dump(struct tcf_chain *chain, struct Qdisc *q, u32 parent, |
2270 | struct sk_buff *skb, struct netlink_callback *cb, | |
acb31fae JP |
2271 | long index_start, long *p_index) |
2272 | { | |
2273 | struct net *net = sock_net(skb->sk); | |
7960d1da | 2274 | struct tcf_block *block = chain->block; |
acb31fae | 2275 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
fe2923af | 2276 | struct tcf_proto *tp, *tp_prev; |
acb31fae | 2277 | struct tcf_dump_args arg; |
acb31fae | 2278 | |
fe2923af VB |
2279 | for (tp = __tcf_get_next_proto(chain, NULL); |
2280 | tp; | |
2281 | tp_prev = tp, | |
2282 | tp = __tcf_get_next_proto(chain, tp), | |
2283 | tcf_proto_put(tp_prev, NULL), | |
2284 | (*p_index)++) { | |
acb31fae JP |
2285 | if (*p_index < index_start) |
2286 | continue; | |
2287 | if (TC_H_MAJ(tcm->tcm_info) && | |
2288 | TC_H_MAJ(tcm->tcm_info) != tp->prio) | |
2289 | continue; | |
2290 | if (TC_H_MIN(tcm->tcm_info) && | |
2291 | TC_H_MIN(tcm->tcm_info) != tp->protocol) | |
2292 | continue; | |
2293 | if (*p_index > index_start) | |
2294 | memset(&cb->args[1], 0, | |
2295 | sizeof(cb->args) - sizeof(cb->args[0])); | |
2296 | if (cb->args[1] == 0) { | |
53189183 | 2297 | if (tcf_fill_node(net, skb, tp, block, q, parent, NULL, |
acb31fae JP |
2298 | NETLINK_CB(cb->skb).portid, |
2299 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
2300 | RTM_NEWTFILTER) <= 0) | |
fe2923af | 2301 | goto errout; |
acb31fae JP |
2302 | |
2303 | cb->args[1] = 1; | |
2304 | } | |
2305 | if (!tp->ops->walk) | |
2306 | continue; | |
2307 | arg.w.fn = tcf_node_dump; | |
2308 | arg.skb = skb; | |
2309 | arg.cb = cb; | |
7960d1da | 2310 | arg.block = block; |
a10fa201 JP |
2311 | arg.q = q; |
2312 | arg.parent = parent; | |
acb31fae JP |
2313 | arg.w.stop = 0; |
2314 | arg.w.skip = cb->args[1] - 1; | |
2315 | arg.w.count = 0; | |
01683a14 | 2316 | arg.w.cookie = cb->args[2]; |
acb31fae | 2317 | tp->ops->walk(tp, &arg.w); |
01683a14 | 2318 | cb->args[2] = arg.w.cookie; |
acb31fae JP |
2319 | cb->args[1] = arg.w.count + 1; |
2320 | if (arg.w.stop) | |
fe2923af | 2321 | goto errout; |
acb31fae | 2322 | } |
5bc17018 | 2323 | return true; |
fe2923af VB |
2324 | |
2325 | errout: | |
2326 | tcf_proto_put(tp, NULL); | |
2327 | return false; | |
acb31fae JP |
2328 | } |
2329 | ||
bd27a875 | 2330 | /* called with RTNL */ |
1da177e4 LT |
2331 | static int tc_dump_tfilter(struct sk_buff *skb, struct netlink_callback *cb) |
2332 | { | |
bbf73830 | 2333 | struct tcf_chain *chain, *chain_prev; |
3b1e0a65 | 2334 | struct net *net = sock_net(skb->sk); |
5bc17018 | 2335 | struct nlattr *tca[TCA_MAX + 1]; |
7960d1da | 2336 | struct Qdisc *q = NULL; |
6529eaba | 2337 | struct tcf_block *block; |
942b8165 | 2338 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
acb31fae JP |
2339 | long index_start; |
2340 | long index; | |
a10fa201 | 2341 | u32 parent; |
5bc17018 | 2342 | int err; |
1da177e4 | 2343 | |
573ce260 | 2344 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) |
1da177e4 | 2345 | return skb->len; |
5bc17018 | 2346 | |
dac9c979 DA |
2347 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, NULL, |
2348 | cb->extack); | |
5bc17018 JP |
2349 | if (err) |
2350 | return err; | |
2351 | ||
7960d1da | 2352 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { |
787ce6d0 | 2353 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
7960d1da JP |
2354 | if (!block) |
2355 | goto out; | |
d680b352 JP |
2356 | /* If we work with block index, q is NULL and parent value |
2357 | * will never be used in the following code. The check | |
2358 | * in tcf_fill_node prevents it. However, compiler does not | |
2359 | * see that far, so set parent to zero to silence the warning | |
2360 | * about parent being uninitialized. | |
2361 | */ | |
2362 | parent = 0; | |
a10fa201 | 2363 | } else { |
7960d1da JP |
2364 | const struct Qdisc_class_ops *cops; |
2365 | struct net_device *dev; | |
2366 | unsigned long cl = 0; | |
2367 | ||
2368 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); | |
2369 | if (!dev) | |
2370 | return skb->len; | |
2371 | ||
2372 | parent = tcm->tcm_parent; | |
2373 | if (!parent) { | |
2374 | q = dev->qdisc; | |
2375 | parent = q->handle; | |
2376 | } else { | |
2377 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); | |
2378 | } | |
2379 | if (!q) | |
2380 | goto out; | |
2381 | cops = q->ops->cl_ops; | |
2382 | if (!cops) | |
143976ce | 2383 | goto out; |
7960d1da JP |
2384 | if (!cops->tcf_block) |
2385 | goto out; | |
2386 | if (TC_H_MIN(tcm->tcm_parent)) { | |
2387 | cl = cops->find(q, tcm->tcm_parent); | |
2388 | if (cl == 0) | |
2389 | goto out; | |
2390 | } | |
2391 | block = cops->tcf_block(q, cl, NULL); | |
2392 | if (!block) | |
2393 | goto out; | |
2394 | if (tcf_block_shared(block)) | |
2395 | q = NULL; | |
1da177e4 | 2396 | } |
1da177e4 | 2397 | |
acb31fae JP |
2398 | index_start = cb->args[0]; |
2399 | index = 0; | |
5bc17018 | 2400 | |
bbf73830 VB |
2401 | for (chain = __tcf_get_next_chain(block, NULL); |
2402 | chain; | |
2403 | chain_prev = chain, | |
2404 | chain = __tcf_get_next_chain(block, chain), | |
2405 | tcf_chain_put(chain_prev)) { | |
5bc17018 JP |
2406 | if (tca[TCA_CHAIN] && |
2407 | nla_get_u32(tca[TCA_CHAIN]) != chain->index) | |
2408 | continue; | |
a10fa201 | 2409 | if (!tcf_chain_dump(chain, q, parent, skb, cb, |
5ae437ad | 2410 | index_start, &index)) { |
bbf73830 | 2411 | tcf_chain_put(chain); |
5ae437ad | 2412 | err = -EMSGSIZE; |
5bc17018 | 2413 | break; |
5ae437ad | 2414 | } |
5bc17018 JP |
2415 | } |
2416 | ||
787ce6d0 VB |
2417 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
2418 | tcf_block_refcnt_put(block); | |
acb31fae | 2419 | cb->args[0] = index; |
1da177e4 | 2420 | |
1da177e4 | 2421 | out: |
5ae437ad RK |
2422 | /* If we did no progress, the error (EMSGSIZE) is real */ |
2423 | if (skb->len == 0 && err) | |
2424 | return err; | |
1da177e4 LT |
2425 | return skb->len; |
2426 | } | |
2427 | ||
a5654820 VB |
2428 | static int tc_chain_fill_node(const struct tcf_proto_ops *tmplt_ops, |
2429 | void *tmplt_priv, u32 chain_index, | |
2430 | struct net *net, struct sk_buff *skb, | |
2431 | struct tcf_block *block, | |
32a4f5ec JP |
2432 | u32 portid, u32 seq, u16 flags, int event) |
2433 | { | |
2434 | unsigned char *b = skb_tail_pointer(skb); | |
9f407f17 | 2435 | const struct tcf_proto_ops *ops; |
32a4f5ec JP |
2436 | struct nlmsghdr *nlh; |
2437 | struct tcmsg *tcm; | |
9f407f17 JP |
2438 | void *priv; |
2439 | ||
a5654820 VB |
2440 | ops = tmplt_ops; |
2441 | priv = tmplt_priv; | |
32a4f5ec JP |
2442 | |
2443 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); | |
2444 | if (!nlh) | |
2445 | goto out_nlmsg_trim; | |
2446 | tcm = nlmsg_data(nlh); | |
2447 | tcm->tcm_family = AF_UNSPEC; | |
2448 | tcm->tcm__pad1 = 0; | |
2449 | tcm->tcm__pad2 = 0; | |
2450 | tcm->tcm_handle = 0; | |
2451 | if (block->q) { | |
2452 | tcm->tcm_ifindex = qdisc_dev(block->q)->ifindex; | |
2453 | tcm->tcm_parent = block->q->handle; | |
2454 | } else { | |
2455 | tcm->tcm_ifindex = TCM_IFINDEX_MAGIC_BLOCK; | |
2456 | tcm->tcm_block_index = block->index; | |
2457 | } | |
2458 | ||
a5654820 | 2459 | if (nla_put_u32(skb, TCA_CHAIN, chain_index)) |
32a4f5ec JP |
2460 | goto nla_put_failure; |
2461 | ||
9f407f17 JP |
2462 | if (ops) { |
2463 | if (nla_put_string(skb, TCA_KIND, ops->kind)) | |
2464 | goto nla_put_failure; | |
2465 | if (ops->tmplt_dump(skb, net, priv) < 0) | |
2466 | goto nla_put_failure; | |
2467 | } | |
2468 | ||
32a4f5ec JP |
2469 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
2470 | return skb->len; | |
2471 | ||
2472 | out_nlmsg_trim: | |
2473 | nla_put_failure: | |
2474 | nlmsg_trim(skb, b); | |
2475 | return -EMSGSIZE; | |
2476 | } | |
2477 | ||
2478 | static int tc_chain_notify(struct tcf_chain *chain, struct sk_buff *oskb, | |
2479 | u32 seq, u16 flags, int event, bool unicast) | |
2480 | { | |
2481 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
2482 | struct tcf_block *block = chain->block; | |
2483 | struct net *net = block->net; | |
2484 | struct sk_buff *skb; | |
2485 | ||
2486 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
2487 | if (!skb) | |
2488 | return -ENOBUFS; | |
2489 | ||
a5654820 VB |
2490 | if (tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, |
2491 | chain->index, net, skb, block, portid, | |
32a4f5ec JP |
2492 | seq, flags, event) <= 0) { |
2493 | kfree_skb(skb); | |
2494 | return -EINVAL; | |
2495 | } | |
2496 | ||
2497 | if (unicast) | |
2498 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
2499 | ||
2500 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); | |
2501 | } | |
2502 | ||
a5654820 VB |
2503 | static int tc_chain_notify_delete(const struct tcf_proto_ops *tmplt_ops, |
2504 | void *tmplt_priv, u32 chain_index, | |
2505 | struct tcf_block *block, struct sk_buff *oskb, | |
2506 | u32 seq, u16 flags, bool unicast) | |
2507 | { | |
2508 | u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; | |
2509 | struct net *net = block->net; | |
2510 | struct sk_buff *skb; | |
2511 | ||
2512 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
2513 | if (!skb) | |
2514 | return -ENOBUFS; | |
2515 | ||
2516 | if (tc_chain_fill_node(tmplt_ops, tmplt_priv, chain_index, net, skb, | |
2517 | block, portid, seq, flags, RTM_DELCHAIN) <= 0) { | |
2518 | kfree_skb(skb); | |
2519 | return -EINVAL; | |
2520 | } | |
2521 | ||
2522 | if (unicast) | |
2523 | return netlink_unicast(net->rtnl, skb, portid, MSG_DONTWAIT); | |
2524 | ||
2525 | return rtnetlink_send(skb, net, portid, RTNLGRP_TC, flags & NLM_F_ECHO); | |
2526 | } | |
2527 | ||
9f407f17 JP |
2528 | static int tc_chain_tmplt_add(struct tcf_chain *chain, struct net *net, |
2529 | struct nlattr **tca, | |
2530 | struct netlink_ext_ack *extack) | |
2531 | { | |
2532 | const struct tcf_proto_ops *ops; | |
2533 | void *tmplt_priv; | |
2534 | ||
2535 | /* If kind is not set, user did not specify template. */ | |
2536 | if (!tca[TCA_KIND]) | |
2537 | return 0; | |
2538 | ||
2539 | ops = tcf_proto_lookup_ops(nla_data(tca[TCA_KIND]), extack); | |
2540 | if (IS_ERR(ops)) | |
2541 | return PTR_ERR(ops); | |
2542 | if (!ops->tmplt_create || !ops->tmplt_destroy || !ops->tmplt_dump) { | |
2543 | NL_SET_ERR_MSG(extack, "Chain templates are not supported with specified classifier"); | |
2544 | return -EOPNOTSUPP; | |
2545 | } | |
2546 | ||
2547 | tmplt_priv = ops->tmplt_create(net, chain, tca, extack); | |
2548 | if (IS_ERR(tmplt_priv)) { | |
2549 | module_put(ops->owner); | |
2550 | return PTR_ERR(tmplt_priv); | |
2551 | } | |
2552 | chain->tmplt_ops = ops; | |
2553 | chain->tmplt_priv = tmplt_priv; | |
2554 | return 0; | |
2555 | } | |
2556 | ||
a5654820 VB |
2557 | static void tc_chain_tmplt_del(const struct tcf_proto_ops *tmplt_ops, |
2558 | void *tmplt_priv) | |
9f407f17 | 2559 | { |
9f407f17 | 2560 | /* If template ops are set, no work to do for us. */ |
a5654820 | 2561 | if (!tmplt_ops) |
9f407f17 JP |
2562 | return; |
2563 | ||
a5654820 VB |
2564 | tmplt_ops->tmplt_destroy(tmplt_priv); |
2565 | module_put(tmplt_ops->owner); | |
9f407f17 JP |
2566 | } |
2567 | ||
32a4f5ec JP |
2568 | /* Add/delete/get a chain */ |
2569 | ||
2570 | static int tc_ctl_chain(struct sk_buff *skb, struct nlmsghdr *n, | |
2571 | struct netlink_ext_ack *extack) | |
2572 | { | |
2573 | struct net *net = sock_net(skb->sk); | |
2574 | struct nlattr *tca[TCA_MAX + 1]; | |
2575 | struct tcmsg *t; | |
2576 | u32 parent; | |
2577 | u32 chain_index; | |
2578 | struct Qdisc *q = NULL; | |
2579 | struct tcf_chain *chain = NULL; | |
2580 | struct tcf_block *block; | |
2581 | unsigned long cl; | |
2582 | int err; | |
2583 | ||
2584 | if (n->nlmsg_type != RTM_GETCHAIN && | |
2585 | !netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) | |
2586 | return -EPERM; | |
2587 | ||
2588 | replay: | |
e331473f | 2589 | err = nlmsg_parse(n, sizeof(*t), tca, TCA_MAX, rtm_tca_policy, extack); |
32a4f5ec JP |
2590 | if (err < 0) |
2591 | return err; | |
2592 | ||
2593 | t = nlmsg_data(n); | |
2594 | parent = t->tcm_parent; | |
2595 | cl = 0; | |
2596 | ||
2597 | block = tcf_block_find(net, &q, &parent, &cl, | |
2598 | t->tcm_ifindex, t->tcm_block_index, extack); | |
2599 | if (IS_ERR(block)) | |
2600 | return PTR_ERR(block); | |
2601 | ||
2602 | chain_index = tca[TCA_CHAIN] ? nla_get_u32(tca[TCA_CHAIN]) : 0; | |
2603 | if (chain_index > TC_ACT_EXT_VAL_MASK) { | |
2604 | NL_SET_ERR_MSG(extack, "Specified chain index exceeds upper limit"); | |
e368fdb6 VB |
2605 | err = -EINVAL; |
2606 | goto errout_block; | |
32a4f5ec | 2607 | } |
2cbfab07 VB |
2608 | |
2609 | mutex_lock(&block->lock); | |
32a4f5ec JP |
2610 | chain = tcf_chain_lookup(block, chain_index); |
2611 | if (n->nlmsg_type == RTM_NEWCHAIN) { | |
2612 | if (chain) { | |
3d32f4c5 | 2613 | if (tcf_chain_held_by_acts_only(chain)) { |
1f3ed383 | 2614 | /* The chain exists only because there is |
3d32f4c5 | 2615 | * some action referencing it. |
1f3ed383 JP |
2616 | */ |
2617 | tcf_chain_hold(chain); | |
2618 | } else { | |
2619 | NL_SET_ERR_MSG(extack, "Filter chain already exists"); | |
e368fdb6 | 2620 | err = -EEXIST; |
2cbfab07 | 2621 | goto errout_block_locked; |
1f3ed383 JP |
2622 | } |
2623 | } else { | |
2624 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
2625 | NL_SET_ERR_MSG(extack, "Need both RTM_NEWCHAIN and NLM_F_CREATE to create a new chain"); | |
e368fdb6 | 2626 | err = -ENOENT; |
2cbfab07 | 2627 | goto errout_block_locked; |
1f3ed383 JP |
2628 | } |
2629 | chain = tcf_chain_create(block, chain_index); | |
2630 | if (!chain) { | |
2631 | NL_SET_ERR_MSG(extack, "Failed to create filter chain"); | |
e368fdb6 | 2632 | err = -ENOMEM; |
2cbfab07 | 2633 | goto errout_block_locked; |
1f3ed383 | 2634 | } |
32a4f5ec JP |
2635 | } |
2636 | } else { | |
3d32f4c5 | 2637 | if (!chain || tcf_chain_held_by_acts_only(chain)) { |
32a4f5ec | 2638 | NL_SET_ERR_MSG(extack, "Cannot find specified filter chain"); |
e368fdb6 | 2639 | err = -EINVAL; |
2cbfab07 | 2640 | goto errout_block_locked; |
32a4f5ec JP |
2641 | } |
2642 | tcf_chain_hold(chain); | |
2643 | } | |
2644 | ||
2cbfab07 VB |
2645 | if (n->nlmsg_type == RTM_NEWCHAIN) { |
2646 | /* Modifying chain requires holding parent block lock. In case | |
2647 | * the chain was successfully added, take a reference to the | |
2648 | * chain. This ensures that an empty chain does not disappear at | |
2649 | * the end of this function. | |
2650 | */ | |
2651 | tcf_chain_hold(chain); | |
2652 | chain->explicitly_created = true; | |
2653 | } | |
2654 | mutex_unlock(&block->lock); | |
2655 | ||
32a4f5ec JP |
2656 | switch (n->nlmsg_type) { |
2657 | case RTM_NEWCHAIN: | |
9f407f17 | 2658 | err = tc_chain_tmplt_add(chain, net, tca, extack); |
2cbfab07 VB |
2659 | if (err) { |
2660 | tcf_chain_put_explicitly_created(chain); | |
9f407f17 | 2661 | goto errout; |
2cbfab07 VB |
2662 | } |
2663 | ||
32a4f5ec JP |
2664 | tc_chain_notify(chain, NULL, 0, NLM_F_CREATE | NLM_F_EXCL, |
2665 | RTM_NEWCHAIN, false); | |
2666 | break; | |
2667 | case RTM_DELCHAIN: | |
f5b9bac7 CW |
2668 | tfilter_notify_chain(net, skb, block, q, parent, n, |
2669 | chain, RTM_DELTFILTER); | |
32a4f5ec JP |
2670 | /* Flush the chain first as the user requested chain removal. */ |
2671 | tcf_chain_flush(chain); | |
2672 | /* In case the chain was successfully deleted, put a reference | |
2673 | * to the chain previously taken during addition. | |
2674 | */ | |
2675 | tcf_chain_put_explicitly_created(chain); | |
2676 | break; | |
2677 | case RTM_GETCHAIN: | |
32a4f5ec JP |
2678 | err = tc_chain_notify(chain, skb, n->nlmsg_seq, |
2679 | n->nlmsg_seq, n->nlmsg_type, true); | |
2680 | if (err < 0) | |
2681 | NL_SET_ERR_MSG(extack, "Failed to send chain notify message"); | |
2682 | break; | |
2683 | default: | |
2684 | err = -EOPNOTSUPP; | |
2685 | NL_SET_ERR_MSG(extack, "Unsupported message type"); | |
2686 | goto errout; | |
2687 | } | |
2688 | ||
2689 | errout: | |
2690 | tcf_chain_put(chain); | |
e368fdb6 VB |
2691 | errout_block: |
2692 | tcf_block_release(q, block); | |
32a4f5ec JP |
2693 | if (err == -EAGAIN) |
2694 | /* Replay the request. */ | |
2695 | goto replay; | |
2696 | return err; | |
2cbfab07 VB |
2697 | |
2698 | errout_block_locked: | |
2699 | mutex_unlock(&block->lock); | |
2700 | goto errout_block; | |
32a4f5ec JP |
2701 | } |
2702 | ||
2703 | /* called with RTNL */ | |
2704 | static int tc_dump_chain(struct sk_buff *skb, struct netlink_callback *cb) | |
2705 | { | |
bbf73830 | 2706 | struct tcf_chain *chain, *chain_prev; |
32a4f5ec JP |
2707 | struct net *net = sock_net(skb->sk); |
2708 | struct nlattr *tca[TCA_MAX + 1]; | |
2709 | struct Qdisc *q = NULL; | |
2710 | struct tcf_block *block; | |
32a4f5ec JP |
2711 | struct tcmsg *tcm = nlmsg_data(cb->nlh); |
2712 | long index_start; | |
2713 | long index; | |
2714 | u32 parent; | |
2715 | int err; | |
2716 | ||
2717 | if (nlmsg_len(cb->nlh) < sizeof(*tcm)) | |
2718 | return skb->len; | |
2719 | ||
e331473f | 2720 | err = nlmsg_parse(cb->nlh, sizeof(*tcm), tca, TCA_MAX, rtm_tca_policy, |
dac9c979 | 2721 | cb->extack); |
32a4f5ec JP |
2722 | if (err) |
2723 | return err; | |
2724 | ||
2725 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) { | |
787ce6d0 | 2726 | block = tcf_block_refcnt_get(net, tcm->tcm_block_index); |
32a4f5ec JP |
2727 | if (!block) |
2728 | goto out; | |
2729 | /* If we work with block index, q is NULL and parent value | |
2730 | * will never be used in the following code. The check | |
2731 | * in tcf_fill_node prevents it. However, compiler does not | |
2732 | * see that far, so set parent to zero to silence the warning | |
2733 | * about parent being uninitialized. | |
2734 | */ | |
2735 | parent = 0; | |
2736 | } else { | |
2737 | const struct Qdisc_class_ops *cops; | |
2738 | struct net_device *dev; | |
2739 | unsigned long cl = 0; | |
2740 | ||
2741 | dev = __dev_get_by_index(net, tcm->tcm_ifindex); | |
2742 | if (!dev) | |
2743 | return skb->len; | |
2744 | ||
2745 | parent = tcm->tcm_parent; | |
2746 | if (!parent) { | |
2747 | q = dev->qdisc; | |
2748 | parent = q->handle; | |
2749 | } else { | |
2750 | q = qdisc_lookup(dev, TC_H_MAJ(tcm->tcm_parent)); | |
2751 | } | |
2752 | if (!q) | |
2753 | goto out; | |
2754 | cops = q->ops->cl_ops; | |
2755 | if (!cops) | |
2756 | goto out; | |
2757 | if (!cops->tcf_block) | |
2758 | goto out; | |
2759 | if (TC_H_MIN(tcm->tcm_parent)) { | |
2760 | cl = cops->find(q, tcm->tcm_parent); | |
2761 | if (cl == 0) | |
2762 | goto out; | |
2763 | } | |
2764 | block = cops->tcf_block(q, cl, NULL); | |
2765 | if (!block) | |
2766 | goto out; | |
2767 | if (tcf_block_shared(block)) | |
2768 | q = NULL; | |
2769 | } | |
2770 | ||
2771 | index_start = cb->args[0]; | |
2772 | index = 0; | |
2773 | ||
bbf73830 VB |
2774 | for (chain = __tcf_get_next_chain(block, NULL); |
2775 | chain; | |
2776 | chain_prev = chain, | |
2777 | chain = __tcf_get_next_chain(block, chain), | |
2778 | tcf_chain_put(chain_prev)) { | |
32a4f5ec JP |
2779 | if ((tca[TCA_CHAIN] && |
2780 | nla_get_u32(tca[TCA_CHAIN]) != chain->index)) | |
2781 | continue; | |
2782 | if (index < index_start) { | |
2783 | index++; | |
2784 | continue; | |
2785 | } | |
a5654820 VB |
2786 | err = tc_chain_fill_node(chain->tmplt_ops, chain->tmplt_priv, |
2787 | chain->index, net, skb, block, | |
32a4f5ec JP |
2788 | NETLINK_CB(cb->skb).portid, |
2789 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
2790 | RTM_NEWCHAIN); | |
bbf73830 VB |
2791 | if (err <= 0) { |
2792 | tcf_chain_put(chain); | |
32a4f5ec | 2793 | break; |
bbf73830 | 2794 | } |
32a4f5ec JP |
2795 | index++; |
2796 | } | |
2797 | ||
787ce6d0 VB |
2798 | if (tcm->tcm_ifindex == TCM_IFINDEX_MAGIC_BLOCK) |
2799 | tcf_block_refcnt_put(block); | |
32a4f5ec JP |
2800 | cb->args[0] = index; |
2801 | ||
2802 | out: | |
2803 | /* If we did no progress, the error (EMSGSIZE) is real */ | |
2804 | if (skb->len == 0 && err) | |
2805 | return err; | |
2806 | return skb->len; | |
2807 | } | |
2808 | ||
18d0264f | 2809 | void tcf_exts_destroy(struct tcf_exts *exts) |
1da177e4 LT |
2810 | { |
2811 | #ifdef CONFIG_NET_CLS_ACT | |
90b73b77 | 2812 | tcf_action_destroy(exts->actions, TCA_ACT_UNBIND); |
22dc13c8 WC |
2813 | kfree(exts->actions); |
2814 | exts->nr_actions = 0; | |
1da177e4 LT |
2815 | #endif |
2816 | } | |
aa767bfe | 2817 | EXPORT_SYMBOL(tcf_exts_destroy); |
1da177e4 | 2818 | |
c1b52739 | 2819 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, struct nlattr **tb, |
50a56190 AA |
2820 | struct nlattr *rate_tlv, struct tcf_exts *exts, bool ovr, |
2821 | struct netlink_ext_ack *extack) | |
1da177e4 | 2822 | { |
1da177e4 LT |
2823 | #ifdef CONFIG_NET_CLS_ACT |
2824 | { | |
1da177e4 | 2825 | struct tc_action *act; |
d04e6990 | 2826 | size_t attr_size = 0; |
1da177e4 | 2827 | |
5da57f42 | 2828 | if (exts->police && tb[exts->police]) { |
9fb9f251 JP |
2829 | act = tcf_action_init_1(net, tp, tb[exts->police], |
2830 | rate_tlv, "police", ovr, | |
789871bb | 2831 | TCA_ACT_BIND, true, extack); |
ab27cfb8 PM |
2832 | if (IS_ERR(act)) |
2833 | return PTR_ERR(act); | |
1da177e4 | 2834 | |
33be6271 | 2835 | act->type = exts->type = TCA_OLD_COMPAT; |
22dc13c8 WC |
2836 | exts->actions[0] = act; |
2837 | exts->nr_actions = 1; | |
5da57f42 | 2838 | } else if (exts->action && tb[exts->action]) { |
90b73b77 | 2839 | int err; |
22dc13c8 | 2840 | |
9fb9f251 JP |
2841 | err = tcf_action_init(net, tp, tb[exts->action], |
2842 | rate_tlv, NULL, ovr, TCA_ACT_BIND, | |
90b73b77 | 2843 | exts->actions, &attr_size, true, |
789871bb | 2844 | extack); |
90b73b77 | 2845 | if (err < 0) |
33be6271 | 2846 | return err; |
90b73b77 | 2847 | exts->nr_actions = err; |
1da177e4 | 2848 | } |
e4b95c41 | 2849 | exts->net = net; |
1da177e4 | 2850 | } |
1da177e4 | 2851 | #else |
5da57f42 | 2852 | if ((exts->action && tb[exts->action]) || |
50a56190 AA |
2853 | (exts->police && tb[exts->police])) { |
2854 | NL_SET_ERR_MSG(extack, "Classifier actions are not supported per compile options (CONFIG_NET_CLS_ACT)"); | |
1da177e4 | 2855 | return -EOPNOTSUPP; |
50a56190 | 2856 | } |
1da177e4 LT |
2857 | #endif |
2858 | ||
2859 | return 0; | |
2860 | } | |
aa767bfe | 2861 | EXPORT_SYMBOL(tcf_exts_validate); |
1da177e4 | 2862 | |
9b0d4446 | 2863 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src) |
1da177e4 LT |
2864 | { |
2865 | #ifdef CONFIG_NET_CLS_ACT | |
22dc13c8 WC |
2866 | struct tcf_exts old = *dst; |
2867 | ||
9b0d4446 | 2868 | *dst = *src; |
22dc13c8 | 2869 | tcf_exts_destroy(&old); |
1da177e4 LT |
2870 | #endif |
2871 | } | |
aa767bfe | 2872 | EXPORT_SYMBOL(tcf_exts_change); |
1da177e4 | 2873 | |
22dc13c8 WC |
2874 | #ifdef CONFIG_NET_CLS_ACT |
2875 | static struct tc_action *tcf_exts_first_act(struct tcf_exts *exts) | |
2876 | { | |
2877 | if (exts->nr_actions == 0) | |
2878 | return NULL; | |
2879 | else | |
2880 | return exts->actions[0]; | |
2881 | } | |
2882 | #endif | |
33be6271 | 2883 | |
5da57f42 | 2884 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
2885 | { |
2886 | #ifdef CONFIG_NET_CLS_ACT | |
9cc63db5 CW |
2887 | struct nlattr *nest; |
2888 | ||
978dfd8d | 2889 | if (exts->action && tcf_exts_has_actions(exts)) { |
1da177e4 LT |
2890 | /* |
2891 | * again for backward compatible mode - we want | |
2892 | * to work with both old and new modes of entering | |
2893 | * tc data even if iproute2 was newer - jhs | |
2894 | */ | |
33be6271 | 2895 | if (exts->type != TCA_OLD_COMPAT) { |
5da57f42 | 2896 | nest = nla_nest_start(skb, exts->action); |
4b3550ef PM |
2897 | if (nest == NULL) |
2898 | goto nla_put_failure; | |
22dc13c8 | 2899 | |
90b73b77 | 2900 | if (tcf_action_dump(skb, exts->actions, 0, 0) < 0) |
add93b61 | 2901 | goto nla_put_failure; |
4b3550ef | 2902 | nla_nest_end(skb, nest); |
5da57f42 | 2903 | } else if (exts->police) { |
33be6271 | 2904 | struct tc_action *act = tcf_exts_first_act(exts); |
5da57f42 | 2905 | nest = nla_nest_start(skb, exts->police); |
63acd680 | 2906 | if (nest == NULL || !act) |
4b3550ef | 2907 | goto nla_put_failure; |
33be6271 | 2908 | if (tcf_action_dump_old(skb, act, 0, 0) < 0) |
add93b61 | 2909 | goto nla_put_failure; |
4b3550ef | 2910 | nla_nest_end(skb, nest); |
1da177e4 LT |
2911 | } |
2912 | } | |
1da177e4 | 2913 | return 0; |
9cc63db5 CW |
2914 | |
2915 | nla_put_failure: | |
2916 | nla_nest_cancel(skb, nest); | |
1da177e4 | 2917 | return -1; |
9cc63db5 CW |
2918 | #else |
2919 | return 0; | |
2920 | #endif | |
1da177e4 | 2921 | } |
aa767bfe | 2922 | EXPORT_SYMBOL(tcf_exts_dump); |
1da177e4 | 2923 | |
aa767bfe | 2924 | |
5da57f42 | 2925 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts) |
1da177e4 LT |
2926 | { |
2927 | #ifdef CONFIG_NET_CLS_ACT | |
33be6271 | 2928 | struct tc_action *a = tcf_exts_first_act(exts); |
b057df24 | 2929 | if (a != NULL && tcf_action_copy_stats(skb, a, 1) < 0) |
33be6271 | 2930 | return -1; |
1da177e4 LT |
2931 | #endif |
2932 | return 0; | |
1da177e4 | 2933 | } |
aa767bfe | 2934 | EXPORT_SYMBOL(tcf_exts_dump_stats); |
1da177e4 | 2935 | |
aeb3fecd CW |
2936 | int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, |
2937 | void *type_data, bool err_stop) | |
717503b9 | 2938 | { |
aeb3fecd CW |
2939 | struct tcf_block_cb *block_cb; |
2940 | int ok_count = 0; | |
2941 | int err; | |
2942 | ||
2943 | /* Make sure all netdevs sharing this block are offload-capable. */ | |
2944 | if (block->nooffloaddevcnt && err_stop) | |
2945 | return -EOPNOTSUPP; | |
2946 | ||
2947 | list_for_each_entry(block_cb, &block->cb_list, list) { | |
2948 | err = block_cb->cb(type, type_data, block_cb->cb_priv); | |
2949 | if (err) { | |
2950 | if (err_stop) | |
2951 | return err; | |
2952 | } else { | |
2953 | ok_count++; | |
2954 | } | |
2955 | } | |
2956 | return ok_count; | |
717503b9 JP |
2957 | } |
2958 | EXPORT_SYMBOL(tc_setup_cb_call); | |
b3f55bdd | 2959 | |
3a7b6861 PNA |
2960 | int tc_setup_flow_action(struct flow_action *flow_action, |
2961 | const struct tcf_exts *exts) | |
2962 | { | |
2963 | const struct tc_action *act; | |
2964 | int i, j, k; | |
2965 | ||
2966 | if (!exts) | |
2967 | return 0; | |
2968 | ||
2969 | j = 0; | |
2970 | tcf_exts_for_each_action(i, act, exts) { | |
2971 | struct flow_action_entry *entry; | |
2972 | ||
2973 | entry = &flow_action->entries[j]; | |
2974 | if (is_tcf_gact_ok(act)) { | |
2975 | entry->id = FLOW_ACTION_ACCEPT; | |
2976 | } else if (is_tcf_gact_shot(act)) { | |
2977 | entry->id = FLOW_ACTION_DROP; | |
2978 | } else if (is_tcf_gact_trap(act)) { | |
2979 | entry->id = FLOW_ACTION_TRAP; | |
2980 | } else if (is_tcf_gact_goto_chain(act)) { | |
2981 | entry->id = FLOW_ACTION_GOTO; | |
2982 | entry->chain_index = tcf_gact_goto_chain_index(act); | |
2983 | } else if (is_tcf_mirred_egress_redirect(act)) { | |
2984 | entry->id = FLOW_ACTION_REDIRECT; | |
2985 | entry->dev = tcf_mirred_dev(act); | |
2986 | } else if (is_tcf_mirred_egress_mirror(act)) { | |
2987 | entry->id = FLOW_ACTION_MIRRED; | |
2988 | entry->dev = tcf_mirred_dev(act); | |
2989 | } else if (is_tcf_vlan(act)) { | |
2990 | switch (tcf_vlan_action(act)) { | |
2991 | case TCA_VLAN_ACT_PUSH: | |
2992 | entry->id = FLOW_ACTION_VLAN_PUSH; | |
2993 | entry->vlan.vid = tcf_vlan_push_vid(act); | |
2994 | entry->vlan.proto = tcf_vlan_push_proto(act); | |
2995 | entry->vlan.prio = tcf_vlan_push_prio(act); | |
2996 | break; | |
2997 | case TCA_VLAN_ACT_POP: | |
2998 | entry->id = FLOW_ACTION_VLAN_POP; | |
2999 | break; | |
3000 | case TCA_VLAN_ACT_MODIFY: | |
3001 | entry->id = FLOW_ACTION_VLAN_MANGLE; | |
3002 | entry->vlan.vid = tcf_vlan_push_vid(act); | |
3003 | entry->vlan.proto = tcf_vlan_push_proto(act); | |
3004 | entry->vlan.prio = tcf_vlan_push_prio(act); | |
3005 | break; | |
3006 | default: | |
3007 | goto err_out; | |
3008 | } | |
3009 | } else if (is_tcf_tunnel_set(act)) { | |
3010 | entry->id = FLOW_ACTION_TUNNEL_ENCAP; | |
3011 | entry->tunnel = tcf_tunnel_info(act); | |
3012 | } else if (is_tcf_tunnel_release(act)) { | |
3013 | entry->id = FLOW_ACTION_TUNNEL_DECAP; | |
3014 | entry->tunnel = tcf_tunnel_info(act); | |
3015 | } else if (is_tcf_pedit(act)) { | |
3016 | for (k = 0; k < tcf_pedit_nkeys(act); k++) { | |
3017 | switch (tcf_pedit_cmd(act, k)) { | |
3018 | case TCA_PEDIT_KEY_EX_CMD_SET: | |
3019 | entry->id = FLOW_ACTION_MANGLE; | |
3020 | break; | |
3021 | case TCA_PEDIT_KEY_EX_CMD_ADD: | |
3022 | entry->id = FLOW_ACTION_ADD; | |
3023 | break; | |
3024 | default: | |
3025 | goto err_out; | |
3026 | } | |
3027 | entry->mangle.htype = tcf_pedit_htype(act, k); | |
3028 | entry->mangle.mask = tcf_pedit_mask(act, k); | |
3029 | entry->mangle.val = tcf_pedit_val(act, k); | |
3030 | entry->mangle.offset = tcf_pedit_offset(act, k); | |
3031 | entry = &flow_action->entries[++j]; | |
3032 | } | |
3033 | } else if (is_tcf_csum(act)) { | |
3034 | entry->id = FLOW_ACTION_CSUM; | |
3035 | entry->csum_flags = tcf_csum_update_flags(act); | |
3036 | } else if (is_tcf_skbedit_mark(act)) { | |
3037 | entry->id = FLOW_ACTION_MARK; | |
3038 | entry->mark = tcf_skbedit_mark(act); | |
3039 | } else { | |
3040 | goto err_out; | |
3041 | } | |
3042 | ||
3043 | if (!is_tcf_pedit(act)) | |
3044 | j++; | |
3045 | } | |
3046 | return 0; | |
3047 | err_out: | |
3048 | return -EOPNOTSUPP; | |
3049 | } | |
3050 | EXPORT_SYMBOL(tc_setup_flow_action); | |
3051 | ||
e3ab786b PNA |
3052 | unsigned int tcf_exts_num_actions(struct tcf_exts *exts) |
3053 | { | |
3054 | unsigned int num_acts = 0; | |
3055 | struct tc_action *act; | |
3056 | int i; | |
3057 | ||
3058 | tcf_exts_for_each_action(i, act, exts) { | |
3059 | if (is_tcf_pedit(act)) | |
3060 | num_acts += tcf_pedit_nkeys(act); | |
3061 | else | |
3062 | num_acts++; | |
3063 | } | |
3064 | return num_acts; | |
3065 | } | |
3066 | EXPORT_SYMBOL(tcf_exts_num_actions); | |
3067 | ||
48617387 JP |
3068 | static __net_init int tcf_net_init(struct net *net) |
3069 | { | |
3070 | struct tcf_net *tn = net_generic(net, tcf_net_id); | |
3071 | ||
ab281629 | 3072 | spin_lock_init(&tn->idr_lock); |
48617387 JP |
3073 | idr_init(&tn->idr); |
3074 | return 0; | |
3075 | } | |
3076 | ||
3077 | static void __net_exit tcf_net_exit(struct net *net) | |
3078 | { | |
3079 | struct tcf_net *tn = net_generic(net, tcf_net_id); | |
3080 | ||
3081 | idr_destroy(&tn->idr); | |
3082 | } | |
3083 | ||
3084 | static struct pernet_operations tcf_net_ops = { | |
3085 | .init = tcf_net_init, | |
3086 | .exit = tcf_net_exit, | |
3087 | .id = &tcf_net_id, | |
3088 | .size = sizeof(struct tcf_net), | |
3089 | }; | |
3090 | ||
1da177e4 LT |
3091 | static int __init tc_filter_init(void) |
3092 | { | |
48617387 JP |
3093 | int err; |
3094 | ||
7aa0045d CW |
3095 | tc_filter_wq = alloc_ordered_workqueue("tc_filter_workqueue", 0); |
3096 | if (!tc_filter_wq) | |
3097 | return -ENOMEM; | |
3098 | ||
48617387 JP |
3099 | err = register_pernet_subsys(&tcf_net_ops); |
3100 | if (err) | |
3101 | goto err_register_pernet_subsys; | |
3102 | ||
7f76fa36 JH |
3103 | err = rhashtable_init(&indr_setup_block_ht, |
3104 | &tc_indr_setup_block_ht_params); | |
3105 | if (err) | |
3106 | goto err_rhash_setup_block_ht; | |
3107 | ||
c431f89b VB |
3108 | rtnl_register(PF_UNSPEC, RTM_NEWTFILTER, tc_new_tfilter, NULL, 0); |
3109 | rtnl_register(PF_UNSPEC, RTM_DELTFILTER, tc_del_tfilter, NULL, 0); | |
3110 | rtnl_register(PF_UNSPEC, RTM_GETTFILTER, tc_get_tfilter, | |
b97bac64 | 3111 | tc_dump_tfilter, 0); |
32a4f5ec JP |
3112 | rtnl_register(PF_UNSPEC, RTM_NEWCHAIN, tc_ctl_chain, NULL, 0); |
3113 | rtnl_register(PF_UNSPEC, RTM_DELCHAIN, tc_ctl_chain, NULL, 0); | |
3114 | rtnl_register(PF_UNSPEC, RTM_GETCHAIN, tc_ctl_chain, | |
3115 | tc_dump_chain, 0); | |
1da177e4 | 3116 | |
1da177e4 | 3117 | return 0; |
48617387 | 3118 | |
7f76fa36 JH |
3119 | err_rhash_setup_block_ht: |
3120 | unregister_pernet_subsys(&tcf_net_ops); | |
48617387 JP |
3121 | err_register_pernet_subsys: |
3122 | destroy_workqueue(tc_filter_wq); | |
3123 | return err; | |
1da177e4 LT |
3124 | } |
3125 | ||
3126 | subsys_initcall(tc_filter_init); |