]>
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; | |
367a8ce8 | 37 | a->goto_chain = tcf_chain_get(tp->chain->block, chain_index, true); |
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 | { | |
45 | tcf_chain_put(a->goto_chain); | |
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 | ||
d7fb60b9 CW |
56 | /* XXX: For standalone actions, we don't need a RCU grace period either, because |
57 | * actions are always connected to filters and filters are already destroyed in | |
58 | * RCU callbacks, so after a RCU grace period actions are already disconnected | |
59 | * from filters. Readers later can not find us. | |
60 | */ | |
61 | static void free_tcf(struct tc_action *p) | |
519c818e | 62 | { |
519c818e ED |
63 | free_percpu(p->cpu_bstats); |
64 | free_percpu(p->cpu_qstats); | |
1045ba77 JHS |
65 | |
66 | if (p->act_cookie) { | |
67 | kfree(p->act_cookie->data); | |
68 | kfree(p->act_cookie); | |
69 | } | |
db50514f JP |
70 | if (p->goto_chain) |
71 | tcf_action_goto_chain_fini(p); | |
1045ba77 | 72 | |
519c818e ED |
73 | kfree(p); |
74 | } | |
75 | ||
65a206c0 | 76 | static void tcf_idr_remove(struct tcf_idrinfo *idrinfo, struct tc_action *p) |
e9ce1cd3 | 77 | { |
65a206c0 CM |
78 | spin_lock_bh(&idrinfo->lock); |
79 | idr_remove_ext(&idrinfo->action_idr, p->tcfa_index); | |
80 | spin_unlock_bh(&idrinfo->lock); | |
1c0d32fd | 81 | gen_kill_estimator(&p->tcfa_rate_est); |
d7fb60b9 | 82 | free_tcf(p); |
e9ce1cd3 | 83 | } |
e9ce1cd3 | 84 | |
65a206c0 | 85 | int __tcf_idr_release(struct tc_action *p, bool bind, bool strict) |
e9ce1cd3 DM |
86 | { |
87 | int ret = 0; | |
88 | ||
89 | if (p) { | |
90 | if (bind) | |
ec0595cc WC |
91 | p->tcfa_bindcnt--; |
92 | else if (strict && p->tcfa_bindcnt > 0) | |
55334a5d | 93 | return -EPERM; |
e9ce1cd3 | 94 | |
ec0595cc WC |
95 | p->tcfa_refcnt--; |
96 | if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) { | |
97 | if (p->ops->cleanup) | |
98 | p->ops->cleanup(p, bind); | |
65a206c0 | 99 | tcf_idr_remove(p->idrinfo, p); |
1d4150c0 | 100 | ret = ACT_P_DELETED; |
e9ce1cd3 DM |
101 | } |
102 | } | |
28e6b67f | 103 | |
e9ce1cd3 DM |
104 | return ret; |
105 | } | |
65a206c0 | 106 | EXPORT_SYMBOL(__tcf_idr_release); |
e9ce1cd3 | 107 | |
65a206c0 | 108 | static int tcf_dump_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
a85a970a | 109 | struct netlink_callback *cb) |
e9ce1cd3 | 110 | { |
65a206c0 | 111 | int err = 0, index = -1, s_i = 0, n_i = 0; |
90825b23 | 112 | u32 act_flags = cb->args[2]; |
e62e484d | 113 | unsigned long jiffy_since = cb->args[3]; |
4b3550ef | 114 | struct nlattr *nest; |
65a206c0 CM |
115 | struct idr *idr = &idrinfo->action_idr; |
116 | struct tc_action *p; | |
117 | unsigned long id = 1; | |
e9ce1cd3 | 118 | |
65a206c0 | 119 | spin_lock_bh(&idrinfo->lock); |
e9ce1cd3 DM |
120 | |
121 | s_i = cb->args[0]; | |
122 | ||
65a206c0 CM |
123 | idr_for_each_entry_ext(idr, p, id) { |
124 | index++; | |
125 | if (index < s_i) | |
126 | continue; | |
127 | ||
128 | if (jiffy_since && | |
129 | time_after(jiffy_since, | |
130 | (unsigned long)p->tcfa_tm.lastuse)) | |
131 | continue; | |
132 | ||
133 | nest = nla_nest_start(skb, n_i); | |
134 | if (!nest) | |
135 | goto nla_put_failure; | |
136 | err = tcf_action_dump_1(skb, p, 0, 0); | |
137 | if (err < 0) { | |
138 | index--; | |
139 | nlmsg_trim(skb, nest); | |
140 | goto done; | |
e9ce1cd3 | 141 | } |
65a206c0 CM |
142 | nla_nest_end(skb, nest); |
143 | n_i++; | |
144 | if (!(act_flags & TCA_FLAG_LARGE_DUMP_ON) && | |
145 | n_i >= TCA_ACT_MAX_PRIO) | |
146 | goto done; | |
e9ce1cd3 DM |
147 | } |
148 | done: | |
e62e484d JHS |
149 | if (index >= 0) |
150 | cb->args[0] = index + 1; | |
151 | ||
65a206c0 | 152 | spin_unlock_bh(&idrinfo->lock); |
90825b23 | 153 | if (n_i) { |
90825b23 JHS |
154 | if (act_flags & TCA_FLAG_LARGE_DUMP_ON) |
155 | cb->args[1] = n_i; | |
156 | } | |
e9ce1cd3 DM |
157 | return n_i; |
158 | ||
7ba699c6 | 159 | nla_put_failure: |
4b3550ef | 160 | nla_nest_cancel(skb, nest); |
e9ce1cd3 DM |
161 | goto done; |
162 | } | |
163 | ||
65a206c0 | 164 | static int tcf_del_walker(struct tcf_idrinfo *idrinfo, struct sk_buff *skb, |
a85a970a | 165 | const struct tc_action_ops *ops) |
e9ce1cd3 | 166 | { |
4b3550ef | 167 | struct nlattr *nest; |
65a206c0 | 168 | int n_i = 0; |
55334a5d | 169 | int ret = -EINVAL; |
65a206c0 CM |
170 | struct idr *idr = &idrinfo->action_idr; |
171 | struct tc_action *p; | |
172 | unsigned long id = 1; | |
e9ce1cd3 | 173 | |
a85a970a | 174 | nest = nla_nest_start(skb, 0); |
4b3550ef PM |
175 | if (nest == NULL) |
176 | goto nla_put_failure; | |
a85a970a | 177 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
1b34ec43 | 178 | goto nla_put_failure; |
65a206c0 CM |
179 | |
180 | idr_for_each_entry_ext(idr, p, id) { | |
181 | ret = __tcf_idr_release(p, false, true); | |
182 | if (ret == ACT_P_DELETED) { | |
255cd50f | 183 | module_put(ops->owner); |
65a206c0 CM |
184 | n_i++; |
185 | } else if (ret < 0) { | |
186 | goto nla_put_failure; | |
e9ce1cd3 DM |
187 | } |
188 | } | |
1b34ec43 DM |
189 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
190 | goto nla_put_failure; | |
4b3550ef | 191 | nla_nest_end(skb, nest); |
e9ce1cd3 DM |
192 | |
193 | return n_i; | |
7ba699c6 | 194 | nla_put_failure: |
4b3550ef | 195 | nla_nest_cancel(skb, nest); |
55334a5d | 196 | return ret; |
e9ce1cd3 DM |
197 | } |
198 | ||
ddf97ccd WC |
199 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
200 | struct netlink_callback *cb, int type, | |
a85a970a | 201 | const struct tc_action_ops *ops) |
e9ce1cd3 | 202 | { |
65a206c0 | 203 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
ddf97ccd | 204 | |
e9ce1cd3 | 205 | if (type == RTM_DELACTION) { |
65a206c0 | 206 | return tcf_del_walker(idrinfo, skb, ops); |
e9ce1cd3 | 207 | } else if (type == RTM_GETACTION) { |
65a206c0 | 208 | return tcf_dump_walker(idrinfo, skb, cb); |
e9ce1cd3 | 209 | } else { |
6ff9c364 | 210 | WARN(1, "tcf_generic_walker: unknown action %d\n", type); |
e9ce1cd3 DM |
211 | return -EINVAL; |
212 | } | |
213 | } | |
ddf97ccd | 214 | EXPORT_SYMBOL(tcf_generic_walker); |
e9ce1cd3 | 215 | |
65a206c0 | 216 | static struct tc_action *tcf_idr_lookup(u32 index, struct tcf_idrinfo *idrinfo) |
e9ce1cd3 | 217 | { |
ec0595cc | 218 | struct tc_action *p = NULL; |
e9ce1cd3 | 219 | |
65a206c0 CM |
220 | spin_lock_bh(&idrinfo->lock); |
221 | p = idr_find_ext(&idrinfo->action_idr, index); | |
222 | spin_unlock_bh(&idrinfo->lock); | |
e9ce1cd3 DM |
223 | |
224 | return p; | |
225 | } | |
e9ce1cd3 | 226 | |
65a206c0 | 227 | int tcf_idr_search(struct tc_action_net *tn, struct tc_action **a, u32 index) |
e9ce1cd3 | 228 | { |
65a206c0 CM |
229 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
230 | struct tc_action *p = tcf_idr_lookup(index, idrinfo); | |
e9ce1cd3 DM |
231 | |
232 | if (p) { | |
ec0595cc | 233 | *a = p; |
e9ce1cd3 DM |
234 | return 1; |
235 | } | |
236 | return 0; | |
237 | } | |
65a206c0 | 238 | EXPORT_SYMBOL(tcf_idr_search); |
e9ce1cd3 | 239 | |
65a206c0 CM |
240 | bool tcf_idr_check(struct tc_action_net *tn, u32 index, struct tc_action **a, |
241 | int bind) | |
e9ce1cd3 | 242 | { |
65a206c0 CM |
243 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
244 | struct tc_action *p = tcf_idr_lookup(index, idrinfo); | |
ec0595cc | 245 | |
65a206c0 | 246 | if (index && p) { |
76aab2c1 | 247 | if (bind) |
ec0595cc WC |
248 | p->tcfa_bindcnt++; |
249 | p->tcfa_refcnt++; | |
250 | *a = p; | |
b2313077 | 251 | return true; |
e9ce1cd3 | 252 | } |
b2313077 | 253 | return false; |
e9ce1cd3 | 254 | } |
65a206c0 | 255 | EXPORT_SYMBOL(tcf_idr_check); |
e9ce1cd3 | 256 | |
65a206c0 | 257 | void tcf_idr_cleanup(struct tc_action *a, struct nlattr *est) |
86062033 | 258 | { |
86062033 | 259 | if (est) |
1c0d32fd | 260 | gen_kill_estimator(&a->tcfa_rate_est); |
d7fb60b9 | 261 | free_tcf(a); |
86062033 | 262 | } |
65a206c0 | 263 | EXPORT_SYMBOL(tcf_idr_cleanup); |
86062033 | 264 | |
65a206c0 CM |
265 | int tcf_idr_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
266 | struct tc_action **a, const struct tc_action_ops *ops, | |
267 | int bind, bool cpustats) | |
e9ce1cd3 | 268 | { |
ec0595cc | 269 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
65a206c0 CM |
270 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
271 | struct idr *idr = &idrinfo->action_idr; | |
519c818e | 272 | int err = -ENOMEM; |
65a206c0 | 273 | unsigned long idr_index; |
e9ce1cd3 DM |
274 | |
275 | if (unlikely(!p)) | |
86062033 | 276 | return -ENOMEM; |
ec0595cc | 277 | p->tcfa_refcnt = 1; |
e9ce1cd3 | 278 | if (bind) |
ec0595cc | 279 | p->tcfa_bindcnt = 1; |
e9ce1cd3 | 280 | |
519c818e ED |
281 | if (cpustats) { |
282 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); | |
283 | if (!p->cpu_bstats) { | |
284 | err1: | |
285 | kfree(p); | |
286 | return err; | |
287 | } | |
288 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); | |
289 | if (!p->cpu_qstats) { | |
290 | err2: | |
291 | free_percpu(p->cpu_bstats); | |
292 | goto err1; | |
293 | } | |
294 | } | |
ec0595cc | 295 | spin_lock_init(&p->tcfa_lock); |
65a206c0 CM |
296 | /* user doesn't specify an index */ |
297 | if (!index) { | |
2c8468dc | 298 | idr_preload(GFP_KERNEL); |
65a206c0 CM |
299 | spin_lock_bh(&idrinfo->lock); |
300 | err = idr_alloc_ext(idr, NULL, &idr_index, 1, 0, | |
2c8468dc | 301 | GFP_ATOMIC); |
65a206c0 | 302 | spin_unlock_bh(&idrinfo->lock); |
2c8468dc | 303 | idr_preload_end(); |
65a206c0 CM |
304 | if (err) { |
305 | err3: | |
306 | free_percpu(p->cpu_qstats); | |
307 | goto err2; | |
308 | } | |
309 | p->tcfa_index = idr_index; | |
310 | } else { | |
2c8468dc | 311 | idr_preload(GFP_KERNEL); |
65a206c0 CM |
312 | spin_lock_bh(&idrinfo->lock); |
313 | err = idr_alloc_ext(idr, NULL, NULL, index, index + 1, | |
2c8468dc | 314 | GFP_ATOMIC); |
65a206c0 | 315 | spin_unlock_bh(&idrinfo->lock); |
2c8468dc | 316 | idr_preload_end(); |
65a206c0 CM |
317 | if (err) |
318 | goto err3; | |
319 | p->tcfa_index = index; | |
320 | } | |
321 | ||
ec0595cc WC |
322 | p->tcfa_tm.install = jiffies; |
323 | p->tcfa_tm.lastuse = jiffies; | |
324 | p->tcfa_tm.firstuse = 0; | |
0e991ec6 | 325 | if (est) { |
ec0595cc WC |
326 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
327 | &p->tcfa_rate_est, | |
328 | &p->tcfa_lock, NULL, est); | |
0e991ec6 | 329 | if (err) { |
65a206c0 | 330 | goto err3; |
0e991ec6 SH |
331 | } |
332 | } | |
333 | ||
65a206c0 | 334 | p->idrinfo = idrinfo; |
ec0595cc WC |
335 | p->ops = ops; |
336 | INIT_LIST_HEAD(&p->list); | |
337 | *a = p; | |
86062033 | 338 | return 0; |
e9ce1cd3 | 339 | } |
65a206c0 | 340 | EXPORT_SYMBOL(tcf_idr_create); |
e9ce1cd3 | 341 | |
65a206c0 | 342 | void tcf_idr_insert(struct tc_action_net *tn, struct tc_action *a) |
e9ce1cd3 | 343 | { |
65a206c0 | 344 | struct tcf_idrinfo *idrinfo = tn->idrinfo; |
e9ce1cd3 | 345 | |
65a206c0 CM |
346 | spin_lock_bh(&idrinfo->lock); |
347 | idr_replace_ext(&idrinfo->action_idr, a, a->tcfa_index); | |
348 | spin_unlock_bh(&idrinfo->lock); | |
e9ce1cd3 | 349 | } |
65a206c0 | 350 | EXPORT_SYMBOL(tcf_idr_insert); |
1da177e4 | 351 | |
65a206c0 CM |
352 | void tcf_idrinfo_destroy(const struct tc_action_ops *ops, |
353 | struct tcf_idrinfo *idrinfo) | |
1d4150c0 | 354 | { |
65a206c0 CM |
355 | struct idr *idr = &idrinfo->action_idr; |
356 | struct tc_action *p; | |
357 | int ret; | |
358 | unsigned long id = 1; | |
1d4150c0 | 359 | |
65a206c0 CM |
360 | idr_for_each_entry_ext(idr, p, id) { |
361 | ret = __tcf_idr_release(p, false, true); | |
362 | if (ret == ACT_P_DELETED) | |
363 | module_put(ops->owner); | |
364 | else if (ret < 0) | |
365 | return; | |
1d4150c0 | 366 | } |
65a206c0 | 367 | idr_destroy(&idrinfo->action_idr); |
1d4150c0 | 368 | } |
65a206c0 | 369 | EXPORT_SYMBOL(tcf_idrinfo_destroy); |
1d4150c0 | 370 | |
1f747c26 | 371 | static LIST_HEAD(act_base); |
1da177e4 LT |
372 | static DEFINE_RWLOCK(act_mod_lock); |
373 | ||
ddf97ccd WC |
374 | int tcf_register_action(struct tc_action_ops *act, |
375 | struct pernet_operations *ops) | |
1da177e4 | 376 | { |
1f747c26 | 377 | struct tc_action_ops *a; |
ddf97ccd | 378 | int ret; |
1da177e4 | 379 | |
ddf97ccd | 380 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
76c82d7a JHS |
381 | return -EINVAL; |
382 | ||
ab102b80 WC |
383 | /* We have to register pernet ops before making the action ops visible, |
384 | * otherwise tcf_action_init_1() could get a partially initialized | |
385 | * netns. | |
386 | */ | |
387 | ret = register_pernet_subsys(ops); | |
388 | if (ret) | |
389 | return ret; | |
390 | ||
1da177e4 | 391 | write_lock(&act_mod_lock); |
1f747c26 | 392 | list_for_each_entry(a, &act_base, head) { |
1da177e4 LT |
393 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
394 | write_unlock(&act_mod_lock); | |
ab102b80 | 395 | unregister_pernet_subsys(ops); |
1da177e4 LT |
396 | return -EEXIST; |
397 | } | |
398 | } | |
1f747c26 | 399 | list_add_tail(&act->head, &act_base); |
1da177e4 | 400 | write_unlock(&act_mod_lock); |
ddf97ccd | 401 | |
1da177e4 LT |
402 | return 0; |
403 | } | |
62e3ba1b | 404 | EXPORT_SYMBOL(tcf_register_action); |
1da177e4 | 405 | |
ddf97ccd WC |
406 | int tcf_unregister_action(struct tc_action_ops *act, |
407 | struct pernet_operations *ops) | |
1da177e4 | 408 | { |
1f747c26 | 409 | struct tc_action_ops *a; |
1da177e4 LT |
410 | int err = -ENOENT; |
411 | ||
412 | write_lock(&act_mod_lock); | |
a792866a ED |
413 | list_for_each_entry(a, &act_base, head) { |
414 | if (a == act) { | |
415 | list_del(&act->head); | |
416 | err = 0; | |
1da177e4 | 417 | break; |
a792866a | 418 | } |
1da177e4 LT |
419 | } |
420 | write_unlock(&act_mod_lock); | |
ab102b80 WC |
421 | if (!err) |
422 | unregister_pernet_subsys(ops); | |
1da177e4 LT |
423 | return err; |
424 | } | |
62e3ba1b | 425 | EXPORT_SYMBOL(tcf_unregister_action); |
1da177e4 LT |
426 | |
427 | /* lookup by name */ | |
428 | static struct tc_action_ops *tc_lookup_action_n(char *kind) | |
429 | { | |
a792866a | 430 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
431 | |
432 | if (kind) { | |
433 | read_lock(&act_mod_lock); | |
1f747c26 | 434 | list_for_each_entry(a, &act_base, head) { |
1da177e4 | 435 | if (strcmp(kind, a->kind) == 0) { |
a792866a ED |
436 | if (try_module_get(a->owner)) |
437 | res = a; | |
1da177e4 LT |
438 | break; |
439 | } | |
440 | } | |
441 | read_unlock(&act_mod_lock); | |
442 | } | |
a792866a | 443 | return res; |
1da177e4 LT |
444 | } |
445 | ||
7ba699c6 PM |
446 | /* lookup by nlattr */ |
447 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) | |
1da177e4 | 448 | { |
a792866a | 449 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
450 | |
451 | if (kind) { | |
452 | read_lock(&act_mod_lock); | |
1f747c26 | 453 | list_for_each_entry(a, &act_base, head) { |
7ba699c6 | 454 | if (nla_strcmp(kind, a->kind) == 0) { |
a792866a ED |
455 | if (try_module_get(a->owner)) |
456 | res = a; | |
1da177e4 LT |
457 | break; |
458 | } | |
459 | } | |
460 | read_unlock(&act_mod_lock); | |
461 | } | |
a792866a | 462 | return res; |
1da177e4 | 463 | } |
1da177e4 | 464 | |
e0ee84de JHS |
465 | /*TCA_ACT_MAX_PRIO is 32, there count upto 32 */ |
466 | #define TCA_ACT_MAX_PRIO_MASK 0x1FF | |
22dc13c8 WC |
467 | int tcf_action_exec(struct sk_buff *skb, struct tc_action **actions, |
468 | int nr_actions, struct tcf_result *res) | |
1da177e4 | 469 | { |
e0ee84de JHS |
470 | u32 jmp_prgcnt = 0; |
471 | u32 jmp_ttl = TCA_ACT_MAX_PRIO; /*matches actions per filter */ | |
ec1a9cca JP |
472 | int i; |
473 | int ret = TC_ACT_OK; | |
1da177e4 | 474 | |
e7246e12 WB |
475 | if (skb_skip_tc_classify(skb)) |
476 | return TC_ACT_OK; | |
477 | ||
e0ee84de | 478 | restart_act_graph: |
22dc13c8 WC |
479 | for (i = 0; i < nr_actions; i++) { |
480 | const struct tc_action *a = actions[i]; | |
481 | ||
e0ee84de JHS |
482 | if (jmp_prgcnt > 0) { |
483 | jmp_prgcnt -= 1; | |
484 | continue; | |
485 | } | |
1da177e4 | 486 | repeat: |
63acd680 | 487 | ret = a->ops->act(skb, a, res); |
63acd680 JHS |
488 | if (ret == TC_ACT_REPEAT) |
489 | goto repeat; /* we need a ttl - JHS */ | |
e0ee84de | 490 | |
9da3242e | 491 | if (TC_ACT_EXT_CMP(ret, TC_ACT_JUMP)) { |
e0ee84de JHS |
492 | jmp_prgcnt = ret & TCA_ACT_MAX_PRIO_MASK; |
493 | if (!jmp_prgcnt || (jmp_prgcnt > nr_actions)) { | |
494 | /* faulty opcode, stop pipeline */ | |
495 | return TC_ACT_OK; | |
496 | } else { | |
497 | jmp_ttl -= 1; | |
498 | if (jmp_ttl > 0) | |
499 | goto restart_act_graph; | |
500 | else /* faulty graph, stop pipeline */ | |
501 | return TC_ACT_OK; | |
502 | } | |
db50514f JP |
503 | } else if (TC_ACT_EXT_CMP(ret, TC_ACT_GOTO_CHAIN)) { |
504 | tcf_action_goto_chain_exec(a, res); | |
e0ee84de JHS |
505 | } |
506 | ||
63acd680 | 507 | if (ret != TC_ACT_PIPE) |
e7246e12 | 508 | break; |
1da177e4 | 509 | } |
e0ee84de | 510 | |
1da177e4 LT |
511 | return ret; |
512 | } | |
62e3ba1b | 513 | EXPORT_SYMBOL(tcf_action_exec); |
1da177e4 | 514 | |
55334a5d | 515 | int tcf_action_destroy(struct list_head *actions, int bind) |
1da177e4 | 516 | { |
255cd50f | 517 | const struct tc_action_ops *ops; |
33be6271 | 518 | struct tc_action *a, *tmp; |
55334a5d | 519 | int ret = 0; |
1da177e4 | 520 | |
33be6271 | 521 | list_for_each_entry_safe(a, tmp, actions, list) { |
255cd50f | 522 | ops = a->ops; |
65a206c0 | 523 | ret = __tcf_idr_release(a, bind, true); |
55334a5d | 524 | if (ret == ACT_P_DELETED) |
255cd50f | 525 | module_put(ops->owner); |
55334a5d WC |
526 | else if (ret < 0) |
527 | return ret; | |
1da177e4 | 528 | } |
55334a5d | 529 | return ret; |
1da177e4 LT |
530 | } |
531 | ||
532 | int | |
533 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
534 | { | |
1da177e4 LT |
535 | return a->ops->dump(skb, a, bind, ref); |
536 | } | |
537 | ||
538 | int | |
539 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
540 | { | |
541 | int err = -EINVAL; | |
27a884dc | 542 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 543 | struct nlattr *nest; |
1da177e4 | 544 | |
1b34ec43 DM |
545 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
546 | goto nla_put_failure; | |
1da177e4 | 547 | if (tcf_action_copy_stats(skb, a, 0)) |
7ba699c6 | 548 | goto nla_put_failure; |
1045ba77 JHS |
549 | if (a->act_cookie) { |
550 | if (nla_put(skb, TCA_ACT_COOKIE, a->act_cookie->len, | |
551 | a->act_cookie->data)) | |
552 | goto nla_put_failure; | |
553 | } | |
554 | ||
4b3550ef PM |
555 | nest = nla_nest_start(skb, TCA_OPTIONS); |
556 | if (nest == NULL) | |
557 | goto nla_put_failure; | |
cc7ec456 ED |
558 | err = tcf_action_dump_old(skb, a, bind, ref); |
559 | if (err > 0) { | |
4b3550ef | 560 | nla_nest_end(skb, nest); |
1da177e4 LT |
561 | return err; |
562 | } | |
563 | ||
7ba699c6 | 564 | nla_put_failure: |
dc5fc579 | 565 | nlmsg_trim(skb, b); |
1da177e4 LT |
566 | return -1; |
567 | } | |
62e3ba1b | 568 | EXPORT_SYMBOL(tcf_action_dump_1); |
1da177e4 | 569 | |
0b0f43fe JHS |
570 | int tcf_action_dump(struct sk_buff *skb, struct list_head *actions, |
571 | int bind, int ref) | |
1da177e4 LT |
572 | { |
573 | struct tc_action *a; | |
574 | int err = -EINVAL; | |
4b3550ef | 575 | struct nlattr *nest; |
1da177e4 | 576 | |
33be6271 | 577 | list_for_each_entry(a, actions, list) { |
4b3550ef PM |
578 | nest = nla_nest_start(skb, a->order); |
579 | if (nest == NULL) | |
580 | goto nla_put_failure; | |
1da177e4 LT |
581 | err = tcf_action_dump_1(skb, a, bind, ref); |
582 | if (err < 0) | |
4fe683f5 | 583 | goto errout; |
4b3550ef | 584 | nla_nest_end(skb, nest); |
1da177e4 LT |
585 | } |
586 | ||
587 | return 0; | |
588 | ||
7ba699c6 | 589 | nla_put_failure: |
4fe683f5 TG |
590 | err = -EINVAL; |
591 | errout: | |
4b3550ef | 592 | nla_nest_cancel(skb, nest); |
4fe683f5 | 593 | return err; |
1da177e4 LT |
594 | } |
595 | ||
e0535ce5 | 596 | static struct tc_cookie *nla_memdup_cookie(struct nlattr **tb) |
1045ba77 | 597 | { |
e0535ce5 WB |
598 | struct tc_cookie *c = kzalloc(sizeof(*c), GFP_KERNEL); |
599 | if (!c) | |
600 | return NULL; | |
601 | ||
602 | c->data = nla_memdup(tb[TCA_ACT_COOKIE], GFP_KERNEL); | |
603 | if (!c->data) { | |
604 | kfree(c); | |
605 | return NULL; | |
1045ba77 | 606 | } |
e0535ce5 | 607 | c->len = nla_len(tb[TCA_ACT_COOKIE]); |
1045ba77 | 608 | |
e0535ce5 | 609 | return c; |
1045ba77 JHS |
610 | } |
611 | ||
9fb9f251 JP |
612 | struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp, |
613 | struct nlattr *nla, struct nlattr *est, | |
614 | char *name, int ovr, int bind) | |
1da177e4 LT |
615 | { |
616 | struct tc_action *a; | |
617 | struct tc_action_ops *a_o; | |
e0535ce5 | 618 | struct tc_cookie *cookie = NULL; |
1da177e4 | 619 | char act_name[IFNAMSIZ]; |
cc7ec456 | 620 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
7ba699c6 | 621 | struct nlattr *kind; |
ab27cfb8 | 622 | int err; |
1da177e4 | 623 | |
1da177e4 | 624 | if (name == NULL) { |
fceb6435 | 625 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
cee63723 | 626 | if (err < 0) |
1da177e4 | 627 | goto err_out; |
cee63723 | 628 | err = -EINVAL; |
7ba699c6 | 629 | kind = tb[TCA_ACT_KIND]; |
1da177e4 LT |
630 | if (kind == NULL) |
631 | goto err_out; | |
7ba699c6 | 632 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) |
1da177e4 | 633 | goto err_out; |
e0535ce5 WB |
634 | if (tb[TCA_ACT_COOKIE]) { |
635 | int cklen = nla_len(tb[TCA_ACT_COOKIE]); | |
636 | ||
637 | if (cklen > TC_COOKIE_MAX_SIZE) | |
638 | goto err_out; | |
639 | ||
640 | cookie = nla_memdup_cookie(tb); | |
641 | if (!cookie) { | |
642 | err = -ENOMEM; | |
643 | goto err_out; | |
644 | } | |
645 | } | |
1da177e4 | 646 | } else { |
cee63723 | 647 | err = -EINVAL; |
1da177e4 LT |
648 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) |
649 | goto err_out; | |
650 | } | |
651 | ||
652 | a_o = tc_lookup_action_n(act_name); | |
653 | if (a_o == NULL) { | |
95a5afca | 654 | #ifdef CONFIG_MODULES |
1da177e4 | 655 | rtnl_unlock(); |
4bba3925 | 656 | request_module("act_%s", act_name); |
1da177e4 LT |
657 | rtnl_lock(); |
658 | ||
659 | a_o = tc_lookup_action_n(act_name); | |
660 | ||
661 | /* We dropped the RTNL semaphore in order to | |
662 | * perform the module load. So, even if we | |
663 | * succeeded in loading the module we have to | |
664 | * tell the caller to replay the request. We | |
665 | * indicate this using -EAGAIN. | |
666 | */ | |
667 | if (a_o != NULL) { | |
ab27cfb8 | 668 | err = -EAGAIN; |
1da177e4 LT |
669 | goto err_mod; |
670 | } | |
671 | #endif | |
ab27cfb8 | 672 | err = -ENOENT; |
1da177e4 LT |
673 | goto err_out; |
674 | } | |
675 | ||
1da177e4 LT |
676 | /* backward compatibility for policer */ |
677 | if (name == NULL) | |
a85a970a | 678 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind); |
1da177e4 | 679 | else |
a85a970a | 680 | err = a_o->init(net, nla, est, &a, ovr, bind); |
ab27cfb8 | 681 | if (err < 0) |
a85a970a | 682 | goto err_mod; |
1da177e4 | 683 | |
e0535ce5 WB |
684 | if (name == NULL && tb[TCA_ACT_COOKIE]) { |
685 | if (a->act_cookie) { | |
686 | kfree(a->act_cookie->data); | |
687 | kfree(a->act_cookie); | |
1045ba77 | 688 | } |
e0535ce5 | 689 | a->act_cookie = cookie; |
1045ba77 JHS |
690 | } |
691 | ||
1da177e4 | 692 | /* module count goes up only when brand new policy is created |
cc7ec456 ED |
693 | * if it exists and is only bound to in a_o->init() then |
694 | * ACT_P_CREATED is not returned (a zero is). | |
695 | */ | |
ab27cfb8 | 696 | if (err != ACT_P_CREATED) |
1da177e4 | 697 | module_put(a_o->owner); |
1da177e4 | 698 | |
db50514f JP |
699 | if (TC_ACT_EXT_CMP(a->tcfa_action, TC_ACT_GOTO_CHAIN)) { |
700 | err = tcf_action_goto_chain_init(a, tp); | |
701 | if (err) { | |
702 | LIST_HEAD(actions); | |
703 | ||
704 | list_add_tail(&a->list, &actions); | |
705 | tcf_action_destroy(&actions, bind); | |
706 | return ERR_PTR(err); | |
707 | } | |
708 | } | |
709 | ||
1da177e4 LT |
710 | return a; |
711 | ||
1da177e4 LT |
712 | err_mod: |
713 | module_put(a_o->owner); | |
714 | err_out: | |
e0535ce5 WB |
715 | if (cookie) { |
716 | kfree(cookie->data); | |
717 | kfree(cookie); | |
718 | } | |
ab27cfb8 | 719 | return ERR_PTR(err); |
1da177e4 LT |
720 | } |
721 | ||
aecc5cef JHS |
722 | static void cleanup_a(struct list_head *actions, int ovr) |
723 | { | |
724 | struct tc_action *a; | |
725 | ||
726 | if (!ovr) | |
727 | return; | |
728 | ||
729 | list_for_each_entry(a, actions, list) | |
730 | a->tcfa_refcnt--; | |
731 | } | |
732 | ||
9fb9f251 JP |
733 | int tcf_action_init(struct net *net, struct tcf_proto *tp, struct nlattr *nla, |
734 | struct nlattr *est, char *name, int ovr, int bind, | |
735 | struct list_head *actions) | |
1da177e4 | 736 | { |
cc7ec456 | 737 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 | 738 | struct tc_action *act; |
cee63723 | 739 | int err; |
1da177e4 LT |
740 | int i; |
741 | ||
fceb6435 | 742 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); |
cee63723 | 743 | if (err < 0) |
33be6271 | 744 | return err; |
1da177e4 | 745 | |
7ba699c6 | 746 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
9fb9f251 | 747 | act = tcf_action_init_1(net, tp, tb[i], est, name, ovr, bind); |
33be6271 WC |
748 | if (IS_ERR(act)) { |
749 | err = PTR_ERR(act); | |
1da177e4 | 750 | goto err; |
33be6271 | 751 | } |
7ba699c6 | 752 | act->order = i; |
aecc5cef JHS |
753 | if (ovr) |
754 | act->tcfa_refcnt++; | |
33be6271 | 755 | list_add_tail(&act->list, actions); |
1da177e4 | 756 | } |
aecc5cef JHS |
757 | |
758 | /* Remove the temp refcnt which was necessary to protect against | |
759 | * destroying an existing action which was being replaced | |
760 | */ | |
761 | cleanup_a(actions, ovr); | |
33be6271 | 762 | return 0; |
1da177e4 LT |
763 | |
764 | err: | |
33be6271 WC |
765 | tcf_action_destroy(actions, bind); |
766 | return err; | |
1da177e4 LT |
767 | } |
768 | ||
ec0595cc | 769 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, |
1da177e4 LT |
770 | int compat_mode) |
771 | { | |
772 | int err = 0; | |
773 | struct gnet_dump d; | |
10297b99 | 774 | |
7eb8896d | 775 | if (p == NULL) |
1da177e4 LT |
776 | goto errout; |
777 | ||
778 | /* compat_mode being true specifies a call that is supposed | |
06fe9fb4 | 779 | * to add additional backward compatibility statistic TLVs. |
1da177e4 LT |
780 | */ |
781 | if (compat_mode) { | |
ec0595cc | 782 | if (p->type == TCA_OLD_COMPAT) |
1da177e4 | 783 | err = gnet_stats_start_copy_compat(skb, 0, |
9854518e ND |
784 | TCA_STATS, |
785 | TCA_XSTATS, | |
ec0595cc | 786 | &p->tcfa_lock, &d, |
9854518e | 787 | TCA_PAD); |
1da177e4 LT |
788 | else |
789 | return 0; | |
790 | } else | |
791 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, | |
ec0595cc | 792 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
1da177e4 LT |
793 | |
794 | if (err < 0) | |
795 | goto errout; | |
796 | ||
ec0595cc | 797 | if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || |
1c0d32fd | 798 | gnet_stats_copy_rate_est(&d, &p->tcfa_rate_est) < 0 || |
519c818e | 799 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
ec0595cc WC |
800 | &p->tcfa_qstats, |
801 | p->tcfa_qstats.qlen) < 0) | |
1da177e4 LT |
802 | goto errout; |
803 | ||
804 | if (gnet_stats_finish_copy(&d) < 0) | |
805 | goto errout; | |
806 | ||
807 | return 0; | |
808 | ||
809 | errout: | |
810 | return -1; | |
811 | } | |
812 | ||
0b0f43fe JHS |
813 | static int tca_get_fill(struct sk_buff *skb, struct list_head *actions, |
814 | u32 portid, u32 seq, u16 flags, int event, int bind, | |
815 | int ref) | |
1da177e4 LT |
816 | { |
817 | struct tcamsg *t; | |
818 | struct nlmsghdr *nlh; | |
27a884dc | 819 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 820 | struct nlattr *nest; |
1da177e4 | 821 | |
15e47304 | 822 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
8b00a53c DM |
823 | if (!nlh) |
824 | goto out_nlmsg_trim; | |
825 | t = nlmsg_data(nlh); | |
1da177e4 | 826 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
827 | t->tca__pad1 = 0; |
828 | t->tca__pad2 = 0; | |
10297b99 | 829 | |
4b3550ef PM |
830 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
831 | if (nest == NULL) | |
8b00a53c | 832 | goto out_nlmsg_trim; |
1da177e4 | 833 | |
33be6271 | 834 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
8b00a53c | 835 | goto out_nlmsg_trim; |
1da177e4 | 836 | |
4b3550ef | 837 | nla_nest_end(skb, nest); |
10297b99 | 838 | |
27a884dc | 839 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
840 | return skb->len; |
841 | ||
8b00a53c | 842 | out_nlmsg_trim: |
dc5fc579 | 843 | nlmsg_trim(skb, b); |
1da177e4 LT |
844 | return -1; |
845 | } | |
846 | ||
847 | static int | |
c4c4290c | 848 | tcf_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
33be6271 | 849 | struct list_head *actions, int event) |
1da177e4 LT |
850 | { |
851 | struct sk_buff *skb; | |
1da177e4 LT |
852 | |
853 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
854 | if (!skb) | |
855 | return -ENOBUFS; | |
0b0f43fe JHS |
856 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
857 | 0, 0) <= 0) { | |
1da177e4 LT |
858 | kfree_skb(skb); |
859 | return -EINVAL; | |
860 | } | |
2942e900 | 861 | |
15e47304 | 862 | return rtnl_unicast(skb, net, portid); |
1da177e4 LT |
863 | } |
864 | ||
ddf97ccd WC |
865 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
866 | struct nlmsghdr *n, u32 portid) | |
1da177e4 | 867 | { |
cc7ec456 | 868 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 869 | const struct tc_action_ops *ops; |
1da177e4 LT |
870 | struct tc_action *a; |
871 | int index; | |
ab27cfb8 | 872 | int err; |
1da177e4 | 873 | |
fceb6435 | 874 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
cee63723 | 875 | if (err < 0) |
ab27cfb8 | 876 | goto err_out; |
1da177e4 | 877 | |
cee63723 | 878 | err = -EINVAL; |
7ba699c6 PM |
879 | if (tb[TCA_ACT_INDEX] == NULL || |
880 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) | |
ab27cfb8 | 881 | goto err_out; |
1587bac4 | 882 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
1da177e4 | 883 | |
ab27cfb8 | 884 | err = -EINVAL; |
a85a970a WC |
885 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
886 | if (!ops) /* could happen in batch of actions */ | |
887 | goto err_out; | |
ab27cfb8 | 888 | err = -ENOENT; |
a85a970a | 889 | if (ops->lookup(net, &a, index) == 0) |
1da177e4 LT |
890 | goto err_mod; |
891 | ||
a85a970a | 892 | module_put(ops->owner); |
1da177e4 | 893 | return a; |
ab27cfb8 | 894 | |
1da177e4 | 895 | err_mod: |
a85a970a | 896 | module_put(ops->owner); |
ab27cfb8 PM |
897 | err_out: |
898 | return ERR_PTR(err); | |
1da177e4 LT |
899 | } |
900 | ||
7316ae88 | 901 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
15e47304 | 902 | struct nlmsghdr *n, u32 portid) |
1da177e4 LT |
903 | { |
904 | struct sk_buff *skb; | |
905 | unsigned char *b; | |
906 | struct nlmsghdr *nlh; | |
907 | struct tcamsg *t; | |
908 | struct netlink_callback dcb; | |
4b3550ef | 909 | struct nlattr *nest; |
cc7ec456 | 910 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 911 | const struct tc_action_ops *ops; |
7ba699c6 | 912 | struct nlattr *kind; |
36723873 | 913 | int err = -ENOMEM; |
1da177e4 | 914 | |
1da177e4 LT |
915 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
916 | if (!skb) { | |
6ff9c364 | 917 | pr_debug("tca_action_flush: failed skb alloc\n"); |
36723873 | 918 | return err; |
1da177e4 LT |
919 | } |
920 | ||
27a884dc | 921 | b = skb_tail_pointer(skb); |
1da177e4 | 922 | |
fceb6435 | 923 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL, NULL); |
cee63723 | 924 | if (err < 0) |
1da177e4 LT |
925 | goto err_out; |
926 | ||
cee63723 | 927 | err = -EINVAL; |
7ba699c6 | 928 | kind = tb[TCA_ACT_KIND]; |
a85a970a WC |
929 | ops = tc_lookup_action(kind); |
930 | if (!ops) /*some idjot trying to flush unknown action */ | |
1da177e4 LT |
931 | goto err_out; |
932 | ||
0b0f43fe JHS |
933 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
934 | sizeof(*t), 0); | |
8b00a53c DM |
935 | if (!nlh) |
936 | goto out_module_put; | |
937 | t = nlmsg_data(nlh); | |
1da177e4 | 938 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
939 | t->tca__pad1 = 0; |
940 | t->tca__pad2 = 0; | |
1da177e4 | 941 | |
4b3550ef PM |
942 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
943 | if (nest == NULL) | |
8b00a53c | 944 | goto out_module_put; |
1da177e4 | 945 | |
a85a970a | 946 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops); |
edb9d1bf | 947 | if (err <= 0) |
8b00a53c | 948 | goto out_module_put; |
1da177e4 | 949 | |
4b3550ef | 950 | nla_nest_end(skb, nest); |
1da177e4 | 951 | |
27a884dc | 952 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 | 953 | nlh->nlmsg_flags |= NLM_F_ROOT; |
a85a970a | 954 | module_put(ops->owner); |
15e47304 | 955 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
cc7ec456 | 956 | n->nlmsg_flags & NLM_F_ECHO); |
1da177e4 LT |
957 | if (err > 0) |
958 | return 0; | |
959 | ||
960 | return err; | |
961 | ||
8b00a53c | 962 | out_module_put: |
a85a970a | 963 | module_put(ops->owner); |
1da177e4 LT |
964 | err_out: |
965 | kfree_skb(skb); | |
1da177e4 LT |
966 | return err; |
967 | } | |
968 | ||
a56e1953 WC |
969 | static int |
970 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, | |
971 | u32 portid) | |
972 | { | |
973 | int ret; | |
974 | struct sk_buff *skb; | |
975 | ||
976 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
977 | if (!skb) | |
978 | return -ENOBUFS; | |
979 | ||
980 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, | |
981 | 0, 1) <= 0) { | |
982 | kfree_skb(skb); | |
983 | return -EINVAL; | |
984 | } | |
985 | ||
986 | /* now do the delete */ | |
55334a5d WC |
987 | ret = tcf_action_destroy(actions, 0); |
988 | if (ret < 0) { | |
989 | kfree_skb(skb); | |
990 | return ret; | |
991 | } | |
a56e1953 WC |
992 | |
993 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
994 | n->nlmsg_flags & NLM_F_ECHO); | |
995 | if (ret > 0) | |
996 | return 0; | |
997 | return ret; | |
998 | } | |
999 | ||
1da177e4 | 1000 | static int |
7316ae88 | 1001 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
15e47304 | 1002 | u32 portid, int event) |
1da177e4 | 1003 | { |
cee63723 | 1004 | int i, ret; |
cc7ec456 | 1005 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 WC |
1006 | struct tc_action *act; |
1007 | LIST_HEAD(actions); | |
1da177e4 | 1008 | |
fceb6435 | 1009 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL, NULL); |
cee63723 PM |
1010 | if (ret < 0) |
1011 | return ret; | |
1da177e4 | 1012 | |
cc7ec456 | 1013 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
f97017cd | 1014 | if (tb[1] != NULL) |
15e47304 | 1015 | return tca_action_flush(net, tb[1], n, portid); |
f97017cd JHS |
1016 | else |
1017 | return -EINVAL; | |
1da177e4 LT |
1018 | } |
1019 | ||
7ba699c6 | 1020 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
ddf97ccd | 1021 | act = tcf_action_get_1(net, tb[i], n, portid); |
ab27cfb8 PM |
1022 | if (IS_ERR(act)) { |
1023 | ret = PTR_ERR(act); | |
1da177e4 | 1024 | goto err; |
ab27cfb8 | 1025 | } |
7ba699c6 | 1026 | act->order = i; |
33be6271 | 1027 | list_add_tail(&act->list, &actions); |
1da177e4 LT |
1028 | } |
1029 | ||
1030 | if (event == RTM_GETACTION) | |
c4c4290c | 1031 | ret = tcf_get_notify(net, portid, n, &actions, event); |
1da177e4 | 1032 | else { /* delete */ |
a56e1953 WC |
1033 | ret = tcf_del_notify(net, n, &actions, portid); |
1034 | if (ret) | |
1da177e4 | 1035 | goto err; |
1da177e4 LT |
1036 | return ret; |
1037 | } | |
1038 | err: | |
0faa9cb5 JHS |
1039 | if (event != RTM_GETACTION) |
1040 | tcf_action_destroy(&actions, 0); | |
1da177e4 LT |
1041 | return ret; |
1042 | } | |
1043 | ||
a56e1953 WC |
1044 | static int |
1045 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, | |
1046 | u32 portid) | |
1da177e4 | 1047 | { |
1da177e4 | 1048 | struct sk_buff *skb; |
1da177e4 LT |
1049 | int err = 0; |
1050 | ||
1051 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1052 | if (!skb) | |
1053 | return -ENOBUFS; | |
1054 | ||
a56e1953 WC |
1055 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
1056 | RTM_NEWACTION, 0, 0) <= 0) { | |
1057 | kfree_skb(skb); | |
1058 | return -EINVAL; | |
1059 | } | |
10297b99 | 1060 | |
a56e1953 WC |
1061 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
1062 | n->nlmsg_flags & NLM_F_ECHO); | |
1da177e4 LT |
1063 | if (err > 0) |
1064 | err = 0; | |
1065 | return err; | |
1da177e4 LT |
1066 | } |
1067 | ||
5a7a5555 JHS |
1068 | static int tcf_action_add(struct net *net, struct nlattr *nla, |
1069 | struct nlmsghdr *n, u32 portid, int ovr) | |
1da177e4 LT |
1070 | { |
1071 | int ret = 0; | |
33be6271 | 1072 | LIST_HEAD(actions); |
1da177e4 | 1073 | |
9fb9f251 | 1074 | ret = tcf_action_init(net, NULL, nla, NULL, NULL, ovr, 0, &actions); |
33be6271 | 1075 | if (ret) |
f07fed82 | 1076 | return ret; |
1da177e4 | 1077 | |
f07fed82 | 1078 | return tcf_add_notify(net, n, &actions, portid); |
1da177e4 LT |
1079 | } |
1080 | ||
90825b23 JHS |
1081 | static u32 tcaa_root_flags_allowed = TCA_FLAG_LARGE_DUMP_ON; |
1082 | static const struct nla_policy tcaa_policy[TCA_ROOT_MAX + 1] = { | |
1083 | [TCA_ROOT_FLAGS] = { .type = NLA_BITFIELD32, | |
1084 | .validation_data = &tcaa_root_flags_allowed }, | |
e62e484d | 1085 | [TCA_ROOT_TIME_DELTA] = { .type = NLA_U32 }, |
90825b23 JHS |
1086 | }; |
1087 | ||
c21ef3e3 DA |
1088 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, |
1089 | struct netlink_ext_ack *extack) | |
1da177e4 | 1090 | { |
3b1e0a65 | 1091 | struct net *net = sock_net(skb->sk); |
90825b23 | 1092 | struct nlattr *tca[TCA_ROOT_MAX + 1]; |
15e47304 | 1093 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
1da177e4 LT |
1094 | int ret = 0, ovr = 0; |
1095 | ||
0b0f43fe JHS |
1096 | if ((n->nlmsg_type != RTM_GETACTION) && |
1097 | !netlink_capable(skb, CAP_NET_ADMIN)) | |
dfc47ef8 EB |
1098 | return -EPERM; |
1099 | ||
90825b23 | 1100 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ROOT_MAX, NULL, |
c21ef3e3 | 1101 | extack); |
7ba699c6 PM |
1102 | if (ret < 0) |
1103 | return ret; | |
1104 | ||
1105 | if (tca[TCA_ACT_TAB] == NULL) { | |
6ff9c364 | 1106 | pr_notice("tc_ctl_action: received NO action attribs\n"); |
1da177e4 LT |
1107 | return -EINVAL; |
1108 | } | |
1109 | ||
cc7ec456 | 1110 | /* n->nlmsg_flags & NLM_F_CREATE */ |
1da177e4 LT |
1111 | switch (n->nlmsg_type) { |
1112 | case RTM_NEWACTION: | |
1113 | /* we are going to assume all other flags | |
25985edc | 1114 | * imply create only if it doesn't exist |
1da177e4 LT |
1115 | * Note that CREATE | EXCL implies that |
1116 | * but since we want avoid ambiguity (eg when flags | |
1117 | * is zero) then just set this | |
1118 | */ | |
cc7ec456 | 1119 | if (n->nlmsg_flags & NLM_F_REPLACE) |
1da177e4 LT |
1120 | ovr = 1; |
1121 | replay: | |
15e47304 | 1122 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); |
1da177e4 LT |
1123 | if (ret == -EAGAIN) |
1124 | goto replay; | |
1125 | break; | |
1126 | case RTM_DELACTION: | |
7316ae88 | 1127 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
15e47304 | 1128 | portid, RTM_DELACTION); |
1da177e4 LT |
1129 | break; |
1130 | case RTM_GETACTION: | |
7316ae88 | 1131 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
15e47304 | 1132 | portid, RTM_GETACTION); |
1da177e4 LT |
1133 | break; |
1134 | default: | |
1135 | BUG(); | |
1136 | } | |
1137 | ||
1138 | return ret; | |
1139 | } | |
1140 | ||
90825b23 | 1141 | static struct nlattr *find_dump_kind(struct nlattr **nla) |
1da177e4 | 1142 | { |
cc7ec456 | 1143 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
7ba699c6 | 1144 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
7ba699c6 | 1145 | struct nlattr *kind; |
1da177e4 | 1146 | |
7ba699c6 | 1147 | tb1 = nla[TCA_ACT_TAB]; |
1da177e4 LT |
1148 | if (tb1 == NULL) |
1149 | return NULL; | |
1150 | ||
7ba699c6 | 1151 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
fceb6435 | 1152 | NLMSG_ALIGN(nla_len(tb1)), NULL, NULL) < 0) |
1da177e4 | 1153 | return NULL; |
1da177e4 | 1154 | |
6d834e04 PM |
1155 | if (tb[1] == NULL) |
1156 | return NULL; | |
fceb6435 | 1157 | if (nla_parse_nested(tb2, TCA_ACT_MAX, tb[1], NULL, NULL) < 0) |
1da177e4 | 1158 | return NULL; |
7ba699c6 | 1159 | kind = tb2[TCA_ACT_KIND]; |
1da177e4 | 1160 | |
26dab893 | 1161 | return kind; |
1da177e4 LT |
1162 | } |
1163 | ||
5a7a5555 | 1164 | static int tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) |
1da177e4 | 1165 | { |
ddf97ccd | 1166 | struct net *net = sock_net(skb->sk); |
1da177e4 | 1167 | struct nlmsghdr *nlh; |
27a884dc | 1168 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 1169 | struct nlattr *nest; |
1da177e4 | 1170 | struct tc_action_ops *a_o; |
1da177e4 | 1171 | int ret = 0; |
8b00a53c | 1172 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
90825b23 JHS |
1173 | struct nlattr *tb[TCA_ROOT_MAX + 1]; |
1174 | struct nlattr *count_attr = NULL; | |
e62e484d | 1175 | unsigned long jiffy_since = 0; |
90825b23 JHS |
1176 | struct nlattr *kind = NULL; |
1177 | struct nla_bitfield32 bf; | |
e62e484d | 1178 | u32 msecs_since = 0; |
90825b23 JHS |
1179 | u32 act_count = 0; |
1180 | ||
1181 | ret = nlmsg_parse(cb->nlh, sizeof(struct tcamsg), tb, TCA_ROOT_MAX, | |
1182 | tcaa_policy, NULL); | |
1183 | if (ret < 0) | |
1184 | return ret; | |
1da177e4 | 1185 | |
90825b23 | 1186 | kind = find_dump_kind(tb); |
1da177e4 | 1187 | if (kind == NULL) { |
6ff9c364 | 1188 | pr_info("tc_dump_action: action bad kind\n"); |
1da177e4 LT |
1189 | return 0; |
1190 | } | |
1191 | ||
26dab893 | 1192 | a_o = tc_lookup_action(kind); |
cc7ec456 | 1193 | if (a_o == NULL) |
1da177e4 | 1194 | return 0; |
1da177e4 | 1195 | |
90825b23 JHS |
1196 | cb->args[2] = 0; |
1197 | if (tb[TCA_ROOT_FLAGS]) { | |
1198 | bf = nla_get_bitfield32(tb[TCA_ROOT_FLAGS]); | |
1199 | cb->args[2] = bf.value; | |
1200 | } | |
1201 | ||
e62e484d JHS |
1202 | if (tb[TCA_ROOT_TIME_DELTA]) { |
1203 | msecs_since = nla_get_u32(tb[TCA_ROOT_TIME_DELTA]); | |
1204 | } | |
1205 | ||
15e47304 | 1206 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
8b00a53c DM |
1207 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
1208 | if (!nlh) | |
1209 | goto out_module_put; | |
90825b23 | 1210 | |
e62e484d JHS |
1211 | if (msecs_since) |
1212 | jiffy_since = jiffies - msecs_to_jiffies(msecs_since); | |
1213 | ||
8b00a53c | 1214 | t = nlmsg_data(nlh); |
1da177e4 | 1215 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
1216 | t->tca__pad1 = 0; |
1217 | t->tca__pad2 = 0; | |
e62e484d | 1218 | cb->args[3] = jiffy_since; |
90825b23 JHS |
1219 | count_attr = nla_reserve(skb, TCA_ROOT_COUNT, sizeof(u32)); |
1220 | if (!count_attr) | |
1221 | goto out_module_put; | |
1da177e4 | 1222 | |
4b3550ef PM |
1223 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
1224 | if (nest == NULL) | |
8b00a53c | 1225 | goto out_module_put; |
1da177e4 | 1226 | |
a85a970a | 1227 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o); |
1da177e4 | 1228 | if (ret < 0) |
8b00a53c | 1229 | goto out_module_put; |
1da177e4 LT |
1230 | |
1231 | if (ret > 0) { | |
4b3550ef | 1232 | nla_nest_end(skb, nest); |
1da177e4 | 1233 | ret = skb->len; |
90825b23 JHS |
1234 | act_count = cb->args[1]; |
1235 | memcpy(nla_data(count_attr), &act_count, sizeof(u32)); | |
1236 | cb->args[1] = 0; | |
1da177e4 | 1237 | } else |
ebecaa66 | 1238 | nlmsg_trim(skb, b); |
1da177e4 | 1239 | |
27a884dc | 1240 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
15e47304 | 1241 | if (NETLINK_CB(cb->skb).portid && ret) |
1da177e4 LT |
1242 | nlh->nlmsg_flags |= NLM_F_MULTI; |
1243 | module_put(a_o->owner); | |
1244 | return skb->len; | |
1245 | ||
8b00a53c | 1246 | out_module_put: |
1da177e4 | 1247 | module_put(a_o->owner); |
dc5fc579 | 1248 | nlmsg_trim(skb, b); |
1da177e4 LT |
1249 | return skb->len; |
1250 | } | |
1251 | ||
1252 | static int __init tc_action_init(void) | |
1253 | { | |
b97bac64 FW |
1254 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, 0); |
1255 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, 0); | |
c7ac8679 | 1256 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, |
b97bac64 | 1257 | 0); |
1da177e4 | 1258 | |
1da177e4 LT |
1259 | return 0; |
1260 | } | |
1261 | ||
1262 | subsys_initcall(tc_action_init); |