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