Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * net/sched/act_api.c Packet action 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 | * Author: Jamal Hadi Salim | |
10 | * | |
11 | * | |
12 | */ | |
13 | ||
1da177e4 LT |
14 | #include <linux/types.h> |
15 | #include <linux/kernel.h> | |
1da177e4 | 16 | #include <linux/string.h> |
1da177e4 | 17 | #include <linux/errno.h> |
5a0e3ad6 | 18 | #include <linux/slab.h> |
1da177e4 | 19 | #include <linux/skbuff.h> |
1da177e4 LT |
20 | #include <linux/init.h> |
21 | #include <linux/kmod.h> | |
ab27cfb8 | 22 | #include <linux/err.h> |
3a9a231d | 23 | #include <linux/module.h> |
b3f55bdd JP |
24 | #include <linux/rhashtable.h> |
25 | #include <linux/list.h> | |
b854272b DL |
26 | #include <net/net_namespace.h> |
27 | #include <net/sock.h> | |
1da177e4 | 28 | #include <net/sch_generic.h> |
1045ba77 | 29 | #include <net/pkt_cls.h> |
1da177e4 | 30 | #include <net/act_api.h> |
dc5fc579 | 31 | #include <net/netlink.h> |
1da177e4 | 32 | |
db50514f JP |
33 | static int tcf_action_goto_chain_init(struct tc_action *a, struct tcf_proto *tp) |
34 | { | |
35 | u32 chain_index = a->tcfa_action & TC_ACT_EXT_VAL_MASK; | |
36 | ||
37 | if (!tp) | |
38 | return -EINVAL; | |
1f3ed383 | 39 | a->goto_chain = tcf_chain_get_by_act(tp->chain->block, chain_index); |
db50514f JP |
40 | if (!a->goto_chain) |
41 | return -ENOMEM; | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static void tcf_action_goto_chain_fini(struct tc_action *a) | |
46 | { | |
1f3ed383 | 47 | tcf_chain_put_by_act(a->goto_chain); |
db50514f JP |
48 | } |
49 | ||
50 | static void tcf_action_goto_chain_exec(const struct tc_action *a, | |
51 | struct tcf_result *res) | |
52 | { | |
53 | const struct tcf_chain *chain = a->goto_chain; | |
54 | ||
55 | res->goto_tp = rcu_dereference_bh(chain->filter_chain); | |
56 | } | |
57 | ||
eec94fdb VB |
58 | static void tcf_free_cookie_rcu(struct rcu_head *p) |
59 | { | |
60 | struct tc_cookie *cookie = container_of(p, struct tc_cookie, rcu); | |
61 | ||
62 | kfree(cookie->data); | |
63 | kfree(cookie); | |
64 | } | |
65 | ||
66 | static void tcf_set_action_cookie(struct tc_cookie __rcu **old_cookie, | |
67 | struct tc_cookie *new_cookie) | |
68 | { | |
69 | struct tc_cookie *old; | |
70 | ||
0dbc81ea | 71 | old = xchg((__force struct tc_cookie **)old_cookie, new_cookie); |
eec94fdb VB |
72 | if (old) |
73 | call_rcu(&old->rcu, tcf_free_cookie_rcu); | |
74 | } | |
75 | ||
d7fb60b9 CW |
76 | /* XXX: For standalone actions, we don't need a RCU grace period either, because |
77 | * actions are always connected to filters and filters are already destroyed in | |
78 | * RCU callbacks, so after a RCU grace period actions are already disconnected | |
79 | * from filters. Readers later can not find us. | |
80 | */ | |
81 | static void free_tcf(struct tc_action *p) | |
519c818e | 82 | { |
519c818e ED |
83 | free_percpu(p->cpu_bstats); |
84 | free_percpu(p->cpu_qstats); | |
1045ba77 | 85 | |
eec94fdb | 86 | tcf_set_action_cookie(&p->act_cookie, NULL); |
db50514f JP |
87 | if (p->goto_chain) |
88 | tcf_action_goto_chain_fini(p); | |
1045ba77 | 89 | |
519c818e ED |
90 | kfree(p); |
91 | } | |
92 | ||
16af6067 | 93 | static void tcf_action_cleanup(struct tc_action *p) |
e9ce1cd3 | 94 | { |
16af6067 VB |
95 | if (p->ops->cleanup) |
96 | p->ops->cleanup(p); | |
97 | ||
1c0d32fd | 98 | gen_kill_estimator(&p->tcfa_rate_est); |
d7fb60b9 | 99 | free_tcf(p); |
e9ce1cd3 | 100 | } |
e9ce1cd3 | 101 | |
16af6067 VB |
102 | static int __tcf_action_put(struct tc_action *p, bool bind) |
103 | { | |
104 | struct tcf_idrinfo *idrinfo = p->idrinfo; | |
105 | ||
106 | if (refcount_dec_and_lock(&p->tcfa_refcnt, &idrinfo->lock)) { | |
107 | if (bind) | |
108 | atomic_dec(&p->tcfa_bindcnt); | |
109 | idr_remove(&idrinfo->action_idr, p->tcfa_index); | |
110 | spin_unlock(&idrinfo->lock); | |
111 | ||
112 | tcf_action_cleanup(p); | |
113 | return 1; | |
114 | } | |
115 | ||
116 | if (bind) | |
117 | atomic_dec(&p->tcfa_bindcnt); | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
65a206c0 | 122 | int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) |
e9ce1cd3 DM |
123 | { |
124 | int ret = 0; | |
125 | ||
036bb443 VB |
126 | /* Release with strict==1 and bind==0 is only called through act API |
127 | * interface (classifiers always bind). Only case when action with | |
128 | * positive reference count and zero bind count can exist is when it was | |
129 | * also created with act API (unbinding last classifier will destroy the | |
130 | * action if it was created by classifier). So only case when bind count | |
131 | * can be changed after initial check is when unbound action is | |
132 | * destroyed by act API while classifier binds to action with same id | |
133 | * concurrently. This result either creation of new action(same behavior | |
134 | * as before), or reusing existing action if concurrent process | |
135 | * increments reference count before action is deleted. Both scenarios | |
136 | * are acceptable. | |
137 | */ | |
e9ce1cd3 | 138 | if (p) { |
16af6067 | 139 | if (!bind && strict && atomic_read(&p->tcfa_bindcnt) > 0) |
55334a5d | 140 | return -EPERM; |
e9ce1cd3 | 141 | |
16af6067 | 142 | if (__tcf_action_put(p, bind)) |
1d4150c0 | 143 | ret = ACT_P_DELETED; |
e9ce1cd3 | 144 | } |
28e6b67f | 145 | |
e9ce1cd3 DM |
146 | return ret; |
147 | } | |
65a206c0 | 148 | EXPORT_SYMBOL(__tcf_idr_release); |
e9ce1cd3 | 149 | |
4e76e75d RM |
150 | static size_t tcf_action_shared_attrs_size(const struct tc_action *act) |
151 | { | |
e0479b67 | 152 | struct tc_cookie *act_cookie; |
4e76e75d RM |
153 | u32 cookie_len = 0; |
154 | ||
e0479b67 VB |
155 | rcu_read_lock(); |
156 | act_cookie = rcu_dereference(act->act_cookie); | |
157 | ||
158 | if (act_cookie) | |
159 | cookie_len = nla_total_size(act_cookie->len); | |
160 | rcu_read_unlock(); | |
4e76e75d RM |
161 | |
162 | return nla_total_size(0) /* action number nested */ | |
163 | + nla_total_size(IFNAMSIZ) /* TCA_ACT_KIND */ | |
164 | + cookie_len /* TCA_ACT_COOKIE */ | |
165 | + nla_total_size(0) /* TCA_ACT_STATS nested */ | |
166 | /* TCA_STATS_BASIC */ | |
167 | + nla_total_size_64bit(sizeof(struct gnet_stats_basic)) | |
168 | /* TCA_STATS_QUEUE */ | |
169 | + nla_total_size_64bit(sizeof(struct gnet_stats_queue)) | |
170 | + nla_total_size(0) /* TCA_OPTIONS nested */ | |
171 | + nla_total_size(sizeof(struct tcf_t)); /* TCA_GACT_TM */ | |
172 | } | |
173 | ||
174 | static size_t tcf_action_full_attrs_size(size_t sz) | |
175 | { | |
176 | return NLMSG_HDRLEN /* struct nlmsghdr */ | |
177 | + sizeof(struct tcamsg) | |
178 | + nla_total_size(0) /* TCA_ACT_TAB nested */ | |
179 | + sz; | |
180 | } | |
181 | ||
182 | static size_t tcf_action_fill_size(const struct tc_action *act) | |
183 | { | |
184 | size_t sz = tcf_action_shared_attrs_size(act); | |
185 | ||
186 | if (act->ops->get_fill_size) | |
187 | return act->ops->get_fill_size(act) + sz; | |
188 | return sz; | |
189 | } | |
190 | ||
65a206c0 | 191 | static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
a85a970a | 192 | struct netlink_callback *cb) |
e9ce1cd3 | 193 | { |
65a206c0 | 194 | int err = 0, index = -1, s_i = 0, n_i = 0; |
90825b23 | 195 | u32 act_flags = cb->args[2]; |
e62e484d | 196 | unsigned long jiffy_since = cb->args[3]; |
4b3550ef | 197 | struct nlattr *nest; |
65a206c0 CM |
198 | struct idr *idr = &idrinfo->action_idr; |
199 | struct tc_action *p; | |
200 | unsigned long id = 1; | |
e9ce1cd3 | 201 | |
290aa0ad | 202 | spin_lock(&idrinfo->lock); |
e9ce1cd3 DM |
203 | |
204 | s_i = cb->args[0]; | |
205 | ||
7a457577 | 206 | idr_for_each_entry_ul(idr, p, id) { |
65a206c0 CM |
207 | index++; |
208 | if (index < s_i) | |
209 | continue; | |
210 | ||
211 | if (jiffy_since && | |
212 | time_after(jiffy_since, | |
213 | (unsigned long)p->tcfa_tm.lastuse)) | |
214 | continue; | |
215 | ||
216 | nest = nla_nest_start(skb, n_i); | |
734549eb CD |
217 | if (!nest) { |
218 | index--; | |
65a206c0 | 219 | goto nla_put_failure; |
734549eb | 220 | } |
65a206c0 CM |
221 | err = tcf_action_dump_1(skb, p, 0, 0); |
222 | if (err < 0) { | |
223 | index--; | |
224 | nlmsg_trim(skb, nest); | |
225 | goto done; | |
e9ce1cd3 | 226 | } |
65a206c0 CM |
227 | nla_nest_end(skb, nest); |
228 | n_i++; | |
229 | if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && | |
230 | n_i >= TCA_ACT_MAX_PRIO) | |
231 | goto done; | |
e9ce1cd3 DM |
232 | } |
233 | done: | |
e62e484d JHS |
234 | if (index >= 0) |
235 | cb->args[0] = index + 1; | |
236 | ||
290aa0ad | 237 | spin_unlock(&idrinfo->lock); |
90825b23 | 238 | if (n_i) { |
90825b23 JHS |
239 | if (act_flags & TCA_FLAG_LARGE_DUMP_ON) |
240 | cb->args[1] = n_i; | |
241 | } | |
e9ce1cd3 DM |
242 | return n_i; |
243 | ||
7ba699c6 | 244 | nla_put_failure: |
4b3550ef | 245 | nla_nest_cancel(skb, nest); |
e9ce1cd3 DM |
246 | goto done; |
247 | } | |
248 | ||
65a206c0 | 249 | static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
a85a970a | 250 | const struct tc_action_ops *ops) |
e9ce1cd3 | 251 | { |
4b3550ef | 252 | struct nlattr *nest; |
65a206c0 | 253 | int n_i = 0; |
55334a5d | 254 | int ret = -EINVAL; |
65a206c0 CM |
255 | struct idr *idr = &idrinfo->action_idr; |
256 | struct tc_action *p; | |
257 | unsigned long id = 1; | |
e9ce1cd3 | 258 | |
a85a970a | 259 | nest = nla_nest_start(skb, 0); |
4b3550ef PM |
260 | if (nest == NULL) |
261 | goto nla_put_failure; | |
a85a970a | 262 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
1b34ec43 | 263 | goto nla_put_failure; |
65a206c0 | 264 | |
7a457577 | 265 | idr_for_each_entry_ul(idr, p, id) { |
65a206c0 CM |
266 | ret = __tcf_idr_release(p, false, true); |
267 | if (ret == ACT_P_DELETED) { | |
255cd50f | 268 | module_put(ops->owner); |
65a206c0 CM |
269 | n_i++; |
270 | } else if (ret < 0) { | |
271 | goto nla_put_failure; | |
e9ce1cd3 DM |
272 | } |
273 | } | |
1b34ec43 DM |
274 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
275 | goto nla_put_failure; | |
4b3550ef | 276 | nla_nest_end(skb, nest); |
e9ce1cd3 DM |
277 | |
278 | return n_i; | |
7ba699c6 | 279 | nla_put_failure: |
4b3550ef | 280 | nla_nest_cancel(skb, nest); |
55334a5d | 281 | return ret; |
e9ce1cd3 DM |
282 | } |
283 | ||
ddf97ccd WC |
284 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
285 | struct netlink_callback *cb, int type, | |
b3620145 AA |
286 | const struct tc_action_ops *ops, |
287 | struct netlink_ext_ack *extack) | |
e9ce1cd3 | 288 | { |
65a206c0 | 289 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
ddf97ccd | 290 | |
e9ce1cd3 | 291 | if (type == RTM_DELACTION) { |
65a206c0 | 292 | return tcf_del_walker(idrinfo, skb, ops); |
e9ce1cd3 | 293 | } else if (type == RTM_GETACTION) { |
65a206c0 | 294 | return tcf_dump_walker(idrinfo, skb, cb); |
e9ce1cd3 | 295 | } else { |
b3620145 AA |
296 | WARN(1, "tcf_generic_walker: unknown command %d\n", type); |
297 | NL_SET_ERR_MSG(extack, "tcf_generic_walker: unknown command"); | |
e9ce1cd3 DM |
298 | return -EINVAL; |
299 | } | |
300 | } | |
ddf97ccd | 301 | EXPORT_SYMBOL(tcf_generic_walker); |
e9ce1cd3 | 302 | |
3f7c72bc VB |
303 | static bool __tcf_idr_check(struct tc_action_net *tn, u32 index, |
304 | struct tc_action **a, int bind) | |
e9ce1cd3 | 305 | { |
3f7c72bc VB |
306 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
307 | struct tc_action *p; | |
e9ce1cd3 | 308 | |
290aa0ad | 309 | spin_lock(&idrinfo->lock); |
322d884b | 310 | p = idr_find(&idrinfo->action_idr, index); |
0190c1d4 VB |
311 | if (IS_ERR(p)) { |
312 | p = NULL; | |
313 | } else if (p) { | |
3f7c72bc VB |
314 | refcount_inc(&p->tcfa_refcnt); |
315 | if (bind) | |
316 | atomic_inc(&p->tcfa_bindcnt); | |
317 | } | |
290aa0ad | 318 | spin_unlock(&idrinfo->lock); |
e9ce1cd3 | 319 | |
3f7c72bc VB |
320 | if (p) { |
321 | *a = p; | |
322 | return true; | |
323 | } | |
324 | return false; | |
e9ce1cd3 | 325 | } |
e9ce1cd3 | 326 | |
65a206c0 | 327 | int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) |
e9ce1cd3 | 328 | { |
3f7c72bc | 329 | return __tcf_idr_check(tn, index, a, 0); |
e9ce1cd3 | 330 | } |
65a206c0 | 331 | EXPORT_SYMBOL(tcf_idr_search); |
e9ce1cd3 | 332 | |
65a206c0 CM |
333 | bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a, |
334 | int bind) | |
e9ce1cd3 | 335 | { |
3f7c72bc | 336 | return __tcf_idr_check(tn, index, a, bind); |
e9ce1cd3 | 337 | } |
65a206c0 | 338 | EXPORT_SYMBOL(tcf_idr_check); |
e9ce1cd3 | 339 | |
97a3f84f | 340 | static int tcf_idr_delete_index(struct tcf_idrinfo *idrinfo, u32 index) |
2a2ea349 | 341 | { |
2a2ea349 VB |
342 | struct tc_action *p; |
343 | int ret = 0; | |
344 | ||
345 | spin_lock(&idrinfo->lock); | |
346 | p = idr_find(&idrinfo->action_idr, index); | |
347 | if (!p) { | |
348 | spin_unlock(&idrinfo->lock); | |
349 | return -ENOENT; | |
350 | } | |
351 | ||
352 | if (!atomic_read(&p->tcfa_bindcnt)) { | |
353 | if (refcount_dec_and_test(&p->tcfa_refcnt)) { | |
354 | struct module *owner = p->ops->owner; | |
355 | ||
356 | WARN_ON(p != idr_remove(&idrinfo->action_idr, | |
357 | p->tcfa_index)); | |
358 | spin_unlock(&idrinfo->lock); | |
359 | ||
16af6067 | 360 | tcf_action_cleanup(p); |
2a2ea349 VB |
361 | module_put(owner); |
362 | return 0; | |
363 | } | |
364 | ret = 0; | |
365 | } else { | |
366 | ret = -EPERM; | |
367 | } | |
368 | ||
369 | spin_unlock(&idrinfo->lock); | |
370 | return ret; | |
371 | } | |
2a2ea349 | 372 | |
65a206c0 CM |
373 | int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
374 | struct tc_action **a, const struct tc_action_ops *ops, | |
375 | int bind, bool cpustats) | |
e9ce1cd3 | 376 | { |
ec0595cc | 377 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
65a206c0 | 378 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
519c818e | 379 | int err = -ENOMEM; |
e9ce1cd3 DM |
380 | |
381 | if (unlikely(!p)) | |
86062033 | 382 | return -ENOMEM; |
036bb443 | 383 | refcount_set(&p->tcfa_refcnt, 1); |
e9ce1cd3 | 384 | if (bind) |
036bb443 | 385 | atomic_set(&p->tcfa_bindcnt, 1); |
e9ce1cd3 | 386 | |
519c818e ED |
387 | if (cpustats) { |
388 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); | |
339913a8 | 389 | if (!p->cpu_bstats) |
519c818e | 390 | goto err1; |
339913a8 MW |
391 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); |
392 | if (!p->cpu_qstats) | |
393 | goto err2; | |
519c818e | 394 | } |
ec0595cc | 395 | spin_lock_init(&p->tcfa_lock); |
339913a8 | 396 | p->tcfa_index = index; |
ec0595cc WC |
397 | p->tcfa_tm.install = jiffies; |
398 | p->tcfa_tm.lastuse = jiffies; | |
399 | p->tcfa_tm.firstuse = 0; | |
0e991ec6 | 400 | if (est) { |
ec0595cc WC |
401 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
402 | &p->tcfa_rate_est, | |
403 | &p->tcfa_lock, NULL, est); | |
339913a8 | 404 | if (err) |
0190c1d4 | 405 | goto err3; |
0e991ec6 SH |
406 | } |
407 | ||
65a206c0 | 408 | p->idrinfo = idrinfo; |
ec0595cc WC |
409 | p->ops = ops; |
410 | INIT_LIST_HEAD(&p->list); | |
411 | *a = p; | |
86062033 | 412 | return 0; |
339913a8 MW |
413 | err3: |
414 | free_percpu(p->cpu_qstats); | |
415 | err2: | |
416 | free_percpu(p->cpu_bstats); | |
417 | err1: | |
418 | kfree(p); | |
419 | return err; | |
e9ce1cd3 | 420 | } |
65a206c0 | 421 | EXPORT_SYMBOL(tcf_idr_create); |
e9ce1cd3 | 422 | |
65a206c0 | 423 | void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) |
e9ce1cd3 | 424 | { |
65a206c0 | 425 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
e9ce1cd3 | 426 | |
290aa0ad | 427 | spin_lock(&idrinfo->lock); |
0190c1d4 VB |
428 | /* Replace ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ |
429 | WARN_ON(!IS_ERR(idr_replace(&idrinfo->action_idr, a, a->tcfa_index))); | |
290aa0ad | 430 | spin_unlock(&idrinfo->lock); |
e9ce1cd3 | 431 | } |
65a206c0 | 432 | EXPORT_SYMBOL(tcf_idr_insert); |
1da177e4 | 433 | |
0190c1d4 VB |
434 | /* Cleanup idr index that was allocated but not initialized. */ |
435 | ||
436 | void tcf_idr_cleanup(struct tc_action_net *tn, u32 index) | |
437 | { | |
438 | struct tcf_idrinfo *idrinfo = tn->idrinfo; | |
439 | ||
440 | spin_lock(&idrinfo->lock); | |
441 | /* Remove ERR_PTR(-EBUSY) allocated by tcf_idr_check_alloc */ | |
442 | WARN_ON(!IS_ERR(idr_remove(&idrinfo->action_idr, index))); | |
443 | spin_unlock(&idrinfo->lock); | |
444 | } | |
445 | EXPORT_SYMBOL(tcf_idr_cleanup); | |
446 | ||
447 | /* Check if action with specified index exists. If actions is found, increments | |
448 | * its reference and bind counters, and return 1. Otherwise insert temporary | |
449 | * error pointer (to prevent concurrent users from inserting actions with same | |
450 | * index) and return 0. | |
451 | */ | |
452 | ||
453 | int tcf_idr_check_alloc(struct tc_action_net *tn, u32 *index, | |
454 | struct tc_action **a, int bind) | |
455 | { | |
456 | struct tcf_idrinfo *idrinfo = tn->idrinfo; | |
457 | struct tc_action *p; | |
458 | int ret; | |
459 | ||
460 | again: | |
461 | spin_lock(&idrinfo->lock); | |
462 | if (*index) { | |
463 | p = idr_find(&idrinfo->action_idr, *index); | |
464 | if (IS_ERR(p)) { | |
465 | /* This means that another process allocated | |
466 | * index but did not assign the pointer yet. | |
467 | */ | |
468 | spin_unlock(&idrinfo->lock); | |
469 | goto again; | |
470 | } | |
471 | ||
472 | if (p) { | |
473 | refcount_inc(&p->tcfa_refcnt); | |
474 | if (bind) | |
475 | atomic_inc(&p->tcfa_bindcnt); | |
476 | *a = p; | |
477 | ret = 1; | |
478 | } else { | |
479 | *a = NULL; | |
480 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, | |
481 | *index, GFP_ATOMIC); | |
482 | if (!ret) | |
483 | idr_replace(&idrinfo->action_idr, | |
484 | ERR_PTR(-EBUSY), *index); | |
485 | } | |
486 | } else { | |
487 | *index = 1; | |
488 | *a = NULL; | |
489 | ret = idr_alloc_u32(&idrinfo->action_idr, NULL, index, | |
490 | UINT_MAX, GFP_ATOMIC); | |
491 | if (!ret) | |
492 | idr_replace(&idrinfo->action_idr, ERR_PTR(-EBUSY), | |
493 | *index); | |
494 | } | |
495 | spin_unlock(&idrinfo->lock); | |
496 | return ret; | |
497 | } | |
498 | EXPORT_SYMBOL(tcf_idr_check_alloc); | |
499 | ||
65a206c0 CM |
500 | void tcf_idrinfo_destroy(const struct tc_action_ops *ops, |
501 | struct tcf_idrinfo *idrinfo) | |
1d4150c0 | 502 | { |
65a206c0 CM |
503 | struct idr *idr = &idrinfo->action_idr; |
504 | struct tc_action *p; | |
505 | int ret; | |
506 | unsigned long id = 1; | |
1d4150c0 | 507 | |
7a457577 | 508 | idr_for_each_entry_ul(idr, p, id) { |
65a206c0 CM |
509 | ret = __tcf_idr_release(p, false, true); |
510 | if (ret == ACT_P_DELETED) | |
511 | module_put(ops->owner); | |
512 | else if (ret < 0) | |
513 | return; | |
1d4150c0 | 514 | } |
65a206c0 | 515 | idr_destroy(&idrinfo->action_idr); |
1d4150c0 | 516 | } |
65a206c0 | 517 | EXPORT_SYMBOL(tcf_idrinfo_destroy); |
1d4150c0 | 518 | |
1f747c26 | 519 | static LIST_HEAD(act_base); |
1da177e4 LT |
520 | static DEFINE_RWLOCK(act_mod_lock); |
521 | ||
ddf97ccd WC |
522 | int tcf_register_action(struct tc_action_ops *act, |
523 | struct pernet_operations *ops) | |
1da177e4 | 524 | { |
1f747c26 | 525 | struct tc_action_ops *a; |
ddf97ccd | 526 | int ret; |
1da177e4 | 527 | |
ddf97ccd | 528 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
76c82d7a JHS |
529 | return -EINVAL; |
530 | ||
ab102b80 WC |
531 | /* We have to register pernet ops before making the action ops visible, |
532 | * otherwise tcf_action_init_1() could get a partially initialized | |
533 | * netns. | |
534 | */ | |
535 | ret = register_pernet_subsys(ops); | |
536 | if (ret) | |
537 | return ret; | |
538 | ||
1da177e4 | 539 | write_lock(&act_mod_lock); |
1f747c26 | 540 | list_for_each_entry(a, &act_base, head) { |
1da177e4 LT |
541 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
542 | write_unlock(&act_mod_lock); | |
ab102b80 | 543 | unregister_pernet_subsys(ops); |
1da177e4 LT |
544 | return -EEXIST; |
545 | } | |
546 | } | |
1f747c26 | 547 | list_add_tail(&act->head, &act_base); |
1da177e4 | 548 | write_unlock(&act_mod_lock); |
ddf97ccd | 549 | |
1da177e4 LT |
550 | return 0; |
551 | } | |
62e3ba1b | 552 | EXPORT_SYMBOL(tcf_register_action); |
1da177e4 | 553 | |
ddf97ccd WC |
554 | int tcf_unregister_action(struct tc_action_ops *act, |
555 | struct pernet_operations *ops) | |
1da177e4 | 556 | { |
1f747c26 | 557 | struct tc_action_ops *a; |
1da177e4 LT |
558 | int err = -ENOENT; |
559 | ||
560 | write_lock(&act_mod_lock); | |
a792866a ED |
561 | list_for_each_entry(a, &act_base, head) { |
562 | if (a == act) { | |
563 | list_del(&act->head); | |
564 | err = 0; | |
1da177e4 | 565 | break; |
a792866a | 566 | } |
1da177e4 LT |
567 | } |
568 | write_unlock(&act_mod_lock); | |
ab102b80 WC |
569 | if (!err) |
570 | unregister_pernet_subsys(ops); | |
1da177e4 LT |
571 | return err; |
572 | } | |
62e3ba1b | 573 | EXPORT_SYMBOL(tcf_unregister_action); |
1da177e4 LT |
574 | |
575 | /* lookup by name */ | |
576 | static struct tc_action_ops *tc_lookup_action_n(char *kind) | |
577 | { | |
a792866a | 578 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
579 | |
580 | if (kind) { | |
581 | read_lock(&act_mod_lock); | |
1f747c26 | 582 | list_for_each_entry(a, &act_base, head) { |
1da177e4 | 583 | if (strcmp(kind, a->kind) == 0) { |
a792866a ED |
584 | if (try_module_get(a->owner)) |
585 | res = a; | |
1da177e4 LT |
586 | break; |
587 | } | |
588 | } | |
589 | read_unlock(&act_mod_lock); | |
590 | } | |
a792866a | 591 | return res; |
1da177e4 LT |
592 | } |
593 | ||
7ba699c6 PM |
594 | /* lookup by nlattr */ |
595 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) | |
1da177e4 | 596 | { |
a792866a | 597 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
598 | |
599 | if (kind) { | |
600 | read_lock(&act_mod_lock); | |
1f747c26 | 601 | list_for_each_entry(a, &act_base, head) { |
7ba699c6 | 602 | if (nla_strcmp(kind, a->kind) == 0) { |
a792866a ED |
603 | if (try_module_get(a->owner)) |
604 | res = a; | |
1da177e4 LT |
605 | break; |
606 | } | |
607 | } | |
608 | read_unlock(&act_mod_lock); | |
609 | } | |
a792866a | 610 | return res; |
1da177e4 | 611 | } |
1da177e4 | 612 | |
e0ee84de JHS |
613 | /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ |
614 | #define TCA_ACT_MAX_PRIO_MASK 0x1FF | |
22dc13c8 WC |
615 | int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, |
616 | int nr_actions, struct tcf_result *res) | |
1da177e4 | 617 | { |
e0ee84de JHS |
618 | u32 jmp_prgcnt = 0; |
619 | u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ | |
ec1a9cca JP |
620 | int i; |
621 | int ret = TC_ACT_OK; | |
1da177e4 | 622 | |
e7246e12 WB |
623 | if (skb_skip_tc_classify(skb)) |
624 | return TC_ACT_OK; | |
625 | ||
e0ee84de | 626 | restart_act_graph: |
22dc13c8 WC |
627 | for (i = 0; i < nr_actions; i++) { |
628 | const struct tc_action *a = actions[i]; | |
629 | ||
e0ee84de JHS |
630 | if (jmp_prgcnt > 0) { |
631 | jmp_prgcnt -= 1; | |
632 | continue; | |
633 | } | |
1da177e4 | 634 | repeat: |
63acd680 | 635 | ret = a->ops->act(skb, a, res); |
63acd680 JHS |
636 | if (ret == TC_ACT_REPEAT) |
637 | goto repeat; /* we need a ttl - JHS */ | |
e0ee84de | 638 | |
9da3242e | 639 | if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { |
e0ee84de JHS |
640 | jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; |
641 | if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { | |
642 | /* faulty opcode, stop pipeline */ | |
643 | return TC_ACT_OK; | |
644 | } else { | |
645 | jmp_ttl -= 1; | |
646 | if (jmp_ttl > 0) | |
647 | goto restart_act_graph; | |
648 | else /* faulty graph, stop pipeline */ | |
649 | return TC_ACT_OK; | |
650 | } | |
db50514f JP |
651 | } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { |
652 | tcf_action_goto_chain_exec(a, res); | |
e0ee84de JHS |
653 | } |
654 | ||
63acd680 | 655 | if (ret != TC_ACT_PIPE) |
e7246e12 | 656 | break; |
1da177e4 | 657 | } |
e0ee84de | 658 | |
1da177e4 LT |
659 | return ret; |
660 | } | |
62e3ba1b | 661 | EXPORT_SYMBOL(tcf_action_exec); |
1da177e4 | 662 | |
90b73b77 | 663 | int tcf_action_destroy(struct tc_action *actions[], int bind) |
1da177e4 | 664 | { |
255cd50f | 665 | const struct tc_action_ops *ops; |
90b73b77 VB |
666 | struct tc_action *a; |
667 | int ret = 0, i; | |
1da177e4 | 668 | |
90b73b77 VB |
669 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
670 | a = actions[i]; | |
671 | actions[i] = NULL; | |
255cd50f | 672 | ops = a->ops; |
65a206c0 | 673 | ret = __tcf_idr_release(a, bind, true); |
55334a5d | 674 | if (ret == ACT_P_DELETED) |
255cd50f | 675 | module_put(ops->owner); |
55334a5d WC |
676 | else if (ret < 0) |
677 | return ret; | |
1da177e4 | 678 | } |
55334a5d | 679 | return ret; |
1da177e4 LT |
680 | } |
681 | ||
16af6067 VB |
682 | static int tcf_action_put(struct tc_action *p) |
683 | { | |
684 | return __tcf_action_put(p, false); | |
685 | } | |
686 | ||
edfaf94f | 687 | /* Put all actions in this array, skip those NULL's. */ |
90b73b77 | 688 | static void tcf_action_put_many(struct tc_action *actions[]) |
cae422f3 | 689 | { |
90b73b77 | 690 | int i; |
cae422f3 | 691 | |
edfaf94f | 692 | for (i = 0; i < TCA_ACT_MAX_PRIO; i++) { |
90b73b77 | 693 | struct tc_action *a = actions[i]; |
edfaf94f | 694 | const struct tc_action_ops *ops; |
cae422f3 | 695 | |
edfaf94f CW |
696 | if (!a) |
697 | continue; | |
698 | ops = a->ops; | |
cae422f3 VB |
699 | if (tcf_action_put(a)) |
700 | module_put(ops->owner); | |
701 | } | |
702 | } | |
703 | ||
1da177e4 LT |
704 | int |
705 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
706 | { | |
1da177e4 LT |
707 | return a->ops->dump(skb, a, bind, ref); |
708 | } | |
709 | ||
710 | int | |
711 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
712 | { | |
713 | int err = -EINVAL; | |
27a884dc | 714 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 715 | struct nlattr *nest; |
eec94fdb | 716 | struct tc_cookie *cookie; |
1da177e4 | 717 | |
1b34ec43 DM |
718 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
719 | goto nla_put_failure; | |
1da177e4 | 720 | if (tcf_action_copy_stats(skb, a, 0)) |
7ba699c6 | 721 | goto nla_put_failure; |
eec94fdb VB |
722 | |
723 | rcu_read_lock(); | |
724 | cookie = rcu_dereference(a->act_cookie); | |
725 | if (cookie) { | |
726 | if (nla_put(skb, TCA_ACT_COOKIE, cookie->len, cookie->data)) { | |
727 | rcu_read_unlock(); | |
1045ba77 | 728 | goto nla_put_failure; |
eec94fdb | 729 | } |
1045ba77 | 730 | } |
eec94fdb | 731 | rcu_read_unlock(); |
1045ba77 | 732 | |
4b3550ef PM |
733 | nest = nla_nest_start(skb, TCA_OPTIONS); |
734 | if (nest == NULL) | |
735 | goto nla_put_failure; | |
cc7ec456 ED |
736 | err = tcf_action_dump_old(skb, a, bind, ref); |
737 | if (err > 0) { | |
4b3550ef | 738 | nla_nest_end(skb, nest); |
1da177e4 LT |
739 | return err; |
740 | } | |
741 | ||
7ba699c6 | 742 | nla_put_failure: |
dc5fc579 | 743 | nlmsg_trim(skb, b); |
1da177e4 LT |
744 | return -1; |
745 | } | |
62e3ba1b | 746 | EXPORT_SYMBOL(tcf_action_dump_1); |
1da177e4 | 747 | |
90b73b77 | 748 | int tcf_action_dump(struct sk_buff *skb, struct tc_action *actions[], |
0b0f43fe | 749 | int bind, int ref) |
1da177e4 LT |
750 | { |
751 | struct tc_action *a; | |
90b73b77 | 752 | int err = -EINVAL, i; |
4b3550ef | 753 | struct nlattr *nest; |
1da177e4 | 754 | |
90b73b77 VB |
755 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
756 | a = actions[i]; | |
4b3550ef PM |
757 | nest = nla_nest_start(skb, a->order); |
758 | if (nest == NULL) | |
759 | goto nla_put_failure; | |
1da177e4 LT |
760 | err = tcf_action_dump_1(skb, a, bind, ref); |
761 | if (err < 0) | |
4fe683f5 | 762 | goto errout; |
4b3550ef | 763 | nla_nest_end(skb, nest); |
1da177e4 LT |
764 | } |
765 | ||
766 | return 0; | |
767 | ||
7ba699c6 | 768 | nla_put_failure: |
4fe683f5 TG |
769 | err = -EINVAL; |
770 | errout: | |
4b3550ef | 771 | nla_nest_cancel(skb, nest); |
4fe683f5 | 772 | return err; |
1da177e4 LT |
773 | } |
774 | ||
e0535ce5 | 775 | static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) |
1045ba77 | 776 | { |
e0535ce5 WB |
777 | struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); |
778 | if (!c) | |
779 | return NULL; | |
780 | ||
781 | c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); | |
782 | if (!c->data) { | |
783 | kfree(c); | |
784 | return NULL; | |
1045ba77 | 785 | } |
e0535ce5 | 786 | c->len = nla_len(tb[TCA_ACT_COOKIE]); |
1045ba77 | 787 | |
e0535ce5 | 788 | return c; |
1045ba77 JHS |
789 | } |
790 | ||
802bfb19 PA |
791 | static bool tcf_action_valid(int action) |
792 | { | |
793 | int opcode = TC_ACT_EXT_OPCODE(action); | |
794 | ||
795 | if (!opcode) | |
796 | return action <= TC_ACT_VALUE_MAX; | |
797 | return opcode <= TC_ACT_EXT_OPCODE_MAX || action == TC_ACT_UNSPEC; | |
798 | } | |
799 | ||
9fb9f251 JP |
800 | struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, |
801 | struct nlattr *nla, struct nlattr *est, | |
aea0d727 | 802 | char *name, int ovr, int bind, |
789871bb | 803 | bool rtnl_held, |
aea0d727 | 804 | struct netlink_ext_ack *extack) |
1da177e4 LT |
805 | { |
806 | struct tc_action *a; | |
807 | struct tc_action_ops *a_o; | |
e0535ce5 | 808 | struct tc_cookie *cookie = NULL; |
1da177e4 | 809 | char act_name[IFNAMSIZ]; |
cc7ec456 | 810 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
7ba699c6 | 811 | struct nlattr *kind; |
ab27cfb8 | 812 | int err; |
1da177e4 | 813 | |
1da177e4 | 814 | if (name == NULL) { |
84ae017a | 815 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
cee63723 | 816 | if (err < 0) |
1da177e4 | 817 | goto err_out; |
cee63723 | 818 | err = -EINVAL; |
7ba699c6 | 819 | kind = tb[TCA_ACT_KIND]; |
84ae017a AA |
820 | if (!kind) { |
821 | NL_SET_ERR_MSG(extack, "TC action kind must be specified"); | |
1da177e4 | 822 | goto err_out; |
84ae017a AA |
823 | } |
824 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) { | |
825 | NL_SET_ERR_MSG(extack, "TC action name too long"); | |
1da177e4 | 826 | goto err_out; |
84ae017a | 827 | } |
e0535ce5 WB |
828 | if (tb[TCA_ACT_COOKIE]) { |
829 | int cklen = nla_len(tb[TCA_ACT_COOKIE]); | |
830 | ||
84ae017a AA |
831 | if (cklen > TC_COOKIE_MAX_SIZE) { |
832 | NL_SET_ERR_MSG(extack, "TC cookie size above the maximum"); | |
e0535ce5 | 833 | goto err_out; |
84ae017a | 834 | } |
e0535ce5 WB |
835 | |
836 | cookie = nla_memdup_cookie(tb); | |
837 | if (!cookie) { | |
84ae017a | 838 | NL_SET_ERR_MSG(extack, "No memory to generate TC cookie"); |
e0535ce5 WB |
839 | err = -ENOMEM; |
840 | goto err_out; | |
841 | } | |
842 | } | |
1da177e4 | 843 | } else { |
84ae017a AA |
844 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) { |
845 | NL_SET_ERR_MSG(extack, "TC action name too long"); | |
846 | err = -EINVAL; | |
1da177e4 | 847 | goto err_out; |
84ae017a | 848 | } |
1da177e4 LT |
849 | } |
850 | ||
851 | a_o = tc_lookup_action_n(act_name); | |
852 | if (a_o == NULL) { | |
95a5afca | 853 | #ifdef CONFIG_MODULES |
789871bb VB |
854 | if (rtnl_held) |
855 | rtnl_unlock(); | |
4bba3925 | 856 | request_module("act_%s", act_name); |
789871bb VB |
857 | if (rtnl_held) |
858 | rtnl_lock(); | |
1da177e4 LT |
859 | |
860 | a_o = tc_lookup_action_n(act_name); | |
861 | ||
862 | /* We dropped the RTNL semaphore in order to | |
863 | * perform the module load. So, even if we | |
864 | * succeeded in loading the module we have to | |
865 | * tell the caller to replay the request. We | |
866 | * indicate this using -EAGAIN. | |
867 | */ | |
868 | if (a_o != NULL) { | |
ab27cfb8 | 869 | err = -EAGAIN; |
1da177e4 LT |
870 | goto err_mod; |
871 | } | |
872 | #endif | |
84ae017a | 873 | NL_SET_ERR_MSG(extack, "Failed to load TC action module"); |
ab27cfb8 | 874 | err = -ENOENT; |
1da177e4 LT |
875 | goto err_out; |
876 | } | |
877 | ||
1da177e4 LT |
878 | /* backward compatibility for policer */ |
879 | if (name == NULL) | |
589dad6d | 880 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind, |
789871bb | 881 | rtnl_held, extack); |
1da177e4 | 882 | else |
789871bb VB |
883 | err = a_o->init(net, nla, est, &a, ovr, bind, rtnl_held, |
884 | extack); | |
ab27cfb8 | 885 | if (err < 0) |
a85a970a | 886 | goto err_mod; |
1da177e4 | 887 | |
eec94fdb VB |
888 | if (!name && tb[TCA_ACT_COOKIE]) |
889 | tcf_set_action_cookie(&a->act_cookie, cookie); | |
1045ba77 | 890 | |
1da177e4 | 891 | /* module count goes up only when brand new policy is created |
cc7ec456 ED |
892 | * if it exists and is only bound to in a_o->init() then |
893 | * ACT_P_CREATED is not returned (a zero is). | |
894 | */ | |
ab27cfb8 | 895 | if (err != ACT_P_CREATED) |
1da177e4 | 896 | module_put(a_o->owner); |
1da177e4 | 897 | |
db50514f JP |
898 | if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { |
899 | err = tcf_action_goto_chain_init(a, tp); | |
900 | if (err) { | |
90b73b77 | 901 | struct tc_action *actions[] = { a, NULL }; |
db50514f | 902 | |
90b73b77 | 903 | tcf_action_destroy(actions, bind); |
84ae017a | 904 | NL_SET_ERR_MSG(extack, "Failed to init TC action chain"); |
db50514f JP |
905 | return ERR_PTR(err); |
906 | } | |
907 | } | |
908 | ||
802bfb19 PA |
909 | if (!tcf_action_valid(a->tcfa_action)) { |
910 | NL_SET_ERR_MSG(extack, "invalid action value, using TC_ACT_UNSPEC instead"); | |
911 | a->tcfa_action = TC_ACT_UNSPEC; | |
912 | } | |
913 | ||
1da177e4 LT |
914 | return a; |
915 | ||
1da177e4 LT |
916 | err_mod: |
917 | module_put(a_o->owner); | |
918 | err_out: | |
e0535ce5 WB |
919 | if (cookie) { |
920 | kfree(cookie->data); | |
921 | kfree(cookie); | |
922 | } | |
ab27cfb8 | 923 | return ERR_PTR(err); |
1da177e4 LT |
924 | } |
925 | ||
90b73b77 VB |
926 | /* Returns numbers of initialized actions or negative error. */ |
927 | ||
9fb9f251 JP |
928 | int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, |
929 | struct nlattr *est, char *name, int ovr, int bind, | |
90b73b77 | 930 | struct tc_action *actions[], size_t *attr_size, |
789871bb | 931 | bool rtnl_held, struct netlink_ext_ack *extack) |
1da177e4 | 932 | { |
cc7ec456 | 933 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 | 934 | struct tc_action *act; |
4e76e75d | 935 | size_t sz = 0; |
cee63723 | 936 | int err; |
1da177e4 LT |
937 | int i; |
938 | ||
84ae017a | 939 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); |
cee63723 | 940 | if (err < 0) |
33be6271 | 941 | return err; |
1da177e4 | 942 | |
7ba699c6 | 943 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
aea0d727 | 944 | act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind, |
789871bb | 945 | rtnl_held, extack); |
33be6271 WC |
946 | if (IS_ERR(act)) { |
947 | err = PTR_ERR(act); | |
1da177e4 | 948 | goto err; |
33be6271 | 949 | } |
7ba699c6 | 950 | act->order = i; |
4e76e75d | 951 | sz += tcf_action_fill_size(act); |
90b73b77 VB |
952 | /* Start from index 0 */ |
953 | actions[i - 1] = act; | |
1da177e4 | 954 | } |
aecc5cef | 955 | |
4e76e75d | 956 | *attr_size = tcf_action_full_attrs_size(sz); |
90b73b77 | 957 | return i - 1; |
1da177e4 LT |
958 | |
959 | err: | |
33be6271 WC |
960 | tcf_action_destroy(actions, bind); |
961 | return err; | |
1da177e4 LT |
962 | } |
963 | ||
ec0595cc | 964 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, |
1da177e4 LT |
965 | int compat_mode) |
966 | { | |
967 | int err = 0; | |
968 | struct gnet_dump d; | |
10297b99 | 969 | |
7eb8896d | 970 | if (p == NULL) |
1da177e4 LT |
971 | goto errout; |
972 | ||
973 | /* compat_mode being true specifies a call that is supposed | |
06fe9fb4 | 974 | * to add additional backward compatibility statistic TLVs. |
1da177e4 LT |
975 | */ |
976 | if (compat_mode) { | |
ec0595cc | 977 | if (p->type == TCA_OLD_COMPAT) |
1da177e4 | 978 | err = gnet_stats_start_copy_compat(skb, 0, |
9854518e ND |
979 | TCA_STATS, |
980 | TCA_XSTATS, | |
ec0595cc | 981 | &p->tcfa_lock, &d, |
9854518e | 982 | TCA_PAD); |
1da177e4 LT |
983 | else |
984 | return 0; | |
985 | } else | |
986 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, | |
ec0595cc | 987 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
1da177e4 LT |
988 | |
989 | if (err < 0) | |
990 | goto errout; | |
991 | ||
ec0595cc | 992 | if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || |
1c0d32fd | 993 | gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || |
519c818e | 994 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
ec0595cc WC |
995 | &p->tcfa_qstats, |
996 | p->tcfa_qstats.qlen) < 0) | |
1da177e4 LT |
997 | goto errout; |
998 | ||
999 | if (gnet_stats_finish_copy(&d) < 0) | |
1000 | goto errout; | |
1001 | ||
1002 | return 0; | |
1003 | ||
1004 | errout: | |
1005 | return -1; | |
1006 | } | |
1007 | ||
90b73b77 | 1008 | static int tca_get_fill(struct sk_buff *skb, struct tc_action *actions[], |
0b0f43fe JHS |
1009 | u32 portid, u32 seq, u16 flags, int event, int bind, |
1010 | int ref) | |
1da177e4 LT |
1011 | { |
1012 | struct tcamsg *t; | |
1013 | struct nlmsghdr *nlh; | |
27a884dc | 1014 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 1015 | struct nlattr *nest; |
1da177e4 | 1016 | |
15e47304 | 1017 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
8b00a53c DM |
1018 | if (!nlh) |
1019 | goto out_nlmsg_trim; | |
1020 | t = nlmsg_data(nlh); | |
1da177e4 | 1021 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
1022 | t->tca__pad1 = 0; |
1023 | t->tca__pad2 = 0; | |
10297b99 | 1024 | |
4b3550ef | 1025 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
1af85155 | 1026 | if (!nest) |
8b00a53c | 1027 | goto out_nlmsg_trim; |
1da177e4 | 1028 | |
33be6271 | 1029 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
8b00a53c | 1030 | goto out_nlmsg_trim; |
1da177e4 | 1031 | |
4b3550ef | 1032 | nla_nest_end(skb, nest); |
10297b99 | 1033 | |
27a884dc | 1034 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
1035 | return skb->len; |
1036 | ||
8b00a53c | 1037 | out_nlmsg_trim: |
dc5fc579 | 1038 | nlmsg_trim(skb, b); |
1da177e4 LT |
1039 | return -1; |
1040 | } | |
1041 | ||
1042 | static int | |
c4c4290c | 1043 | tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
90b73b77 | 1044 | struct tc_action *actions[], int event, |
84ae017a | 1045 | struct netlink_ext_ack *extack) |
1da177e4 LT |
1046 | { |
1047 | struct sk_buff *skb; | |
1da177e4 LT |
1048 | |
1049 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1050 | if (!skb) | |
1051 | return -ENOBUFS; | |
0b0f43fe | 1052 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
3f7c72bc | 1053 | 0, 1) <= 0) { |
84ae017a | 1054 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
1da177e4 LT |
1055 | kfree_skb(skb); |
1056 | return -EINVAL; | |
1057 | } | |
2942e900 | 1058 | |
15e47304 | 1059 | return rtnl_unicast(skb, net, portid); |
1da177e4 LT |
1060 | } |
1061 | ||
ddf97ccd | 1062 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
84ae017a AA |
1063 | struct nlmsghdr *n, u32 portid, |
1064 | struct netlink_ext_ack *extack) | |
1da177e4 | 1065 | { |
cc7ec456 | 1066 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 1067 | const struct tc_action_ops *ops; |
1da177e4 LT |
1068 | struct tc_action *a; |
1069 | int index; | |
ab27cfb8 | 1070 | int err; |
1da177e4 | 1071 | |
84ae017a | 1072 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
cee63723 | 1073 | if (err < 0) |
ab27cfb8 | 1074 | goto err_out; |
1da177e4 | 1075 | |
cee63723 | 1076 | err = -EINVAL; |
7ba699c6 | 1077 | if (tb[TCA_ACT_INDEX] == NULL || |
84ae017a AA |
1078 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) { |
1079 | NL_SET_ERR_MSG(extack, "Invalid TC action index value"); | |
ab27cfb8 | 1080 | goto err_out; |
84ae017a | 1081 | } |
1587bac4 | 1082 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
1da177e4 | 1083 | |
ab27cfb8 | 1084 | err = -EINVAL; |
a85a970a | 1085 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
84ae017a AA |
1086 | if (!ops) { /* could happen in batch of actions */ |
1087 | NL_SET_ERR_MSG(extack, "Specified TC action not found"); | |
a85a970a | 1088 | goto err_out; |
84ae017a | 1089 | } |
ab27cfb8 | 1090 | err = -ENOENT; |
331a9295 | 1091 | if (ops->lookup(net, &a, index, extack) == 0) |
1da177e4 LT |
1092 | goto err_mod; |
1093 | ||
a85a970a | 1094 | module_put(ops->owner); |
1da177e4 | 1095 | return a; |
ab27cfb8 | 1096 | |
1da177e4 | 1097 | err_mod: |
a85a970a | 1098 | module_put(ops->owner); |
ab27cfb8 PM |
1099 | err_out: |
1100 | return ERR_PTR(err); | |
1da177e4 LT |
1101 | } |
1102 | ||
7316ae88 | 1103 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
84ae017a AA |
1104 | struct nlmsghdr *n, u32 portid, |
1105 | struct netlink_ext_ack *extack) | |
1da177e4 LT |
1106 | { |
1107 | struct sk_buff *skb; | |
1108 | unsigned char *b; | |
1109 | struct nlmsghdr *nlh; | |
1110 | struct tcamsg *t; | |
1111 | struct netlink_callback dcb; | |
4b3550ef | 1112 | struct nlattr *nest; |
cc7ec456 | 1113 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 1114 | const struct tc_action_ops *ops; |
7ba699c6 | 1115 | struct nlattr *kind; |
36723873 | 1116 | int err = -ENOMEM; |
1da177e4 | 1117 | |
1da177e4 | 1118 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
84ae017a | 1119 | if (!skb) |
36723873 | 1120 | return err; |
1da177e4 | 1121 | |
27a884dc | 1122 | b = skb_tail_pointer(skb); |
1da177e4 | 1123 | |
84ae017a | 1124 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, extack); |
cee63723 | 1125 | if (err < 0) |
1da177e4 LT |
1126 | goto err_out; |
1127 | ||
cee63723 | 1128 | err = -EINVAL; |
7ba699c6 | 1129 | kind = tb[TCA_ACT_KIND]; |
a85a970a | 1130 | ops = tc_lookup_action(kind); |
84ae017a AA |
1131 | if (!ops) { /*some idjot trying to flush unknown action */ |
1132 | NL_SET_ERR_MSG(extack, "Cannot flush unknown TC action"); | |
1da177e4 | 1133 | goto err_out; |
84ae017a | 1134 | } |
1da177e4 | 1135 | |
0b0f43fe JHS |
1136 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
1137 | sizeof(*t), 0); | |
84ae017a AA |
1138 | if (!nlh) { |
1139 | NL_SET_ERR_MSG(extack, "Failed to create TC action flush notification"); | |
8b00a53c | 1140 | goto out_module_put; |
84ae017a | 1141 | } |
8b00a53c | 1142 | t = nlmsg_data(nlh); |
1da177e4 | 1143 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
1144 | t->tca__pad1 = 0; |
1145 | t->tca__pad2 = 0; | |
1da177e4 | 1146 | |
4b3550ef | 1147 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
84ae017a AA |
1148 | if (!nest) { |
1149 | NL_SET_ERR_MSG(extack, "Failed to add new netlink message"); | |
8b00a53c | 1150 | goto out_module_put; |
84ae017a | 1151 | } |
1da177e4 | 1152 | |
41780105 | 1153 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops, extack); |
66dede2d DC |
1154 | if (err <= 0) { |
1155 | nla_nest_cancel(skb, nest); | |
8b00a53c | 1156 | goto out_module_put; |
66dede2d | 1157 | } |
1da177e4 | 1158 | |
4b3550ef | 1159 | nla_nest_end(skb, nest); |
1da177e4 | 1160 | |
27a884dc | 1161 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 | 1162 | nlh->nlmsg_flags |= NLM_F_ROOT; |
a85a970a | 1163 | module_put(ops->owner); |
15e47304 | 1164 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
cc7ec456 | 1165 | n->nlmsg_flags & NLM_F_ECHO); |
1da177e4 LT |
1166 | if (err > 0) |
1167 | return 0; | |
84ae017a AA |
1168 | if (err < 0) |
1169 | NL_SET_ERR_MSG(extack, "Failed to send TC action flush notification"); | |
1da177e4 LT |
1170 | |
1171 | return err; | |
1172 | ||
8b00a53c | 1173 | out_module_put: |
a85a970a | 1174 | module_put(ops->owner); |
1da177e4 LT |
1175 | err_out: |
1176 | kfree_skb(skb); | |
1da177e4 LT |
1177 | return err; |
1178 | } | |
1179 | ||
b144e7ec | 1180 | static int tcf_action_delete(struct net *net, struct tc_action *actions[]) |
16af6067 | 1181 | { |
97a3f84f | 1182 | int i; |
16af6067 | 1183 | |
90b73b77 VB |
1184 | for (i = 0; i < TCA_ACT_MAX_PRIO && actions[i]; i++) { |
1185 | struct tc_action *a = actions[i]; | |
16af6067 | 1186 | const struct tc_action_ops *ops = a->ops; |
16af6067 VB |
1187 | /* Actions can be deleted concurrently so we must save their |
1188 | * type and id to search again after reference is released. | |
1189 | */ | |
97a3f84f CW |
1190 | struct tcf_idrinfo *idrinfo = a->idrinfo; |
1191 | u32 act_index = a->tcfa_index; | |
16af6067 | 1192 | |
16af6067 VB |
1193 | if (tcf_action_put(a)) { |
1194 | /* last reference, action was deleted concurrently */ | |
1195 | module_put(ops->owner); | |
1196 | } else { | |
97a3f84f CW |
1197 | int ret; |
1198 | ||
16af6067 | 1199 | /* now do the delete */ |
97a3f84f | 1200 | ret = tcf_idr_delete_index(idrinfo, act_index); |
edfaf94f | 1201 | if (ret < 0) |
16af6067 VB |
1202 | return ret; |
1203 | } | |
edfaf94f | 1204 | actions[i] = NULL; |
16af6067 VB |
1205 | } |
1206 | return 0; | |
1207 | } | |
1208 | ||
a56e1953 | 1209 | static int |
90b73b77 | 1210 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
edfaf94f | 1211 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
a56e1953 WC |
1212 | { |
1213 | int ret; | |
1214 | struct sk_buff *skb; | |
1215 | ||
d04e6990 RM |
1216 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
1217 | GFP_KERNEL); | |
a56e1953 WC |
1218 | if (!skb) |
1219 | return -ENOBUFS; | |
1220 | ||
1221 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, | |
3f7c72bc | 1222 | 0, 2) <= 0) { |
84ae017a | 1223 | NL_SET_ERR_MSG(extack, "Failed to fill netlink TC action attributes"); |
a56e1953 WC |
1224 | kfree_skb(skb); |
1225 | return -EINVAL; | |
1226 | } | |
1227 | ||
1228 | /* now do the delete */ | |
b144e7ec | 1229 | ret = tcf_action_delete(net, actions); |
55334a5d | 1230 | if (ret < 0) { |
84ae017a | 1231 | NL_SET_ERR_MSG(extack, "Failed to delete TC action"); |
55334a5d WC |
1232 | kfree_skb(skb); |
1233 | return ret; | |
1234 | } | |
a56e1953 WC |
1235 | |
1236 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
1237 | n->nlmsg_flags & NLM_F_ECHO); | |
1238 | if (ret > 0) | |
1239 | return 0; | |
1240 | return ret; | |
1241 | } | |
1242 | ||
1da177e4 | 1243 | static int |
7316ae88 | 1244 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
84ae017a | 1245 | u32 portid, int event, struct netlink_ext_ack *extack) |
1da177e4 | 1246 | { |
cee63723 | 1247 | int i, ret; |
cc7ec456 | 1248 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 | 1249 | struct tc_action *act; |
d04e6990 | 1250 | size_t attr_size = 0; |
edfaf94f | 1251 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
1da177e4 | 1252 | |
84ae017a | 1253 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, extack); |
cee63723 PM |
1254 | if (ret < 0) |
1255 | return ret; | |
1da177e4 | 1256 | |
cc7ec456 | 1257 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
1af85155 | 1258 | if (tb[1]) |
84ae017a | 1259 | return tca_action_flush(net, tb[1], n, portid, extack); |
1af85155 | 1260 | |
84ae017a | 1261 | NL_SET_ERR_MSG(extack, "Invalid netlink attributes while flushing TC action"); |
1af85155 | 1262 | return -EINVAL; |
1da177e4 LT |
1263 | } |
1264 | ||
7ba699c6 | 1265 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
84ae017a | 1266 | act = tcf_action_get_1(net, tb[i], n, portid, extack); |
ab27cfb8 PM |
1267 | if (IS_ERR(act)) { |
1268 | ret = PTR_ERR(act); | |
1da177e4 | 1269 | goto err; |
ab27cfb8 | 1270 | } |
7ba699c6 | 1271 | act->order = i; |
4e76e75d | 1272 | attr_size += tcf_action_fill_size(act); |
90b73b77 | 1273 | actions[i - 1] = act; |
1da177e4 | 1274 | } |
4e76e75d RM |
1275 | |
1276 | attr_size = tcf_action_full_attrs_size(attr_size); | |
1da177e4 LT |
1277 | |
1278 | if (event == RTM_GETACTION) | |
90b73b77 | 1279 | ret = tcf_get_notify(net, portid, n, actions, event, extack); |
1da177e4 | 1280 | else { /* delete */ |
edfaf94f | 1281 | ret = tcf_del_notify(net, n, actions, portid, attr_size, extack); |
a56e1953 | 1282 | if (ret) |
1da177e4 | 1283 | goto err; |
edfaf94f | 1284 | return 0; |
1da177e4 LT |
1285 | } |
1286 | err: | |
edfaf94f | 1287 | tcf_action_put_many(actions); |
1da177e4 LT |
1288 | return ret; |
1289 | } | |
1290 | ||
a56e1953 | 1291 | static int |
90b73b77 | 1292 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct tc_action *actions[], |
d04e6990 | 1293 | u32 portid, size_t attr_size, struct netlink_ext_ack *extack) |
1da177e4 | 1294 | { |
1da177e4 | 1295 | struct sk_buff *skb; |
1da177e4 LT |
1296 | int err = 0; |
1297 | ||
d04e6990 RM |
1298 | skb = alloc_skb(attr_size <= NLMSG_GOODSIZE ? NLMSG_GOODSIZE : attr_size, |
1299 | GFP_KERNEL); | |
1da177e4 LT |
1300 | if (!skb) |
1301 | return -ENOBUFS; | |
1302 | ||
a56e1953 WC |
1303 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
1304 | RTM_NEWACTION, 0, 0) <= 0) { | |
d143b9e3 | 1305 | NL_SET_ERR_MSG(extack, "Failed to fill netlink attributes while adding TC action"); |
a56e1953 WC |
1306 | kfree_skb(skb); |
1307 | return -EINVAL; | |
1308 | } | |
10297b99 | 1309 | |
a56e1953 WC |
1310 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
1311 | n->nlmsg_flags & NLM_F_ECHO); | |
1da177e4 LT |
1312 | if (err > 0) |
1313 | err = 0; | |
1314 | return err; | |
1da177e4 LT |
1315 | } |
1316 | ||
5a7a5555 | 1317 | static int tcf_action_add(struct net *net, struct nlattr *nla, |
aea0d727 AA |
1318 | struct nlmsghdr *n, u32 portid, int ovr, |
1319 | struct netlink_ext_ack *extack) | |
1da177e4 | 1320 | { |
d04e6990 | 1321 | size_t attr_size = 0; |
1da177e4 | 1322 | int ret = 0; |
90b73b77 | 1323 | struct tc_action *actions[TCA_ACT_MAX_PRIO] = {}; |
1da177e4 | 1324 | |
90b73b77 | 1325 | ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, actions, |
789871bb | 1326 | &attr_size, true, extack); |
90b73b77 | 1327 | if (ret < 0) |
f07fed82 | 1328 | return ret; |
90b73b77 | 1329 | ret = tcf_add_notify(net, n, actions, portid, attr_size, extack); |
cae422f3 | 1330 | if (ovr) |
90b73b77 | 1331 | tcf_action_put_many(actions); |
1da177e4 | 1332 | |
cae422f3 | 1333 | return ret; |
1da177e4 LT |
1334 | } |
1335 | ||
90825b23 JHS |
1336 | static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; |
1337 | static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { | |
1338 | [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, | |
1339 | .validation_data = &tcaa_root_flags_allowed }, | |
e62e484d | 1340 | [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, |
90825b23 JHS |
1341 | }; |
1342 | ||
c21ef3e3 DA |
1343 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, |
1344 | struct netlink_ext_ack *extack) | |
1da177e4 | 1345 | { |
3b1e0a65 | 1346 | struct net *net = sock_net(skb->sk); |
90825b23 | 1347 | struct nlattr *tca[TCA_ROOT_MAX + 1]; |
15e47304 | 1348 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
1da177e4 LT |
1349 | int ret = 0, ovr = 0; |
1350 | ||
0b0f43fe JHS |
1351 | if ((n->nlmsg_type != RTM_GETACTION) && |
1352 | !netlink_capable(skb, CAP_NET_ADMIN)) | |
dfc47ef8 EB |
1353 | return -EPERM; |
1354 | ||
90825b23 | 1355 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, |
c21ef3e3 | 1356 | extack); |
7ba699c6 PM |
1357 | if (ret < 0) |
1358 | return ret; | |
1359 | ||
1360 | if (tca[TCA_ACT_TAB] == NULL) { | |
84ae017a | 1361 | NL_SET_ERR_MSG(extack, "Netlink action attributes missing"); |
1da177e4 LT |
1362 | return -EINVAL; |
1363 | } | |
1364 | ||
cc7ec456 | 1365 | /* n->nlmsg_flags & NLM_F_CREATE */ |
1da177e4 LT |
1366 | switch (n->nlmsg_type) { |
1367 | case RTM_NEWACTION: | |
1368 | /* we are going to assume all other flags | |
25985edc | 1369 | * imply create only if it doesn't exist |
1da177e4 LT |
1370 | * Note that CREATE | EXCL implies that |
1371 | * but since we want avoid ambiguity (eg when flags | |
1372 | * is zero) then just set this | |
1373 | */ | |
cc7ec456 | 1374 | if (n->nlmsg_flags & NLM_F_REPLACE) |
1da177e4 LT |
1375 | ovr = 1; |
1376 | replay: | |
aea0d727 AA |
1377 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr, |
1378 | extack); | |
1da177e4 LT |
1379 | if (ret == -EAGAIN) |
1380 | goto replay; | |
1381 | break; | |
1382 | case RTM_DELACTION: | |
7316ae88 | 1383 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
84ae017a | 1384 | portid, RTM_DELACTION, extack); |
1da177e4 LT |
1385 | break; |
1386 | case RTM_GETACTION: | |
7316ae88 | 1387 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
84ae017a | 1388 | portid, RTM_GETACTION, extack); |
1da177e4 LT |
1389 | break; |
1390 | default: | |
1391 | BUG(); | |
1392 | } | |
1393 | ||
1394 | return ret; | |
1395 | } | |
1396 | ||
90825b23 | 1397 | static struct nlattr *find_dump_kind(struct nlattr **nla) |
1da177e4 | 1398 | { |
cc7ec456 | 1399 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
7ba699c6 | 1400 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
7ba699c6 | 1401 | struct nlattr *kind; |
1da177e4 | 1402 | |
7ba699c6 | 1403 | tb1 = nla[TCA_ACT_TAB]; |
1da177e4 LT |
1404 | if (tb1 == NULL) |
1405 | return NULL; | |
1406 | ||
7ba699c6 | 1407 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
fceb6435 | 1408 | NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) |
1da177e4 | 1409 | return NULL; |
1da177e4 | 1410 | |
6d834e04 PM |
1411 | if (tb[1] == NULL) |
1412 | return NULL; | |
fceb6435 | 1413 | if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0) |
1da177e4 | 1414 | return NULL; |
7ba699c6 | 1415 | kind = tb2[TCA_ACT_KIND]; |
1da177e4 | 1416 | |
26dab893 | 1417 | return kind; |
1da177e4 LT |
1418 | } |
1419 | ||
5a7a5555 | 1420 | static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 | 1421 | { |
ddf97ccd | 1422 | struct net *net = sock_net(skb->sk); |
1da177e4 | 1423 | struct nlmsghdr *nlh; |
27a884dc | 1424 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 1425 | struct nlattr *nest; |
1da177e4 | 1426 | struct tc_action_ops *a_o; |
1da177e4 | 1427 | int ret = 0; |
8b00a53c | 1428 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
90825b23 JHS |
1429 | struct nlattr *tb[TCA_ROOT_MAX + 1]; |
1430 | struct nlattr *count_attr = NULL; | |
e62e484d | 1431 | unsigned long jiffy_since = 0; |
90825b23 JHS |
1432 | struct nlattr *kind = NULL; |
1433 | struct nla_bitfield32 bf; | |
e62e484d | 1434 | u32 msecs_since = 0; |
90825b23 JHS |
1435 | u32 act_count = 0; |
1436 | ||
1437 | ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, | |
1438 | tcaa_policy, NULL); | |
1439 | if (ret < 0) | |
1440 | return ret; | |
1da177e4 | 1441 | |
90825b23 | 1442 | kind = find_dump_kind(tb); |
1da177e4 | 1443 | if (kind == NULL) { |
6ff9c364 | 1444 | pr_info("tc_dump_action: action bad kind\n"); |
1da177e4 LT |
1445 | return 0; |
1446 | } | |
1447 | ||
26dab893 | 1448 | a_o = tc_lookup_action(kind); |
cc7ec456 | 1449 | if (a_o == NULL) |
1da177e4 | 1450 | return 0; |
1da177e4 | 1451 | |
90825b23 JHS |
1452 | cb->args[2] = 0; |
1453 | if (tb[TCA_ROOT_FLAGS]) { | |
1454 | bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); | |
1455 | cb->args[2] = bf.value; | |
1456 | } | |
1457 | ||
e62e484d JHS |
1458 | if (tb[TCA_ROOT_TIME_DELTA]) { |
1459 | msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); | |
1460 | } | |
1461 | ||
15e47304 | 1462 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
8b00a53c DM |
1463 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
1464 | if (!nlh) | |
1465 | goto out_module_put; | |
90825b23 | 1466 | |
e62e484d JHS |
1467 | if (msecs_since) |
1468 | jiffy_since = jiffies - msecs_to_jiffies(msecs_since); | |
1469 | ||
8b00a53c | 1470 | t = nlmsg_data(nlh); |
1da177e4 | 1471 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
1472 | t->tca__pad1 = 0; |
1473 | t->tca__pad2 = 0; | |
e62e484d | 1474 | cb->args[3] = jiffy_since; |
90825b23 JHS |
1475 | count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); |
1476 | if (!count_attr) | |
1477 | goto out_module_put; | |
1da177e4 | 1478 | |
4b3550ef PM |
1479 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
1480 | if (nest == NULL) | |
8b00a53c | 1481 | goto out_module_put; |
1da177e4 | 1482 | |
41780105 | 1483 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o, NULL); |
1da177e4 | 1484 | if (ret < 0) |
8b00a53c | 1485 | goto out_module_put; |
1da177e4 LT |
1486 | |
1487 | if (ret > 0) { | |
4b3550ef | 1488 | nla_nest_end(skb, nest); |
1da177e4 | 1489 | ret = skb->len; |
90825b23 JHS |
1490 | act_count = cb->args[1]; |
1491 | memcpy(nla_data(count_attr), &act_count, sizeof(u32)); | |
1492 | cb->args[1] = 0; | |
1da177e4 | 1493 | } else |
ebecaa66 | 1494 | nlmsg_trim(skb, b); |
1da177e4 | 1495 | |
27a884dc | 1496 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
15e47304 | 1497 | if (NETLINK_CB(cb->skb).portid && ret) |
1da177e4 LT |
1498 | nlh->nlmsg_flags |= NLM_F_MULTI; |
1499 | module_put(a_o->owner); | |
1500 | return skb->len; | |
1501 | ||
8b00a53c | 1502 | out_module_put: |
1da177e4 | 1503 | module_put(a_o->owner); |
dc5fc579 | 1504 | nlmsg_trim(skb, b); |
1da177e4 LT |
1505 | return skb->len; |
1506 | } | |
1507 | ||
b3f55bdd JP |
1508 | struct tcf_action_net { |
1509 | struct rhashtable egdev_ht; | |
1510 | }; | |
1511 | ||
1512 | static unsigned int tcf_action_net_id; | |
1513 | ||
1514 | struct tcf_action_egdev_cb { | |
1515 | struct list_head list; | |
1516 | tc_setup_cb_t *cb; | |
1517 | void *cb_priv; | |
1518 | }; | |
1519 | ||
1520 | struct tcf_action_egdev { | |
1521 | struct rhash_head ht_node; | |
1522 | const struct net_device *dev; | |
1523 | unsigned int refcnt; | |
1524 | struct list_head cb_list; | |
1525 | }; | |
1526 | ||
1527 | static const struct rhashtable_params tcf_action_egdev_ht_params = { | |
1528 | .key_offset = offsetof(struct tcf_action_egdev, dev), | |
1529 | .head_offset = offsetof(struct tcf_action_egdev, ht_node), | |
1530 | .key_len = sizeof(const struct net_device *), | |
1531 | }; | |
1532 | ||
1533 | static struct tcf_action_egdev * | |
1534 | tcf_action_egdev_lookup(const struct net_device *dev) | |
1535 | { | |
1536 | struct net *net = dev_net(dev); | |
1537 | struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); | |
1538 | ||
1539 | return rhashtable_lookup_fast(&tan->egdev_ht, &dev, | |
1540 | tcf_action_egdev_ht_params); | |
1541 | } | |
1542 | ||
1543 | static struct tcf_action_egdev * | |
1544 | tcf_action_egdev_get(const struct net_device *dev) | |
1545 | { | |
1546 | struct tcf_action_egdev *egdev; | |
1547 | struct tcf_action_net *tan; | |
1548 | ||
1549 | egdev = tcf_action_egdev_lookup(dev); | |
1550 | if (egdev) | |
1551 | goto inc_ref; | |
1552 | ||
1553 | egdev = kzalloc(sizeof(*egdev), GFP_KERNEL); | |
1554 | if (!egdev) | |
1555 | return NULL; | |
1556 | INIT_LIST_HEAD(&egdev->cb_list); | |
0843c092 | 1557 | egdev->dev = dev; |
b3f55bdd JP |
1558 | tan = net_generic(dev_net(dev), tcf_action_net_id); |
1559 | rhashtable_insert_fast(&tan->egdev_ht, &egdev->ht_node, | |
1560 | tcf_action_egdev_ht_params); | |
1561 | ||
1562 | inc_ref: | |
1563 | egdev->refcnt++; | |
1564 | return egdev; | |
1565 | } | |
1566 | ||
1567 | static void tcf_action_egdev_put(struct tcf_action_egdev *egdev) | |
1568 | { | |
1569 | struct tcf_action_net *tan; | |
1570 | ||
1571 | if (--egdev->refcnt) | |
1572 | return; | |
1573 | tan = net_generic(dev_net(egdev->dev), tcf_action_net_id); | |
1574 | rhashtable_remove_fast(&tan->egdev_ht, &egdev->ht_node, | |
1575 | tcf_action_egdev_ht_params); | |
1576 | kfree(egdev); | |
1577 | } | |
1578 | ||
1579 | static struct tcf_action_egdev_cb * | |
1580 | tcf_action_egdev_cb_lookup(struct tcf_action_egdev *egdev, | |
1581 | tc_setup_cb_t *cb, void *cb_priv) | |
1582 | { | |
1583 | struct tcf_action_egdev_cb *egdev_cb; | |
1584 | ||
1585 | list_for_each_entry(egdev_cb, &egdev->cb_list, list) | |
1586 | if (egdev_cb->cb == cb && egdev_cb->cb_priv == cb_priv) | |
1587 | return egdev_cb; | |
1588 | return NULL; | |
1589 | } | |
1590 | ||
1591 | static int tcf_action_egdev_cb_call(struct tcf_action_egdev *egdev, | |
1592 | enum tc_setup_type type, | |
1593 | void *type_data, bool err_stop) | |
1594 | { | |
1595 | struct tcf_action_egdev_cb *egdev_cb; | |
1596 | int ok_count = 0; | |
1597 | int err; | |
1598 | ||
1599 | list_for_each_entry(egdev_cb, &egdev->cb_list, list) { | |
1600 | err = egdev_cb->cb(type, type_data, egdev_cb->cb_priv); | |
1601 | if (err) { | |
1602 | if (err_stop) | |
1603 | return err; | |
1604 | } else { | |
1605 | ok_count++; | |
1606 | } | |
1607 | } | |
1608 | return ok_count; | |
1609 | } | |
1610 | ||
1611 | static int tcf_action_egdev_cb_add(struct tcf_action_egdev *egdev, | |
1612 | tc_setup_cb_t *cb, void *cb_priv) | |
1613 | { | |
1614 | struct tcf_action_egdev_cb *egdev_cb; | |
1615 | ||
1616 | egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv); | |
1617 | if (WARN_ON(egdev_cb)) | |
1618 | return -EEXIST; | |
1619 | egdev_cb = kzalloc(sizeof(*egdev_cb), GFP_KERNEL); | |
1620 | if (!egdev_cb) | |
1621 | return -ENOMEM; | |
1622 | egdev_cb->cb = cb; | |
1623 | egdev_cb->cb_priv = cb_priv; | |
1624 | list_add(&egdev_cb->list, &egdev->cb_list); | |
1625 | return 0; | |
1626 | } | |
1627 | ||
1628 | static void tcf_action_egdev_cb_del(struct tcf_action_egdev *egdev, | |
1629 | tc_setup_cb_t *cb, void *cb_priv) | |
1630 | { | |
1631 | struct tcf_action_egdev_cb *egdev_cb; | |
1632 | ||
1633 | egdev_cb = tcf_action_egdev_cb_lookup(egdev, cb, cb_priv); | |
1634 | if (WARN_ON(!egdev_cb)) | |
1635 | return; | |
1636 | list_del(&egdev_cb->list); | |
1637 | kfree(egdev_cb); | |
1638 | } | |
1639 | ||
1640 | static int __tc_setup_cb_egdev_register(const struct net_device *dev, | |
1641 | tc_setup_cb_t *cb, void *cb_priv) | |
1642 | { | |
1643 | struct tcf_action_egdev *egdev = tcf_action_egdev_get(dev); | |
1644 | int err; | |
1645 | ||
1646 | if (!egdev) | |
1647 | return -ENOMEM; | |
1648 | err = tcf_action_egdev_cb_add(egdev, cb, cb_priv); | |
1649 | if (err) | |
1650 | goto err_cb_add; | |
1651 | return 0; | |
1652 | ||
1653 | err_cb_add: | |
1654 | tcf_action_egdev_put(egdev); | |
1655 | return err; | |
1656 | } | |
1657 | int tc_setup_cb_egdev_register(const struct net_device *dev, | |
1658 | tc_setup_cb_t *cb, void *cb_priv) | |
1659 | { | |
1660 | int err; | |
1661 | ||
1662 | rtnl_lock(); | |
1663 | err = __tc_setup_cb_egdev_register(dev, cb, cb_priv); | |
1664 | rtnl_unlock(); | |
1665 | return err; | |
1666 | } | |
1667 | EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_register); | |
1668 | ||
1669 | static void __tc_setup_cb_egdev_unregister(const struct net_device *dev, | |
1670 | tc_setup_cb_t *cb, void *cb_priv) | |
1671 | { | |
1672 | struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev); | |
1673 | ||
1674 | if (WARN_ON(!egdev)) | |
1675 | return; | |
1676 | tcf_action_egdev_cb_del(egdev, cb, cb_priv); | |
1677 | tcf_action_egdev_put(egdev); | |
1678 | } | |
1679 | void tc_setup_cb_egdev_unregister(const struct net_device *dev, | |
1680 | tc_setup_cb_t *cb, void *cb_priv) | |
1681 | { | |
1682 | rtnl_lock(); | |
1683 | __tc_setup_cb_egdev_unregister(dev, cb, cb_priv); | |
1684 | rtnl_unlock(); | |
1685 | } | |
1686 | EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_unregister); | |
1687 | ||
1688 | int tc_setup_cb_egdev_call(const struct net_device *dev, | |
1689 | enum tc_setup_type type, void *type_data, | |
1690 | bool err_stop) | |
1691 | { | |
1692 | struct tcf_action_egdev *egdev = tcf_action_egdev_lookup(dev); | |
1693 | ||
1694 | if (!egdev) | |
1695 | return 0; | |
1696 | return tcf_action_egdev_cb_call(egdev, type, type_data, err_stop); | |
1697 | } | |
1698 | EXPORT_SYMBOL_GPL(tc_setup_cb_egdev_call); | |
1699 | ||
1700 | static __net_init int tcf_action_net_init(struct net *net) | |
1701 | { | |
1702 | struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); | |
1703 | ||
1704 | return rhashtable_init(&tan->egdev_ht, &tcf_action_egdev_ht_params); | |
1705 | } | |
1706 | ||
1707 | static void __net_exit tcf_action_net_exit(struct net *net) | |
1708 | { | |
1709 | struct tcf_action_net *tan = net_generic(net, tcf_action_net_id); | |
1710 | ||
1711 | rhashtable_destroy(&tan->egdev_ht); | |
1712 | } | |
1713 | ||
1714 | static struct pernet_operations tcf_action_net_ops = { | |
1715 | .init = tcf_action_net_init, | |
1716 | .exit = tcf_action_net_exit, | |
1717 | .id = &tcf_action_net_id, | |
1718 | .size = sizeof(struct tcf_action_net), | |
1719 | }; | |
1720 | ||
1da177e4 LT |
1721 | static int __init tc_action_init(void) |
1722 | { | |
b3f55bdd JP |
1723 | int err; |
1724 | ||
1725 | err = register_pernet_subsys(&tcf_action_net_ops); | |
1726 | if (err) | |
1727 | return err; | |
1728 | ||
b97bac64 FW |
1729 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); |
1730 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); | |
c7ac8679 | 1731 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
b97bac64 | 1732 | 0); |
1da177e4 | 1733 | |
1da177e4 LT |
1734 | return 0; |
1735 | } | |
1736 | ||
1737 | subsys_initcall(tc_action_init); |