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 LT |
26 | #include <net/sch_generic.h> |
27 | #include <net/act_api.h> | |
dc5fc579 | 28 | #include <net/netlink.h> |
1da177e4 | 29 | |
519c818e ED |
30 | static void free_tcf(struct rcu_head *head) |
31 | { | |
ec0595cc | 32 | struct tc_action *p = container_of(head, struct tc_action, tcfa_rcu); |
519c818e ED |
33 | |
34 | free_percpu(p->cpu_bstats); | |
35 | free_percpu(p->cpu_qstats); | |
36 | kfree(p); | |
37 | } | |
38 | ||
ec0595cc | 39 | static void tcf_hash_destroy(struct tcf_hashinfo *hinfo, struct tc_action *p) |
e9ce1cd3 | 40 | { |
89819dc0 | 41 | spin_lock_bh(&hinfo->lock); |
ec0595cc | 42 | hlist_del(&p->tcfa_head); |
89819dc0 | 43 | spin_unlock_bh(&hinfo->lock); |
ec0595cc WC |
44 | gen_kill_estimator(&p->tcfa_bstats, |
45 | &p->tcfa_rate_est); | |
89819dc0 | 46 | /* |
ec0595cc | 47 | * gen_estimator est_timer() might access p->tcfa_lock |
89819dc0 WC |
48 | * or bstats, wait a RCU grace period before freeing p |
49 | */ | |
ec0595cc | 50 | call_rcu(&p->tcfa_rcu, free_tcf); |
e9ce1cd3 | 51 | } |
e9ce1cd3 | 52 | |
ec0595cc | 53 | int __tcf_hash_release(struct tc_action *p, bool bind, bool strict) |
e9ce1cd3 DM |
54 | { |
55 | int ret = 0; | |
56 | ||
57 | if (p) { | |
58 | if (bind) | |
ec0595cc WC |
59 | p->tcfa_bindcnt--; |
60 | else if (strict && p->tcfa_bindcnt > 0) | |
55334a5d | 61 | return -EPERM; |
e9ce1cd3 | 62 | |
ec0595cc WC |
63 | p->tcfa_refcnt--; |
64 | if (p->tcfa_bindcnt <= 0 && p->tcfa_refcnt <= 0) { | |
65 | if (p->ops->cleanup) | |
66 | p->ops->cleanup(p, bind); | |
67 | list_del(&p->list); | |
68 | tcf_hash_destroy(p->hinfo, p); | |
1d4150c0 | 69 | ret = ACT_P_DELETED; |
e9ce1cd3 DM |
70 | } |
71 | } | |
28e6b67f | 72 | |
e9ce1cd3 DM |
73 | return ret; |
74 | } | |
28e6b67f | 75 | EXPORT_SYMBOL(__tcf_hash_release); |
e9ce1cd3 | 76 | |
ddf97ccd | 77 | static int tcf_dump_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb, |
a85a970a | 78 | struct netlink_callback *cb) |
e9ce1cd3 | 79 | { |
cc7ec456 | 80 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
4b3550ef | 81 | struct nlattr *nest; |
e9ce1cd3 | 82 | |
89819dc0 | 83 | spin_lock_bh(&hinfo->lock); |
e9ce1cd3 DM |
84 | |
85 | s_i = cb->args[0]; | |
86 | ||
87 | for (i = 0; i < (hinfo->hmask + 1); i++) { | |
a85a970a | 88 | struct hlist_head *head; |
ec0595cc | 89 | struct tc_action *p; |
a85a970a | 90 | |
89819dc0 | 91 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
e9ce1cd3 | 92 | |
ec0595cc | 93 | hlist_for_each_entry_rcu(p, head, tcfa_head) { |
e9ce1cd3 DM |
94 | index++; |
95 | if (index < s_i) | |
96 | continue; | |
4b3550ef | 97 | |
a85a970a | 98 | nest = nla_nest_start(skb, n_i); |
4b3550ef PM |
99 | if (nest == NULL) |
100 | goto nla_put_failure; | |
ec0595cc | 101 | err = tcf_action_dump_1(skb, p, 0, 0); |
e9ce1cd3 DM |
102 | if (err < 0) { |
103 | index--; | |
4b3550ef | 104 | nlmsg_trim(skb, nest); |
e9ce1cd3 DM |
105 | goto done; |
106 | } | |
4b3550ef | 107 | nla_nest_end(skb, nest); |
e9ce1cd3 DM |
108 | n_i++; |
109 | if (n_i >= TCA_ACT_MAX_PRIO) | |
110 | goto done; | |
111 | } | |
112 | } | |
113 | done: | |
89819dc0 | 114 | spin_unlock_bh(&hinfo->lock); |
e9ce1cd3 DM |
115 | if (n_i) |
116 | cb->args[0] += n_i; | |
117 | return n_i; | |
118 | ||
7ba699c6 | 119 | nla_put_failure: |
4b3550ef | 120 | nla_nest_cancel(skb, nest); |
e9ce1cd3 DM |
121 | goto done; |
122 | } | |
123 | ||
ddf97ccd | 124 | static int tcf_del_walker(struct tcf_hashinfo *hinfo, struct sk_buff *skb, |
a85a970a | 125 | const struct tc_action_ops *ops) |
e9ce1cd3 | 126 | { |
4b3550ef | 127 | struct nlattr *nest; |
cc7ec456 | 128 | int i = 0, n_i = 0; |
55334a5d | 129 | int ret = -EINVAL; |
e9ce1cd3 | 130 | |
a85a970a | 131 | nest = nla_nest_start(skb, 0); |
4b3550ef PM |
132 | if (nest == NULL) |
133 | goto nla_put_failure; | |
a85a970a | 134 | if (nla_put_string(skb, TCA_KIND, ops->kind)) |
1b34ec43 | 135 | goto nla_put_failure; |
e9ce1cd3 | 136 | for (i = 0; i < (hinfo->hmask + 1); i++) { |
a85a970a WC |
137 | struct hlist_head *head; |
138 | struct hlist_node *n; | |
ec0595cc | 139 | struct tc_action *p; |
a85a970a | 140 | |
89819dc0 | 141 | head = &hinfo->htab[tcf_hash(i, hinfo->hmask)]; |
ec0595cc WC |
142 | hlist_for_each_entry_safe(p, n, head, tcfa_head) { |
143 | ret = __tcf_hash_release(p, false, true); | |
55334a5d | 144 | if (ret == ACT_P_DELETED) { |
ec0595cc | 145 | module_put(p->ops->owner); |
805c1f4a | 146 | n_i++; |
55334a5d WC |
147 | } else if (ret < 0) |
148 | goto nla_put_failure; | |
e9ce1cd3 DM |
149 | } |
150 | } | |
1b34ec43 DM |
151 | if (nla_put_u32(skb, TCA_FCNT, n_i)) |
152 | goto nla_put_failure; | |
4b3550ef | 153 | nla_nest_end(skb, nest); |
e9ce1cd3 DM |
154 | |
155 | return n_i; | |
7ba699c6 | 156 | nla_put_failure: |
4b3550ef | 157 | nla_nest_cancel(skb, nest); |
55334a5d | 158 | return ret; |
e9ce1cd3 DM |
159 | } |
160 | ||
ddf97ccd WC |
161 | int tcf_generic_walker(struct tc_action_net *tn, struct sk_buff *skb, |
162 | struct netlink_callback *cb, int type, | |
a85a970a | 163 | const struct tc_action_ops *ops) |
e9ce1cd3 | 164 | { |
ddf97ccd WC |
165 | struct tcf_hashinfo *hinfo = tn->hinfo; |
166 | ||
e9ce1cd3 | 167 | if (type == RTM_DELACTION) { |
a85a970a | 168 | return tcf_del_walker(hinfo, skb, ops); |
e9ce1cd3 | 169 | } else if (type == RTM_GETACTION) { |
a85a970a | 170 | return tcf_dump_walker(hinfo, skb, cb); |
e9ce1cd3 | 171 | } else { |
6ff9c364 | 172 | WARN(1, "tcf_generic_walker: unknown action %d\n", type); |
e9ce1cd3 DM |
173 | return -EINVAL; |
174 | } | |
175 | } | |
ddf97ccd | 176 | EXPORT_SYMBOL(tcf_generic_walker); |
e9ce1cd3 | 177 | |
ec0595cc | 178 | static struct tc_action *tcf_hash_lookup(u32 index, struct tcf_hashinfo *hinfo) |
e9ce1cd3 | 179 | { |
ec0595cc | 180 | struct tc_action *p = NULL; |
89819dc0 | 181 | struct hlist_head *head; |
e9ce1cd3 | 182 | |
89819dc0 WC |
183 | spin_lock_bh(&hinfo->lock); |
184 | head = &hinfo->htab[tcf_hash(index, hinfo->hmask)]; | |
ec0595cc WC |
185 | hlist_for_each_entry_rcu(p, head, tcfa_head) |
186 | if (p->tcfa_index == index) | |
e9ce1cd3 | 187 | break; |
89819dc0 | 188 | spin_unlock_bh(&hinfo->lock); |
e9ce1cd3 DM |
189 | |
190 | return p; | |
191 | } | |
e9ce1cd3 | 192 | |
ddf97ccd | 193 | u32 tcf_hash_new_index(struct tc_action_net *tn) |
e9ce1cd3 | 194 | { |
ddf97ccd | 195 | struct tcf_hashinfo *hinfo = tn->hinfo; |
ddafd34f | 196 | u32 val = hinfo->index; |
e9ce1cd3 DM |
197 | |
198 | do { | |
199 | if (++val == 0) | |
200 | val = 1; | |
201 | } while (tcf_hash_lookup(val, hinfo)); | |
202 | ||
ddafd34f | 203 | hinfo->index = val; |
17569fae | 204 | return val; |
e9ce1cd3 DM |
205 | } |
206 | EXPORT_SYMBOL(tcf_hash_new_index); | |
207 | ||
a85a970a | 208 | int tcf_hash_search(struct tc_action_net *tn, struct tc_action **a, u32 index) |
e9ce1cd3 | 209 | { |
ddf97ccd | 210 | struct tcf_hashinfo *hinfo = tn->hinfo; |
ec0595cc | 211 | struct tc_action *p = tcf_hash_lookup(index, hinfo); |
e9ce1cd3 DM |
212 | |
213 | if (p) { | |
ec0595cc | 214 | *a = p; |
e9ce1cd3 DM |
215 | return 1; |
216 | } | |
217 | return 0; | |
218 | } | |
6e6a50c2 | 219 | EXPORT_SYMBOL(tcf_hash_search); |
e9ce1cd3 | 220 | |
a85a970a | 221 | bool tcf_hash_check(struct tc_action_net *tn, u32 index, struct tc_action **a, |
b2313077 | 222 | int bind) |
e9ce1cd3 | 223 | { |
ddf97ccd | 224 | struct tcf_hashinfo *hinfo = tn->hinfo; |
ec0595cc WC |
225 | struct tc_action *p = NULL; |
226 | ||
e9ce1cd3 | 227 | if (index && (p = tcf_hash_lookup(index, hinfo)) != NULL) { |
76aab2c1 | 228 | if (bind) |
ec0595cc WC |
229 | p->tcfa_bindcnt++; |
230 | p->tcfa_refcnt++; | |
231 | *a = p; | |
b2313077 | 232 | return true; |
e9ce1cd3 | 233 | } |
b2313077 | 234 | return false; |
e9ce1cd3 DM |
235 | } |
236 | EXPORT_SYMBOL(tcf_hash_check); | |
237 | ||
86062033 WC |
238 | void tcf_hash_cleanup(struct tc_action *a, struct nlattr *est) |
239 | { | |
86062033 | 240 | if (est) |
ec0595cc WC |
241 | gen_kill_estimator(&a->tcfa_bstats, |
242 | &a->tcfa_rate_est); | |
243 | call_rcu(&a->tcfa_rcu, free_tcf); | |
86062033 WC |
244 | } |
245 | EXPORT_SYMBOL(tcf_hash_cleanup); | |
246 | ||
ddf97ccd | 247 | int tcf_hash_create(struct tc_action_net *tn, u32 index, struct nlattr *est, |
a85a970a WC |
248 | struct tc_action **a, const struct tc_action_ops *ops, |
249 | int bind, bool cpustats) | |
e9ce1cd3 | 250 | { |
ec0595cc | 251 | struct tc_action *p = kzalloc(ops->size, GFP_KERNEL); |
ddf97ccd | 252 | struct tcf_hashinfo *hinfo = tn->hinfo; |
519c818e | 253 | int err = -ENOMEM; |
e9ce1cd3 DM |
254 | |
255 | if (unlikely(!p)) | |
86062033 | 256 | return -ENOMEM; |
ec0595cc | 257 | p->tcfa_refcnt = 1; |
e9ce1cd3 | 258 | if (bind) |
ec0595cc | 259 | p->tcfa_bindcnt = 1; |
e9ce1cd3 | 260 | |
519c818e ED |
261 | if (cpustats) { |
262 | p->cpu_bstats = netdev_alloc_pcpu_stats(struct gnet_stats_basic_cpu); | |
263 | if (!p->cpu_bstats) { | |
264 | err1: | |
265 | kfree(p); | |
266 | return err; | |
267 | } | |
268 | p->cpu_qstats = alloc_percpu(struct gnet_stats_queue); | |
269 | if (!p->cpu_qstats) { | |
270 | err2: | |
271 | free_percpu(p->cpu_bstats); | |
272 | goto err1; | |
273 | } | |
274 | } | |
ec0595cc WC |
275 | spin_lock_init(&p->tcfa_lock); |
276 | INIT_HLIST_NODE(&p->tcfa_head); | |
277 | p->tcfa_index = index ? index : tcf_hash_new_index(tn); | |
278 | p->tcfa_tm.install = jiffies; | |
279 | p->tcfa_tm.lastuse = jiffies; | |
280 | p->tcfa_tm.firstuse = 0; | |
0e991ec6 | 281 | if (est) { |
ec0595cc WC |
282 | err = gen_new_estimator(&p->tcfa_bstats, p->cpu_bstats, |
283 | &p->tcfa_rate_est, | |
284 | &p->tcfa_lock, NULL, est); | |
0e991ec6 | 285 | if (err) { |
519c818e ED |
286 | free_percpu(p->cpu_qstats); |
287 | goto err2; | |
0e991ec6 SH |
288 | } |
289 | } | |
290 | ||
ec0595cc WC |
291 | p->hinfo = hinfo; |
292 | p->ops = ops; | |
293 | INIT_LIST_HEAD(&p->list); | |
294 | *a = p; | |
86062033 | 295 | return 0; |
e9ce1cd3 DM |
296 | } |
297 | EXPORT_SYMBOL(tcf_hash_create); | |
298 | ||
ddf97ccd | 299 | void tcf_hash_insert(struct tc_action_net *tn, struct tc_action *a) |
e9ce1cd3 | 300 | { |
ddf97ccd | 301 | struct tcf_hashinfo *hinfo = tn->hinfo; |
ec0595cc | 302 | unsigned int h = tcf_hash(a->tcfa_index, hinfo->hmask); |
e9ce1cd3 | 303 | |
89819dc0 | 304 | spin_lock_bh(&hinfo->lock); |
ec0595cc | 305 | hlist_add_head(&a->tcfa_head, &hinfo->htab[h]); |
89819dc0 | 306 | spin_unlock_bh(&hinfo->lock); |
e9ce1cd3 DM |
307 | } |
308 | EXPORT_SYMBOL(tcf_hash_insert); | |
1da177e4 | 309 | |
ddf97ccd WC |
310 | void tcf_hashinfo_destroy(const struct tc_action_ops *ops, |
311 | struct tcf_hashinfo *hinfo) | |
1d4150c0 | 312 | { |
1d4150c0 WC |
313 | int i; |
314 | ||
315 | for (i = 0; i < hinfo->hmask + 1; i++) { | |
ec0595cc | 316 | struct tc_action *p; |
1d4150c0 WC |
317 | struct hlist_node *n; |
318 | ||
ec0595cc | 319 | hlist_for_each_entry_safe(p, n, &hinfo->htab[i], tcfa_head) { |
1d4150c0 WC |
320 | int ret; |
321 | ||
ec0595cc | 322 | ret = __tcf_hash_release(p, false, true); |
1d4150c0 WC |
323 | if (ret == ACT_P_DELETED) |
324 | module_put(ops->owner); | |
325 | else if (ret < 0) | |
326 | return; | |
327 | } | |
328 | } | |
329 | kfree(hinfo->htab); | |
330 | } | |
ddf97ccd | 331 | EXPORT_SYMBOL(tcf_hashinfo_destroy); |
1d4150c0 | 332 | |
1f747c26 | 333 | static LIST_HEAD(act_base); |
1da177e4 LT |
334 | static DEFINE_RWLOCK(act_mod_lock); |
335 | ||
ddf97ccd WC |
336 | int tcf_register_action(struct tc_action_ops *act, |
337 | struct pernet_operations *ops) | |
1da177e4 | 338 | { |
1f747c26 | 339 | struct tc_action_ops *a; |
ddf97ccd | 340 | int ret; |
1da177e4 | 341 | |
ddf97ccd | 342 | if (!act->act || !act->dump || !act->init || !act->walk || !act->lookup) |
76c82d7a JHS |
343 | return -EINVAL; |
344 | ||
1da177e4 | 345 | write_lock(&act_mod_lock); |
1f747c26 | 346 | list_for_each_entry(a, &act_base, head) { |
1da177e4 LT |
347 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { |
348 | write_unlock(&act_mod_lock); | |
349 | return -EEXIST; | |
350 | } | |
351 | } | |
1f747c26 | 352 | list_add_tail(&act->head, &act_base); |
1da177e4 | 353 | write_unlock(&act_mod_lock); |
ddf97ccd WC |
354 | |
355 | ret = register_pernet_subsys(ops); | |
356 | if (ret) { | |
357 | tcf_unregister_action(act, ops); | |
358 | return ret; | |
359 | } | |
360 | ||
1da177e4 LT |
361 | return 0; |
362 | } | |
62e3ba1b | 363 | EXPORT_SYMBOL(tcf_register_action); |
1da177e4 | 364 | |
ddf97ccd WC |
365 | int tcf_unregister_action(struct tc_action_ops *act, |
366 | struct pernet_operations *ops) | |
1da177e4 | 367 | { |
1f747c26 | 368 | struct tc_action_ops *a; |
1da177e4 LT |
369 | int err = -ENOENT; |
370 | ||
ddf97ccd WC |
371 | unregister_pernet_subsys(ops); |
372 | ||
1da177e4 | 373 | write_lock(&act_mod_lock); |
a792866a ED |
374 | list_for_each_entry(a, &act_base, head) { |
375 | if (a == act) { | |
376 | list_del(&act->head); | |
377 | err = 0; | |
1da177e4 | 378 | break; |
a792866a | 379 | } |
1da177e4 LT |
380 | } |
381 | write_unlock(&act_mod_lock); | |
382 | return err; | |
383 | } | |
62e3ba1b | 384 | EXPORT_SYMBOL(tcf_unregister_action); |
1da177e4 LT |
385 | |
386 | /* lookup by name */ | |
387 | static struct tc_action_ops *tc_lookup_action_n(char *kind) | |
388 | { | |
a792866a | 389 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
390 | |
391 | if (kind) { | |
392 | read_lock(&act_mod_lock); | |
1f747c26 | 393 | list_for_each_entry(a, &act_base, head) { |
1da177e4 | 394 | if (strcmp(kind, a->kind) == 0) { |
a792866a ED |
395 | if (try_module_get(a->owner)) |
396 | res = a; | |
1da177e4 LT |
397 | break; |
398 | } | |
399 | } | |
400 | read_unlock(&act_mod_lock); | |
401 | } | |
a792866a | 402 | return res; |
1da177e4 LT |
403 | } |
404 | ||
7ba699c6 PM |
405 | /* lookup by nlattr */ |
406 | static struct tc_action_ops *tc_lookup_action(struct nlattr *kind) | |
1da177e4 | 407 | { |
a792866a | 408 | struct tc_action_ops *a, *res = NULL; |
1da177e4 LT |
409 | |
410 | if (kind) { | |
411 | read_lock(&act_mod_lock); | |
1f747c26 | 412 | list_for_each_entry(a, &act_base, head) { |
7ba699c6 | 413 | if (nla_strcmp(kind, a->kind) == 0) { |
a792866a ED |
414 | if (try_module_get(a->owner)) |
415 | res = a; | |
1da177e4 LT |
416 | break; |
417 | } | |
418 | } | |
419 | read_unlock(&act_mod_lock); | |
420 | } | |
a792866a | 421 | return res; |
1da177e4 | 422 | } |
1da177e4 | 423 | |
33be6271 | 424 | int tcf_action_exec(struct sk_buff *skb, const struct list_head *actions, |
10297b99 | 425 | struct tcf_result *res) |
1da177e4 | 426 | { |
dc7f9f6e | 427 | const struct tc_action *a; |
1da177e4 LT |
428 | int ret = -1; |
429 | ||
430 | if (skb->tc_verd & TC_NCLS) { | |
431 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); | |
1da177e4 LT |
432 | ret = TC_ACT_OK; |
433 | goto exec_done; | |
434 | } | |
33be6271 | 435 | list_for_each_entry(a, actions, list) { |
1da177e4 | 436 | repeat: |
63acd680 | 437 | ret = a->ops->act(skb, a, res); |
63acd680 JHS |
438 | if (ret == TC_ACT_REPEAT) |
439 | goto repeat; /* we need a ttl - JHS */ | |
440 | if (ret != TC_ACT_PIPE) | |
441 | goto exec_done; | |
1da177e4 LT |
442 | } |
443 | exec_done: | |
1da177e4 LT |
444 | return ret; |
445 | } | |
62e3ba1b | 446 | EXPORT_SYMBOL(tcf_action_exec); |
1da177e4 | 447 | |
55334a5d | 448 | int tcf_action_destroy(struct list_head *actions, int bind) |
1da177e4 | 449 | { |
33be6271 | 450 | struct tc_action *a, *tmp; |
55334a5d | 451 | int ret = 0; |
1da177e4 | 452 | |
33be6271 | 453 | list_for_each_entry_safe(a, tmp, actions, list) { |
28e6b67f | 454 | ret = __tcf_hash_release(a, bind, true); |
55334a5d | 455 | if (ret == ACT_P_DELETED) |
63acd680 | 456 | module_put(a->ops->owner); |
55334a5d WC |
457 | else if (ret < 0) |
458 | return ret; | |
1da177e4 | 459 | } |
55334a5d | 460 | return ret; |
1da177e4 LT |
461 | } |
462 | ||
463 | int | |
464 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
465 | { | |
1da177e4 LT |
466 | return a->ops->dump(skb, a, bind, ref); |
467 | } | |
468 | ||
469 | int | |
470 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
471 | { | |
472 | int err = -EINVAL; | |
27a884dc | 473 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 474 | struct nlattr *nest; |
1da177e4 | 475 | |
1b34ec43 DM |
476 | if (nla_put_string(skb, TCA_KIND, a->ops->kind)) |
477 | goto nla_put_failure; | |
1da177e4 | 478 | if (tcf_action_copy_stats(skb, a, 0)) |
7ba699c6 | 479 | goto nla_put_failure; |
4b3550ef PM |
480 | nest = nla_nest_start(skb, TCA_OPTIONS); |
481 | if (nest == NULL) | |
482 | goto nla_put_failure; | |
cc7ec456 ED |
483 | err = tcf_action_dump_old(skb, a, bind, ref); |
484 | if (err > 0) { | |
4b3550ef | 485 | nla_nest_end(skb, nest); |
1da177e4 LT |
486 | return err; |
487 | } | |
488 | ||
7ba699c6 | 489 | nla_put_failure: |
dc5fc579 | 490 | nlmsg_trim(skb, b); |
1da177e4 LT |
491 | return -1; |
492 | } | |
62e3ba1b | 493 | EXPORT_SYMBOL(tcf_action_dump_1); |
1da177e4 | 494 | |
0b0f43fe JHS |
495 | int tcf_action_dump(struct sk_buff *skb, struct list_head *actions, |
496 | int bind, int ref) | |
1da177e4 LT |
497 | { |
498 | struct tc_action *a; | |
499 | int err = -EINVAL; | |
4b3550ef | 500 | struct nlattr *nest; |
1da177e4 | 501 | |
33be6271 | 502 | list_for_each_entry(a, actions, list) { |
4b3550ef PM |
503 | nest = nla_nest_start(skb, a->order); |
504 | if (nest == NULL) | |
505 | goto nla_put_failure; | |
1da177e4 LT |
506 | err = tcf_action_dump_1(skb, a, bind, ref); |
507 | if (err < 0) | |
4fe683f5 | 508 | goto errout; |
4b3550ef | 509 | nla_nest_end(skb, nest); |
1da177e4 LT |
510 | } |
511 | ||
512 | return 0; | |
513 | ||
7ba699c6 | 514 | nla_put_failure: |
4fe683f5 TG |
515 | err = -EINVAL; |
516 | errout: | |
4b3550ef | 517 | nla_nest_cancel(skb, nest); |
4fe683f5 | 518 | return err; |
1da177e4 LT |
519 | } |
520 | ||
c1b52739 BL |
521 | struct tc_action *tcf_action_init_1(struct net *net, struct nlattr *nla, |
522 | struct nlattr *est, char *name, int ovr, | |
523 | int bind) | |
1da177e4 LT |
524 | { |
525 | struct tc_action *a; | |
526 | struct tc_action_ops *a_o; | |
527 | char act_name[IFNAMSIZ]; | |
cc7ec456 | 528 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
7ba699c6 | 529 | struct nlattr *kind; |
ab27cfb8 | 530 | int err; |
1da177e4 | 531 | |
1da177e4 | 532 | if (name == NULL) { |
cee63723 PM |
533 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
534 | if (err < 0) | |
1da177e4 | 535 | goto err_out; |
cee63723 | 536 | err = -EINVAL; |
7ba699c6 | 537 | kind = tb[TCA_ACT_KIND]; |
1da177e4 LT |
538 | if (kind == NULL) |
539 | goto err_out; | |
7ba699c6 | 540 | if (nla_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) |
1da177e4 LT |
541 | goto err_out; |
542 | } else { | |
cee63723 | 543 | err = -EINVAL; |
1da177e4 LT |
544 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) |
545 | goto err_out; | |
546 | } | |
547 | ||
548 | a_o = tc_lookup_action_n(act_name); | |
549 | if (a_o == NULL) { | |
95a5afca | 550 | #ifdef CONFIG_MODULES |
1da177e4 | 551 | rtnl_unlock(); |
4bba3925 | 552 | request_module("act_%s", act_name); |
1da177e4 LT |
553 | rtnl_lock(); |
554 | ||
555 | a_o = tc_lookup_action_n(act_name); | |
556 | ||
557 | /* We dropped the RTNL semaphore in order to | |
558 | * perform the module load. So, even if we | |
559 | * succeeded in loading the module we have to | |
560 | * tell the caller to replay the request. We | |
561 | * indicate this using -EAGAIN. | |
562 | */ | |
563 | if (a_o != NULL) { | |
ab27cfb8 | 564 | err = -EAGAIN; |
1da177e4 LT |
565 | goto err_mod; |
566 | } | |
567 | #endif | |
ab27cfb8 | 568 | err = -ENOENT; |
1da177e4 LT |
569 | goto err_out; |
570 | } | |
571 | ||
1da177e4 LT |
572 | /* backward compatibility for policer */ |
573 | if (name == NULL) | |
a85a970a | 574 | err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, ovr, bind); |
1da177e4 | 575 | else |
a85a970a | 576 | err = a_o->init(net, nla, est, &a, ovr, bind); |
ab27cfb8 | 577 | if (err < 0) |
a85a970a | 578 | goto err_mod; |
1da177e4 LT |
579 | |
580 | /* module count goes up only when brand new policy is created | |
cc7ec456 ED |
581 | * if it exists and is only bound to in a_o->init() then |
582 | * ACT_P_CREATED is not returned (a zero is). | |
583 | */ | |
ab27cfb8 | 584 | if (err != ACT_P_CREATED) |
1da177e4 | 585 | module_put(a_o->owner); |
1da177e4 | 586 | |
1da177e4 LT |
587 | return a; |
588 | ||
1da177e4 LT |
589 | err_mod: |
590 | module_put(a_o->owner); | |
591 | err_out: | |
ab27cfb8 | 592 | return ERR_PTR(err); |
1da177e4 LT |
593 | } |
594 | ||
33be6271 | 595 | int tcf_action_init(struct net *net, struct nlattr *nla, |
c1b52739 | 596 | struct nlattr *est, char *name, int ovr, |
33be6271 | 597 | int bind, struct list_head *actions) |
1da177e4 | 598 | { |
cc7ec456 | 599 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 | 600 | struct tc_action *act; |
cee63723 | 601 | int err; |
1da177e4 LT |
602 | int i; |
603 | ||
cee63723 PM |
604 | err = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
605 | if (err < 0) | |
33be6271 | 606 | return err; |
1da177e4 | 607 | |
7ba699c6 | 608 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
c1b52739 | 609 | act = tcf_action_init_1(net, tb[i], est, name, ovr, bind); |
33be6271 WC |
610 | if (IS_ERR(act)) { |
611 | err = PTR_ERR(act); | |
1da177e4 | 612 | goto err; |
33be6271 | 613 | } |
7ba699c6 | 614 | act->order = i; |
33be6271 | 615 | list_add_tail(&act->list, actions); |
1da177e4 | 616 | } |
33be6271 | 617 | return 0; |
1da177e4 LT |
618 | |
619 | err: | |
33be6271 WC |
620 | tcf_action_destroy(actions, bind); |
621 | return err; | |
1da177e4 LT |
622 | } |
623 | ||
ec0595cc | 624 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *p, |
1da177e4 LT |
625 | int compat_mode) |
626 | { | |
627 | int err = 0; | |
628 | struct gnet_dump d; | |
10297b99 | 629 | |
7eb8896d | 630 | if (p == NULL) |
1da177e4 LT |
631 | goto errout; |
632 | ||
633 | /* compat_mode being true specifies a call that is supposed | |
06fe9fb4 | 634 | * to add additional backward compatibility statistic TLVs. |
1da177e4 LT |
635 | */ |
636 | if (compat_mode) { | |
ec0595cc | 637 | if (p->type == TCA_OLD_COMPAT) |
1da177e4 | 638 | err = gnet_stats_start_copy_compat(skb, 0, |
9854518e ND |
639 | TCA_STATS, |
640 | TCA_XSTATS, | |
ec0595cc | 641 | &p->tcfa_lock, &d, |
9854518e | 642 | TCA_PAD); |
1da177e4 LT |
643 | else |
644 | return 0; | |
645 | } else | |
646 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, | |
ec0595cc | 647 | &p->tcfa_lock, &d, TCA_ACT_PAD); |
1da177e4 LT |
648 | |
649 | if (err < 0) | |
650 | goto errout; | |
651 | ||
ec0595cc WC |
652 | if (gnet_stats_copy_basic(NULL, &d, p->cpu_bstats, &p->tcfa_bstats) < 0 || |
653 | gnet_stats_copy_rate_est(&d, &p->tcfa_bstats, | |
654 | &p->tcfa_rate_est) < 0 || | |
519c818e | 655 | gnet_stats_copy_queue(&d, p->cpu_qstats, |
ec0595cc WC |
656 | &p->tcfa_qstats, |
657 | p->tcfa_qstats.qlen) < 0) | |
1da177e4 LT |
658 | goto errout; |
659 | ||
660 | if (gnet_stats_finish_copy(&d) < 0) | |
661 | goto errout; | |
662 | ||
663 | return 0; | |
664 | ||
665 | errout: | |
666 | return -1; | |
667 | } | |
668 | ||
0b0f43fe JHS |
669 | static int tca_get_fill(struct sk_buff *skb, struct list_head *actions, |
670 | u32 portid, u32 seq, u16 flags, int event, int bind, | |
671 | int ref) | |
1da177e4 LT |
672 | { |
673 | struct tcamsg *t; | |
674 | struct nlmsghdr *nlh; | |
27a884dc | 675 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 676 | struct nlattr *nest; |
1da177e4 | 677 | |
15e47304 | 678 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*t), flags); |
8b00a53c DM |
679 | if (!nlh) |
680 | goto out_nlmsg_trim; | |
681 | t = nlmsg_data(nlh); | |
1da177e4 | 682 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
683 | t->tca__pad1 = 0; |
684 | t->tca__pad2 = 0; | |
10297b99 | 685 | |
4b3550ef PM |
686 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
687 | if (nest == NULL) | |
8b00a53c | 688 | goto out_nlmsg_trim; |
1da177e4 | 689 | |
33be6271 | 690 | if (tcf_action_dump(skb, actions, bind, ref) < 0) |
8b00a53c | 691 | goto out_nlmsg_trim; |
1da177e4 | 692 | |
4b3550ef | 693 | nla_nest_end(skb, nest); |
10297b99 | 694 | |
27a884dc | 695 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 LT |
696 | return skb->len; |
697 | ||
8b00a53c | 698 | out_nlmsg_trim: |
dc5fc579 | 699 | nlmsg_trim(skb, b); |
1da177e4 LT |
700 | return -1; |
701 | } | |
702 | ||
703 | static int | |
15e47304 | 704 | act_get_notify(struct net *net, u32 portid, struct nlmsghdr *n, |
33be6271 | 705 | struct list_head *actions, int event) |
1da177e4 LT |
706 | { |
707 | struct sk_buff *skb; | |
1da177e4 LT |
708 | |
709 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
710 | if (!skb) | |
711 | return -ENOBUFS; | |
0b0f43fe JHS |
712 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, event, |
713 | 0, 0) <= 0) { | |
1da177e4 LT |
714 | kfree_skb(skb); |
715 | return -EINVAL; | |
716 | } | |
2942e900 | 717 | |
15e47304 | 718 | return rtnl_unicast(skb, net, portid); |
1da177e4 LT |
719 | } |
720 | ||
ddf97ccd WC |
721 | static struct tc_action *tcf_action_get_1(struct net *net, struct nlattr *nla, |
722 | struct nlmsghdr *n, u32 portid) | |
1da177e4 | 723 | { |
cc7ec456 | 724 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 725 | const struct tc_action_ops *ops; |
1da177e4 LT |
726 | struct tc_action *a; |
727 | int index; | |
ab27cfb8 | 728 | int err; |
1da177e4 | 729 | |
cee63723 PM |
730 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
731 | if (err < 0) | |
ab27cfb8 | 732 | goto err_out; |
1da177e4 | 733 | |
cee63723 | 734 | err = -EINVAL; |
7ba699c6 PM |
735 | if (tb[TCA_ACT_INDEX] == NULL || |
736 | nla_len(tb[TCA_ACT_INDEX]) < sizeof(index)) | |
ab27cfb8 | 737 | goto err_out; |
1587bac4 | 738 | index = nla_get_u32(tb[TCA_ACT_INDEX]); |
1da177e4 | 739 | |
ab27cfb8 | 740 | err = -EINVAL; |
a85a970a WC |
741 | ops = tc_lookup_action(tb[TCA_ACT_KIND]); |
742 | if (!ops) /* could happen in batch of actions */ | |
743 | goto err_out; | |
ab27cfb8 | 744 | err = -ENOENT; |
a85a970a | 745 | if (ops->lookup(net, &a, index) == 0) |
1da177e4 LT |
746 | goto err_mod; |
747 | ||
a85a970a | 748 | module_put(ops->owner); |
1da177e4 | 749 | return a; |
ab27cfb8 | 750 | |
1da177e4 | 751 | err_mod: |
a85a970a | 752 | module_put(ops->owner); |
ab27cfb8 PM |
753 | err_out: |
754 | return ERR_PTR(err); | |
1da177e4 LT |
755 | } |
756 | ||
33be6271 | 757 | static void cleanup_a(struct list_head *actions) |
1da177e4 | 758 | { |
33be6271 | 759 | struct tc_action *a, *tmp; |
1da177e4 | 760 | |
33be6271 WC |
761 | list_for_each_entry_safe(a, tmp, actions, list) { |
762 | list_del(&a->list); | |
1da177e4 LT |
763 | kfree(a); |
764 | } | |
765 | } | |
766 | ||
7316ae88 | 767 | static int tca_action_flush(struct net *net, struct nlattr *nla, |
15e47304 | 768 | struct nlmsghdr *n, u32 portid) |
1da177e4 LT |
769 | { |
770 | struct sk_buff *skb; | |
771 | unsigned char *b; | |
772 | struct nlmsghdr *nlh; | |
773 | struct tcamsg *t; | |
774 | struct netlink_callback dcb; | |
4b3550ef | 775 | struct nlattr *nest; |
cc7ec456 | 776 | struct nlattr *tb[TCA_ACT_MAX + 1]; |
a85a970a | 777 | const struct tc_action_ops *ops; |
7ba699c6 | 778 | struct nlattr *kind; |
36723873 | 779 | int err = -ENOMEM; |
1da177e4 | 780 | |
1da177e4 LT |
781 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
782 | if (!skb) { | |
6ff9c364 | 783 | pr_debug("tca_action_flush: failed skb alloc\n"); |
36723873 | 784 | return err; |
1da177e4 LT |
785 | } |
786 | ||
27a884dc | 787 | b = skb_tail_pointer(skb); |
1da177e4 | 788 | |
cee63723 PM |
789 | err = nla_parse_nested(tb, TCA_ACT_MAX, nla, NULL); |
790 | if (err < 0) | |
1da177e4 LT |
791 | goto err_out; |
792 | ||
cee63723 | 793 | err = -EINVAL; |
7ba699c6 | 794 | kind = tb[TCA_ACT_KIND]; |
a85a970a WC |
795 | ops = tc_lookup_action(kind); |
796 | if (!ops) /*some idjot trying to flush unknown action */ | |
1da177e4 LT |
797 | goto err_out; |
798 | ||
0b0f43fe JHS |
799 | nlh = nlmsg_put(skb, portid, n->nlmsg_seq, RTM_DELACTION, |
800 | sizeof(*t), 0); | |
8b00a53c DM |
801 | if (!nlh) |
802 | goto out_module_put; | |
803 | t = nlmsg_data(nlh); | |
1da177e4 | 804 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
805 | t->tca__pad1 = 0; |
806 | t->tca__pad2 = 0; | |
1da177e4 | 807 | |
4b3550ef PM |
808 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
809 | if (nest == NULL) | |
8b00a53c | 810 | goto out_module_put; |
1da177e4 | 811 | |
a85a970a | 812 | err = ops->walk(net, skb, &dcb, RTM_DELACTION, ops); |
1da177e4 | 813 | if (err < 0) |
8b00a53c | 814 | goto out_module_put; |
f97017cd JHS |
815 | if (err == 0) |
816 | goto noflush_out; | |
1da177e4 | 817 | |
4b3550ef | 818 | nla_nest_end(skb, nest); |
1da177e4 | 819 | |
27a884dc | 820 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
1da177e4 | 821 | nlh->nlmsg_flags |= NLM_F_ROOT; |
a85a970a | 822 | module_put(ops->owner); |
15e47304 | 823 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
cc7ec456 | 824 | n->nlmsg_flags & NLM_F_ECHO); |
1da177e4 LT |
825 | if (err > 0) |
826 | return 0; | |
827 | ||
828 | return err; | |
829 | ||
8b00a53c | 830 | out_module_put: |
a85a970a | 831 | module_put(ops->owner); |
1da177e4 | 832 | err_out: |
f97017cd | 833 | noflush_out: |
1da177e4 | 834 | kfree_skb(skb); |
1da177e4 LT |
835 | return err; |
836 | } | |
837 | ||
a56e1953 WC |
838 | static int |
839 | tcf_del_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, | |
840 | u32 portid) | |
841 | { | |
842 | int ret; | |
843 | struct sk_buff *skb; | |
844 | ||
845 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
846 | if (!skb) | |
847 | return -ENOBUFS; | |
848 | ||
849 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, 0, RTM_DELACTION, | |
850 | 0, 1) <= 0) { | |
851 | kfree_skb(skb); | |
852 | return -EINVAL; | |
853 | } | |
854 | ||
855 | /* now do the delete */ | |
55334a5d WC |
856 | ret = tcf_action_destroy(actions, 0); |
857 | if (ret < 0) { | |
858 | kfree_skb(skb); | |
859 | return ret; | |
860 | } | |
a56e1953 WC |
861 | |
862 | ret = rtnetlink_send(skb, net, portid, RTNLGRP_TC, | |
863 | n->nlmsg_flags & NLM_F_ECHO); | |
864 | if (ret > 0) | |
865 | return 0; | |
866 | return ret; | |
867 | } | |
868 | ||
1da177e4 | 869 | static int |
7316ae88 | 870 | tca_action_gd(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
15e47304 | 871 | u32 portid, int event) |
1da177e4 | 872 | { |
cee63723 | 873 | int i, ret; |
cc7ec456 | 874 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
33be6271 WC |
875 | struct tc_action *act; |
876 | LIST_HEAD(actions); | |
1da177e4 | 877 | |
cee63723 PM |
878 | ret = nla_parse_nested(tb, TCA_ACT_MAX_PRIO, nla, NULL); |
879 | if (ret < 0) | |
880 | return ret; | |
1da177e4 | 881 | |
cc7ec456 | 882 | if (event == RTM_DELACTION && n->nlmsg_flags & NLM_F_ROOT) { |
f97017cd | 883 | if (tb[1] != NULL) |
15e47304 | 884 | return tca_action_flush(net, tb[1], n, portid); |
f97017cd JHS |
885 | else |
886 | return -EINVAL; | |
1da177e4 LT |
887 | } |
888 | ||
7ba699c6 | 889 | for (i = 1; i <= TCA_ACT_MAX_PRIO && tb[i]; i++) { |
ddf97ccd | 890 | act = tcf_action_get_1(net, tb[i], n, portid); |
ab27cfb8 PM |
891 | if (IS_ERR(act)) { |
892 | ret = PTR_ERR(act); | |
1da177e4 | 893 | goto err; |
ab27cfb8 | 894 | } |
7ba699c6 | 895 | act->order = i; |
33be6271 | 896 | list_add_tail(&act->list, &actions); |
1da177e4 LT |
897 | } |
898 | ||
899 | if (event == RTM_GETACTION) | |
33be6271 | 900 | ret = act_get_notify(net, portid, n, &actions, event); |
1da177e4 | 901 | else { /* delete */ |
a56e1953 WC |
902 | ret = tcf_del_notify(net, n, &actions, portid); |
903 | if (ret) | |
1da177e4 | 904 | goto err; |
1da177e4 LT |
905 | return ret; |
906 | } | |
907 | err: | |
33be6271 | 908 | cleanup_a(&actions); |
1da177e4 LT |
909 | return ret; |
910 | } | |
911 | ||
a56e1953 WC |
912 | static int |
913 | tcf_add_notify(struct net *net, struct nlmsghdr *n, struct list_head *actions, | |
914 | u32 portid) | |
1da177e4 | 915 | { |
1da177e4 | 916 | struct sk_buff *skb; |
1da177e4 LT |
917 | int err = 0; |
918 | ||
919 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
920 | if (!skb) | |
921 | return -ENOBUFS; | |
922 | ||
a56e1953 WC |
923 | if (tca_get_fill(skb, actions, portid, n->nlmsg_seq, n->nlmsg_flags, |
924 | RTM_NEWACTION, 0, 0) <= 0) { | |
925 | kfree_skb(skb); | |
926 | return -EINVAL; | |
927 | } | |
10297b99 | 928 | |
a56e1953 WC |
929 | err = rtnetlink_send(skb, net, portid, RTNLGRP_TC, |
930 | n->nlmsg_flags & NLM_F_ECHO); | |
1da177e4 LT |
931 | if (err > 0) |
932 | err = 0; | |
933 | return err; | |
1da177e4 LT |
934 | } |
935 | ||
1da177e4 | 936 | static int |
7316ae88 | 937 | tcf_action_add(struct net *net, struct nlattr *nla, struct nlmsghdr *n, |
15e47304 | 938 | u32 portid, int ovr) |
1da177e4 LT |
939 | { |
940 | int ret = 0; | |
33be6271 | 941 | LIST_HEAD(actions); |
1da177e4 | 942 | |
33be6271 WC |
943 | ret = tcf_action_init(net, nla, NULL, NULL, ovr, 0, &actions); |
944 | if (ret) | |
ab27cfb8 | 945 | goto done; |
1da177e4 LT |
946 | |
947 | /* dump then free all the actions after update; inserted policy | |
948 | * stays intact | |
cc7ec456 | 949 | */ |
a56e1953 | 950 | ret = tcf_add_notify(net, n, &actions, portid); |
33be6271 | 951 | cleanup_a(&actions); |
1da177e4 LT |
952 | done: |
953 | return ret; | |
954 | } | |
955 | ||
661d2967 | 956 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n) |
1da177e4 | 957 | { |
3b1e0a65 | 958 | struct net *net = sock_net(skb->sk); |
7ba699c6 | 959 | struct nlattr *tca[TCA_ACT_MAX + 1]; |
15e47304 | 960 | u32 portid = skb ? NETLINK_CB(skb).portid : 0; |
1da177e4 LT |
961 | int ret = 0, ovr = 0; |
962 | ||
0b0f43fe JHS |
963 | if ((n->nlmsg_type != RTM_GETACTION) && |
964 | !netlink_capable(skb, CAP_NET_ADMIN)) | |
dfc47ef8 EB |
965 | return -EPERM; |
966 | ||
7ba699c6 PM |
967 | ret = nlmsg_parse(n, sizeof(struct tcamsg), tca, TCA_ACT_MAX, NULL); |
968 | if (ret < 0) | |
969 | return ret; | |
970 | ||
971 | if (tca[TCA_ACT_TAB] == NULL) { | |
6ff9c364 | 972 | pr_notice("tc_ctl_action: received NO action attribs\n"); |
1da177e4 LT |
973 | return -EINVAL; |
974 | } | |
975 | ||
cc7ec456 | 976 | /* n->nlmsg_flags & NLM_F_CREATE */ |
1da177e4 LT |
977 | switch (n->nlmsg_type) { |
978 | case RTM_NEWACTION: | |
979 | /* we are going to assume all other flags | |
25985edc | 980 | * imply create only if it doesn't exist |
1da177e4 LT |
981 | * Note that CREATE | EXCL implies that |
982 | * but since we want avoid ambiguity (eg when flags | |
983 | * is zero) then just set this | |
984 | */ | |
cc7ec456 | 985 | if (n->nlmsg_flags & NLM_F_REPLACE) |
1da177e4 LT |
986 | ovr = 1; |
987 | replay: | |
15e47304 | 988 | ret = tcf_action_add(net, tca[TCA_ACT_TAB], n, portid, ovr); |
1da177e4 LT |
989 | if (ret == -EAGAIN) |
990 | goto replay; | |
991 | break; | |
992 | case RTM_DELACTION: | |
7316ae88 | 993 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
15e47304 | 994 | portid, RTM_DELACTION); |
1da177e4 LT |
995 | break; |
996 | case RTM_GETACTION: | |
7316ae88 | 997 | ret = tca_action_gd(net, tca[TCA_ACT_TAB], n, |
15e47304 | 998 | portid, RTM_GETACTION); |
1da177e4 LT |
999 | break; |
1000 | default: | |
1001 | BUG(); | |
1002 | } | |
1003 | ||
1004 | return ret; | |
1005 | } | |
1006 | ||
7ba699c6 | 1007 | static struct nlattr * |
3a6c2b41 | 1008 | find_dump_kind(const struct nlmsghdr *n) |
1da177e4 | 1009 | { |
cc7ec456 | 1010 | struct nlattr *tb1, *tb2[TCA_ACT_MAX + 1]; |
7ba699c6 PM |
1011 | struct nlattr *tb[TCA_ACT_MAX_PRIO + 1]; |
1012 | struct nlattr *nla[TCAA_MAX + 1]; | |
1013 | struct nlattr *kind; | |
1da177e4 | 1014 | |
c96c9471 | 1015 | if (nlmsg_parse(n, sizeof(struct tcamsg), nla, TCAA_MAX, NULL) < 0) |
1da177e4 | 1016 | return NULL; |
7ba699c6 | 1017 | tb1 = nla[TCA_ACT_TAB]; |
1da177e4 LT |
1018 | if (tb1 == NULL) |
1019 | return NULL; | |
1020 | ||
7ba699c6 PM |
1021 | if (nla_parse(tb, TCA_ACT_MAX_PRIO, nla_data(tb1), |
1022 | NLMSG_ALIGN(nla_len(tb1)), NULL) < 0) | |
1da177e4 | 1023 | return NULL; |
1da177e4 | 1024 | |
6d834e04 PM |
1025 | if (tb[1] == NULL) |
1026 | return NULL; | |
1027 | if (nla_parse(tb2, TCA_ACT_MAX, nla_data(tb[1]), | |
1028 | nla_len(tb[1]), NULL) < 0) | |
1da177e4 | 1029 | return NULL; |
7ba699c6 | 1030 | kind = tb2[TCA_ACT_KIND]; |
1da177e4 | 1031 | |
26dab893 | 1032 | return kind; |
1da177e4 LT |
1033 | } |
1034 | ||
1035 | static int | |
1036 | tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) | |
1037 | { | |
ddf97ccd | 1038 | struct net *net = sock_net(skb->sk); |
1da177e4 | 1039 | struct nlmsghdr *nlh; |
27a884dc | 1040 | unsigned char *b = skb_tail_pointer(skb); |
4b3550ef | 1041 | struct nlattr *nest; |
1da177e4 | 1042 | struct tc_action_ops *a_o; |
1da177e4 | 1043 | int ret = 0; |
8b00a53c | 1044 | struct tcamsg *t = (struct tcamsg *) nlmsg_data(cb->nlh); |
7ba699c6 | 1045 | struct nlattr *kind = find_dump_kind(cb->nlh); |
1da177e4 LT |
1046 | |
1047 | if (kind == NULL) { | |
6ff9c364 | 1048 | pr_info("tc_dump_action: action bad kind\n"); |
1da177e4 LT |
1049 | return 0; |
1050 | } | |
1051 | ||
26dab893 | 1052 | a_o = tc_lookup_action(kind); |
cc7ec456 | 1053 | if (a_o == NULL) |
1da177e4 | 1054 | return 0; |
1da177e4 | 1055 | |
15e47304 | 1056 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
8b00a53c DM |
1057 | cb->nlh->nlmsg_type, sizeof(*t), 0); |
1058 | if (!nlh) | |
1059 | goto out_module_put; | |
1060 | t = nlmsg_data(nlh); | |
1da177e4 | 1061 | t->tca_family = AF_UNSPEC; |
9ef1d4c7 PM |
1062 | t->tca__pad1 = 0; |
1063 | t->tca__pad2 = 0; | |
1da177e4 | 1064 | |
4b3550ef PM |
1065 | nest = nla_nest_start(skb, TCA_ACT_TAB); |
1066 | if (nest == NULL) | |
8b00a53c | 1067 | goto out_module_put; |
1da177e4 | 1068 | |
a85a970a | 1069 | ret = a_o->walk(net, skb, cb, RTM_GETACTION, a_o); |
1da177e4 | 1070 | if (ret < 0) |
8b00a53c | 1071 | goto out_module_put; |
1da177e4 LT |
1072 | |
1073 | if (ret > 0) { | |
4b3550ef | 1074 | nla_nest_end(skb, nest); |
1da177e4 LT |
1075 | ret = skb->len; |
1076 | } else | |
ebecaa66 | 1077 | nlmsg_trim(skb, b); |
1da177e4 | 1078 | |
27a884dc | 1079 | nlh->nlmsg_len = skb_tail_pointer(skb) - b; |
15e47304 | 1080 | if (NETLINK_CB(cb->skb).portid && ret) |
1da177e4 LT |
1081 | nlh->nlmsg_flags |= NLM_F_MULTI; |
1082 | module_put(a_o->owner); | |
1083 | return skb->len; | |
1084 | ||
8b00a53c | 1085 | out_module_put: |
1da177e4 | 1086 | module_put(a_o->owner); |
dc5fc579 | 1087 | nlmsg_trim(skb, b); |
1da177e4 LT |
1088 | return skb->len; |
1089 | } | |
1090 | ||
1091 | static int __init tc_action_init(void) | |
1092 | { | |
c7ac8679 GR |
1093 | rtnl_register(PF_UNSPEC, RTM_NEWACTION, tc_ctl_action, NULL, NULL); |
1094 | rtnl_register(PF_UNSPEC, RTM_DELACTION, tc_ctl_action, NULL, NULL); | |
1095 | rtnl_register(PF_UNSPEC, RTM_GETACTION, tc_ctl_action, tc_dump_action, | |
1096 | NULL); | |
1da177e4 | 1097 | |
1da177e4 LT |
1098 | return 0; |
1099 | } | |
1100 | ||
1101 | subsys_initcall(tc_action_init); |