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