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