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