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