]>
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 | ||
14 | #include <asm/uaccess.h> | |
15 | #include <asm/system.h> | |
16 | #include <linux/bitops.h> | |
1da177e4 LT |
17 | #include <linux/types.h> |
18 | #include <linux/kernel.h> | |
19 | #include <linux/sched.h> | |
20 | #include <linux/string.h> | |
21 | #include <linux/mm.h> | |
22 | #include <linux/socket.h> | |
23 | #include <linux/sockios.h> | |
24 | #include <linux/in.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/netdevice.h> | |
28 | #include <linux/skbuff.h> | |
29 | #include <linux/rtnetlink.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/kmod.h> | |
32 | #include <net/sock.h> | |
33 | #include <net/sch_generic.h> | |
34 | #include <net/act_api.h> | |
35 | ||
2edc2689 | 36 | #if 0 /* control */ |
1da177e4 LT |
37 | #define DPRINTK(format, args...) printk(KERN_DEBUG format, ##args) |
38 | #else | |
39 | #define DPRINTK(format, args...) | |
40 | #endif | |
41 | #if 0 /* data */ | |
42 | #define D2PRINTK(format, args...) printk(KERN_DEBUG format, ##args) | |
43 | #else | |
44 | #define D2PRINTK(format, args...) | |
45 | #endif | |
46 | ||
47 | static struct tc_action_ops *act_base = NULL; | |
48 | static DEFINE_RWLOCK(act_mod_lock); | |
49 | ||
50 | int tcf_register_action(struct tc_action_ops *act) | |
51 | { | |
52 | struct tc_action_ops *a, **ap; | |
53 | ||
54 | write_lock(&act_mod_lock); | |
55 | for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) { | |
56 | if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { | |
57 | write_unlock(&act_mod_lock); | |
58 | return -EEXIST; | |
59 | } | |
60 | } | |
61 | act->next = NULL; | |
62 | *ap = act; | |
63 | write_unlock(&act_mod_lock); | |
64 | return 0; | |
65 | } | |
66 | ||
67 | int tcf_unregister_action(struct tc_action_ops *act) | |
68 | { | |
69 | struct tc_action_ops *a, **ap; | |
70 | int err = -ENOENT; | |
71 | ||
72 | write_lock(&act_mod_lock); | |
73 | for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) | |
74 | if (a == act) | |
75 | break; | |
76 | if (a) { | |
77 | *ap = a->next; | |
78 | a->next = NULL; | |
79 | err = 0; | |
80 | } | |
81 | write_unlock(&act_mod_lock); | |
82 | return err; | |
83 | } | |
84 | ||
85 | /* lookup by name */ | |
86 | static struct tc_action_ops *tc_lookup_action_n(char *kind) | |
87 | { | |
88 | struct tc_action_ops *a = NULL; | |
89 | ||
90 | if (kind) { | |
91 | read_lock(&act_mod_lock); | |
92 | for (a = act_base; a; a = a->next) { | |
93 | if (strcmp(kind, a->kind) == 0) { | |
94 | if (!try_module_get(a->owner)) { | |
95 | read_unlock(&act_mod_lock); | |
96 | return NULL; | |
97 | } | |
98 | break; | |
99 | } | |
100 | } | |
101 | read_unlock(&act_mod_lock); | |
102 | } | |
103 | return a; | |
104 | } | |
105 | ||
106 | /* lookup by rtattr */ | |
107 | static struct tc_action_ops *tc_lookup_action(struct rtattr *kind) | |
108 | { | |
109 | struct tc_action_ops *a = NULL; | |
110 | ||
111 | if (kind) { | |
112 | read_lock(&act_mod_lock); | |
113 | for (a = act_base; a; a = a->next) { | |
114 | if (rtattr_strcmp(kind, a->kind) == 0) { | |
115 | if (!try_module_get(a->owner)) { | |
116 | read_unlock(&act_mod_lock); | |
117 | return NULL; | |
118 | } | |
119 | break; | |
120 | } | |
121 | } | |
122 | read_unlock(&act_mod_lock); | |
123 | } | |
124 | return a; | |
125 | } | |
126 | ||
127 | #if 0 | |
128 | /* lookup by id */ | |
129 | static struct tc_action_ops *tc_lookup_action_id(u32 type) | |
130 | { | |
131 | struct tc_action_ops *a = NULL; | |
132 | ||
133 | if (type) { | |
134 | read_lock(&act_mod_lock); | |
135 | for (a = act_base; a; a = a->next) { | |
136 | if (a->type == type) { | |
137 | if (!try_module_get(a->owner)) { | |
138 | read_unlock(&act_mod_lock); | |
139 | return NULL; | |
140 | } | |
141 | break; | |
142 | } | |
143 | } | |
144 | read_unlock(&act_mod_lock); | |
145 | } | |
146 | return a; | |
147 | } | |
148 | #endif | |
149 | ||
150 | int tcf_action_exec(struct sk_buff *skb, struct tc_action *act, | |
151 | struct tcf_result *res) | |
152 | { | |
153 | struct tc_action *a; | |
154 | int ret = -1; | |
155 | ||
156 | if (skb->tc_verd & TC_NCLS) { | |
157 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); | |
158 | D2PRINTK("(%p)tcf_action_exec: cleared TC_NCLS in %s out %s\n", | |
159 | skb, skb->input_dev ? skb->input_dev->name : "xxx", | |
160 | skb->dev->name); | |
161 | ret = TC_ACT_OK; | |
162 | goto exec_done; | |
163 | } | |
164 | while ((a = act) != NULL) { | |
165 | repeat: | |
166 | if (a->ops && a->ops->act) { | |
f43c5a0d | 167 | ret = a->ops->act(skb, a, res); |
1da177e4 LT |
168 | if (TC_MUNGED & skb->tc_verd) { |
169 | /* copied already, allow trampling */ | |
170 | skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd); | |
171 | skb->tc_verd = CLR_TC_MUNGED(skb->tc_verd); | |
172 | } | |
1da177e4 LT |
173 | if (ret == TC_ACT_REPEAT) |
174 | goto repeat; /* we need a ttl - JHS */ | |
14d50e78 HS |
175 | if (ret != TC_ACT_PIPE) |
176 | goto exec_done; | |
1da177e4 LT |
177 | } |
178 | act = a->next; | |
179 | } | |
180 | exec_done: | |
1da177e4 LT |
181 | return ret; |
182 | } | |
183 | ||
184 | void tcf_action_destroy(struct tc_action *act, int bind) | |
185 | { | |
186 | struct tc_action *a; | |
187 | ||
188 | for (a = act; a; a = act) { | |
189 | if (a->ops && a->ops->cleanup) { | |
190 | DPRINTK("tcf_action_destroy destroying %p next %p\n", | |
191 | a, a->next); | |
192 | if (a->ops->cleanup(a, bind) == ACT_P_DELETED) | |
193 | module_put(a->ops->owner); | |
194 | act = act->next; | |
195 | kfree(a); | |
196 | } else { /*FIXME: Remove later - catch insertion bugs*/ | |
197 | printk("tcf_action_destroy: BUG? destroying NULL ops\n"); | |
198 | act = act->next; | |
199 | kfree(a); | |
200 | } | |
201 | } | |
202 | } | |
203 | ||
204 | int | |
205 | tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
206 | { | |
207 | int err = -EINVAL; | |
208 | ||
209 | if (a->ops == NULL || a->ops->dump == NULL) | |
210 | return err; | |
211 | return a->ops->dump(skb, a, bind, ref); | |
212 | } | |
213 | ||
214 | int | |
215 | tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref) | |
216 | { | |
217 | int err = -EINVAL; | |
218 | unsigned char *b = skb->tail; | |
219 | struct rtattr *r; | |
220 | ||
221 | if (a->ops == NULL || a->ops->dump == NULL) | |
222 | return err; | |
223 | ||
224 | RTA_PUT(skb, TCA_KIND, IFNAMSIZ, a->ops->kind); | |
225 | if (tcf_action_copy_stats(skb, a, 0)) | |
226 | goto rtattr_failure; | |
227 | r = (struct rtattr*) skb->tail; | |
228 | RTA_PUT(skb, TCA_OPTIONS, 0, NULL); | |
229 | if ((err = tcf_action_dump_old(skb, a, bind, ref)) > 0) { | |
230 | r->rta_len = skb->tail - (u8*)r; | |
231 | return err; | |
232 | } | |
233 | ||
234 | rtattr_failure: | |
235 | skb_trim(skb, b - skb->data); | |
236 | return -1; | |
237 | } | |
238 | ||
239 | int | |
240 | tcf_action_dump(struct sk_buff *skb, struct tc_action *act, int bind, int ref) | |
241 | { | |
242 | struct tc_action *a; | |
243 | int err = -EINVAL; | |
244 | unsigned char *b = skb->tail; | |
245 | struct rtattr *r ; | |
246 | ||
247 | while ((a = act) != NULL) { | |
248 | r = (struct rtattr*) skb->tail; | |
249 | act = a->next; | |
250 | RTA_PUT(skb, a->order, 0, NULL); | |
251 | err = tcf_action_dump_1(skb, a, bind, ref); | |
252 | if (err < 0) | |
253 | goto rtattr_failure; | |
254 | r->rta_len = skb->tail - (u8*)r; | |
255 | } | |
256 | ||
257 | return 0; | |
258 | ||
259 | rtattr_failure: | |
260 | skb_trim(skb, b - skb->data); | |
261 | return -err; | |
262 | } | |
263 | ||
264 | struct tc_action *tcf_action_init_1(struct rtattr *rta, struct rtattr *est, | |
265 | char *name, int ovr, int bind, int *err) | |
266 | { | |
267 | struct tc_action *a; | |
268 | struct tc_action_ops *a_o; | |
269 | char act_name[IFNAMSIZ]; | |
270 | struct rtattr *tb[TCA_ACT_MAX+1]; | |
271 | struct rtattr *kind; | |
272 | ||
273 | *err = -EINVAL; | |
274 | ||
275 | if (name == NULL) { | |
276 | if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0) | |
277 | goto err_out; | |
278 | kind = tb[TCA_ACT_KIND-1]; | |
279 | if (kind == NULL) | |
280 | goto err_out; | |
281 | if (rtattr_strlcpy(act_name, kind, IFNAMSIZ) >= IFNAMSIZ) | |
282 | goto err_out; | |
283 | } else { | |
284 | if (strlcpy(act_name, name, IFNAMSIZ) >= IFNAMSIZ) | |
285 | goto err_out; | |
286 | } | |
287 | ||
288 | a_o = tc_lookup_action_n(act_name); | |
289 | if (a_o == NULL) { | |
290 | #ifdef CONFIG_KMOD | |
291 | rtnl_unlock(); | |
4bba3925 | 292 | request_module("act_%s", act_name); |
1da177e4 LT |
293 | rtnl_lock(); |
294 | ||
295 | a_o = tc_lookup_action_n(act_name); | |
296 | ||
297 | /* We dropped the RTNL semaphore in order to | |
298 | * perform the module load. So, even if we | |
299 | * succeeded in loading the module we have to | |
300 | * tell the caller to replay the request. We | |
301 | * indicate this using -EAGAIN. | |
302 | */ | |
303 | if (a_o != NULL) { | |
304 | *err = -EAGAIN; | |
305 | goto err_mod; | |
306 | } | |
307 | #endif | |
308 | goto err_out; | |
309 | } | |
310 | ||
311 | *err = -ENOMEM; | |
312 | a = kmalloc(sizeof(*a), GFP_KERNEL); | |
313 | if (a == NULL) | |
314 | goto err_mod; | |
315 | memset(a, 0, sizeof(*a)); | |
316 | ||
317 | /* backward compatibility for policer */ | |
318 | if (name == NULL) | |
319 | *err = a_o->init(tb[TCA_ACT_OPTIONS-1], est, a, ovr, bind); | |
320 | else | |
321 | *err = a_o->init(rta, est, a, ovr, bind); | |
322 | if (*err < 0) | |
323 | goto err_free; | |
324 | ||
325 | /* module count goes up only when brand new policy is created | |
326 | if it exists and is only bound to in a_o->init() then | |
327 | ACT_P_CREATED is not returned (a zero is). | |
328 | */ | |
329 | if (*err != ACT_P_CREATED) | |
330 | module_put(a_o->owner); | |
331 | a->ops = a_o; | |
332 | DPRINTK("tcf_action_init_1: successfull %s\n", act_name); | |
333 | ||
334 | *err = 0; | |
335 | return a; | |
336 | ||
337 | err_free: | |
338 | kfree(a); | |
339 | err_mod: | |
340 | module_put(a_o->owner); | |
341 | err_out: | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | struct tc_action *tcf_action_init(struct rtattr *rta, struct rtattr *est, | |
346 | char *name, int ovr, int bind, int *err) | |
347 | { | |
348 | struct rtattr *tb[TCA_ACT_MAX_PRIO+1]; | |
349 | struct tc_action *head = NULL, *act, *act_prev = NULL; | |
350 | int i; | |
351 | ||
352 | if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) { | |
353 | *err = -EINVAL; | |
354 | return head; | |
355 | } | |
356 | ||
357 | for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) { | |
358 | act = tcf_action_init_1(tb[i], est, name, ovr, bind, err); | |
359 | if (act == NULL) | |
360 | goto err; | |
361 | act->order = i+1; | |
362 | ||
363 | if (head == NULL) | |
364 | head = act; | |
365 | else | |
366 | act_prev->next = act; | |
367 | act_prev = act; | |
368 | } | |
369 | return head; | |
370 | ||
371 | err: | |
372 | if (head != NULL) | |
373 | tcf_action_destroy(head, bind); | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | int tcf_action_copy_stats(struct sk_buff *skb, struct tc_action *a, | |
378 | int compat_mode) | |
379 | { | |
380 | int err = 0; | |
381 | struct gnet_dump d; | |
382 | struct tcf_act_hdr *h = a->priv; | |
383 | ||
384 | if (h == NULL) | |
385 | goto errout; | |
386 | ||
387 | /* compat_mode being true specifies a call that is supposed | |
388 | * to add additional backward compatiblity statistic TLVs. | |
389 | */ | |
390 | if (compat_mode) { | |
391 | if (a->type == TCA_OLD_COMPAT) | |
392 | err = gnet_stats_start_copy_compat(skb, 0, | |
393 | TCA_STATS, TCA_XSTATS, h->stats_lock, &d); | |
394 | else | |
395 | return 0; | |
396 | } else | |
397 | err = gnet_stats_start_copy(skb, TCA_ACT_STATS, | |
398 | h->stats_lock, &d); | |
399 | ||
400 | if (err < 0) | |
401 | goto errout; | |
402 | ||
403 | if (a->ops != NULL && a->ops->get_stats != NULL) | |
404 | if (a->ops->get_stats(skb, a) < 0) | |
405 | goto errout; | |
406 | ||
407 | if (gnet_stats_copy_basic(&d, &h->bstats) < 0 || | |
408 | #ifdef CONFIG_NET_ESTIMATOR | |
409 | gnet_stats_copy_rate_est(&d, &h->rate_est) < 0 || | |
410 | #endif | |
411 | gnet_stats_copy_queue(&d, &h->qstats) < 0) | |
412 | goto errout; | |
413 | ||
414 | if (gnet_stats_finish_copy(&d) < 0) | |
415 | goto errout; | |
416 | ||
417 | return 0; | |
418 | ||
419 | errout: | |
420 | return -1; | |
421 | } | |
422 | ||
423 | static int | |
424 | tca_get_fill(struct sk_buff *skb, struct tc_action *a, u32 pid, u32 seq, | |
e431b8c0 | 425 | u16 flags, int event, int bind, int ref) |
1da177e4 LT |
426 | { |
427 | struct tcamsg *t; | |
428 | struct nlmsghdr *nlh; | |
429 | unsigned char *b = skb->tail; | |
430 | struct rtattr *x; | |
431 | ||
e431b8c0 JHS |
432 | nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags); |
433 | ||
1da177e4 LT |
434 | t = NLMSG_DATA(nlh); |
435 | t->tca_family = AF_UNSPEC; | |
9ef1d4c7 PM |
436 | t->tca__pad1 = 0; |
437 | t->tca__pad2 = 0; | |
1da177e4 LT |
438 | |
439 | x = (struct rtattr*) skb->tail; | |
440 | RTA_PUT(skb, TCA_ACT_TAB, 0, NULL); | |
441 | ||
442 | if (tcf_action_dump(skb, a, bind, ref) < 0) | |
443 | goto rtattr_failure; | |
444 | ||
445 | x->rta_len = skb->tail - (u8*)x; | |
446 | ||
447 | nlh->nlmsg_len = skb->tail - b; | |
448 | return skb->len; | |
449 | ||
450 | rtattr_failure: | |
451 | nlmsg_failure: | |
452 | skb_trim(skb, b - skb->data); | |
453 | return -1; | |
454 | } | |
455 | ||
456 | static int | |
457 | act_get_notify(u32 pid, struct nlmsghdr *n, struct tc_action *a, int event) | |
458 | { | |
459 | struct sk_buff *skb; | |
460 | int err = 0; | |
461 | ||
462 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
463 | if (!skb) | |
464 | return -ENOBUFS; | |
465 | if (tca_get_fill(skb, a, pid, n->nlmsg_seq, 0, event, 0, 0) <= 0) { | |
466 | kfree_skb(skb); | |
467 | return -EINVAL; | |
468 | } | |
469 | err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT); | |
470 | if (err > 0) | |
471 | err = 0; | |
472 | return err; | |
473 | } | |
474 | ||
475 | static struct tc_action * | |
476 | tcf_action_get_1(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int *err) | |
477 | { | |
478 | struct rtattr *tb[TCA_ACT_MAX+1]; | |
479 | struct tc_action *a; | |
480 | int index; | |
481 | ||
482 | *err = -EINVAL; | |
483 | if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0) | |
484 | return NULL; | |
485 | ||
486 | if (tb[TCA_ACT_INDEX - 1] == NULL || | |
487 | RTA_PAYLOAD(tb[TCA_ACT_INDEX - 1]) < sizeof(index)) | |
488 | return NULL; | |
489 | index = *(int *)RTA_DATA(tb[TCA_ACT_INDEX - 1]); | |
490 | ||
491 | *err = -ENOMEM; | |
492 | a = kmalloc(sizeof(struct tc_action), GFP_KERNEL); | |
493 | if (a == NULL) | |
494 | return NULL; | |
495 | memset(a, 0, sizeof(struct tc_action)); | |
496 | ||
497 | *err = -EINVAL; | |
498 | a->ops = tc_lookup_action(tb[TCA_ACT_KIND - 1]); | |
499 | if (a->ops == NULL) | |
500 | goto err_free; | |
501 | if (a->ops->lookup == NULL) | |
502 | goto err_mod; | |
503 | *err = -ENOENT; | |
504 | if (a->ops->lookup(a, index) == 0) | |
505 | goto err_mod; | |
506 | ||
507 | module_put(a->ops->owner); | |
508 | *err = 0; | |
509 | return a; | |
510 | err_mod: | |
511 | module_put(a->ops->owner); | |
512 | err_free: | |
513 | kfree(a); | |
514 | return NULL; | |
515 | } | |
516 | ||
517 | static void cleanup_a(struct tc_action *act) | |
518 | { | |
519 | struct tc_action *a; | |
520 | ||
521 | for (a = act; a; a = act) { | |
522 | act = a->next; | |
523 | kfree(a); | |
524 | } | |
525 | } | |
526 | ||
527 | static struct tc_action *create_a(int i) | |
528 | { | |
529 | struct tc_action *act; | |
530 | ||
531 | act = kmalloc(sizeof(*act), GFP_KERNEL); | |
532 | if (act == NULL) { | |
533 | printk("create_a: failed to alloc!\n"); | |
534 | return NULL; | |
535 | } | |
536 | memset(act, 0, sizeof(*act)); | |
537 | act->order = i; | |
538 | return act; | |
539 | } | |
540 | ||
541 | static int tca_action_flush(struct rtattr *rta, struct nlmsghdr *n, u32 pid) | |
542 | { | |
543 | struct sk_buff *skb; | |
544 | unsigned char *b; | |
545 | struct nlmsghdr *nlh; | |
546 | struct tcamsg *t; | |
547 | struct netlink_callback dcb; | |
548 | struct rtattr *x; | |
549 | struct rtattr *tb[TCA_ACT_MAX+1]; | |
550 | struct rtattr *kind; | |
551 | struct tc_action *a = create_a(0); | |
552 | int err = -EINVAL; | |
553 | ||
554 | if (a == NULL) { | |
555 | printk("tca_action_flush: couldnt create tc_action\n"); | |
556 | return err; | |
557 | } | |
558 | ||
559 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
560 | if (!skb) { | |
561 | printk("tca_action_flush: failed skb alloc\n"); | |
562 | kfree(a); | |
563 | return -ENOBUFS; | |
564 | } | |
565 | ||
566 | b = (unsigned char *)skb->tail; | |
567 | ||
568 | if (rtattr_parse_nested(tb, TCA_ACT_MAX, rta) < 0) | |
569 | goto err_out; | |
570 | ||
571 | kind = tb[TCA_ACT_KIND-1]; | |
572 | a->ops = tc_lookup_action(kind); | |
573 | if (a->ops == NULL) | |
574 | goto err_out; | |
575 | ||
576 | nlh = NLMSG_PUT(skb, pid, n->nlmsg_seq, RTM_DELACTION, sizeof(*t)); | |
577 | t = NLMSG_DATA(nlh); | |
578 | t->tca_family = AF_UNSPEC; | |
9ef1d4c7 PM |
579 | t->tca__pad1 = 0; |
580 | t->tca__pad2 = 0; | |
1da177e4 LT |
581 | |
582 | x = (struct rtattr *) skb->tail; | |
583 | RTA_PUT(skb, TCA_ACT_TAB, 0, NULL); | |
584 | ||
585 | err = a->ops->walk(skb, &dcb, RTM_DELACTION, a); | |
586 | if (err < 0) | |
587 | goto rtattr_failure; | |
588 | ||
589 | x->rta_len = skb->tail - (u8 *) x; | |
590 | ||
591 | nlh->nlmsg_len = skb->tail - b; | |
592 | nlh->nlmsg_flags |= NLM_F_ROOT; | |
593 | module_put(a->ops->owner); | |
594 | kfree(a); | |
ac6d439d | 595 | err = rtnetlink_send(skb, pid, RTNLGRP_TC, n->nlmsg_flags&NLM_F_ECHO); |
1da177e4 LT |
596 | if (err > 0) |
597 | return 0; | |
598 | ||
599 | return err; | |
600 | ||
601 | rtattr_failure: | |
602 | module_put(a->ops->owner); | |
603 | nlmsg_failure: | |
604 | err_out: | |
605 | kfree_skb(skb); | |
606 | kfree(a); | |
607 | return err; | |
608 | } | |
609 | ||
610 | static int | |
611 | tca_action_gd(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int event) | |
612 | { | |
613 | int i, ret = 0; | |
614 | struct rtattr *tb[TCA_ACT_MAX_PRIO+1]; | |
615 | struct tc_action *head = NULL, *act, *act_prev = NULL; | |
616 | ||
617 | if (rtattr_parse_nested(tb, TCA_ACT_MAX_PRIO, rta) < 0) | |
618 | return -EINVAL; | |
619 | ||
620 | if (event == RTM_DELACTION && n->nlmsg_flags&NLM_F_ROOT) { | |
621 | if (tb[0] != NULL && tb[1] == NULL) | |
622 | return tca_action_flush(tb[0], n, pid); | |
623 | } | |
624 | ||
625 | for (i=0; i < TCA_ACT_MAX_PRIO && tb[i]; i++) { | |
626 | act = tcf_action_get_1(tb[i], n, pid, &ret); | |
627 | if (act == NULL) | |
628 | goto err; | |
629 | act->order = i+1; | |
630 | ||
631 | if (head == NULL) | |
632 | head = act; | |
633 | else | |
634 | act_prev->next = act; | |
635 | act_prev = act; | |
636 | } | |
637 | ||
638 | if (event == RTM_GETACTION) | |
639 | ret = act_get_notify(pid, n, head, event); | |
640 | else { /* delete */ | |
641 | struct sk_buff *skb; | |
642 | ||
643 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
644 | if (!skb) { | |
645 | ret = -ENOBUFS; | |
646 | goto err; | |
647 | } | |
648 | ||
649 | if (tca_get_fill(skb, head, pid, n->nlmsg_seq, 0, event, | |
650 | 0, 1) <= 0) { | |
651 | kfree_skb(skb); | |
652 | ret = -EINVAL; | |
653 | goto err; | |
654 | } | |
655 | ||
656 | /* now do the delete */ | |
657 | tcf_action_destroy(head, 0); | |
ac6d439d | 658 | ret = rtnetlink_send(skb, pid, RTNLGRP_TC, |
1da177e4 LT |
659 | n->nlmsg_flags&NLM_F_ECHO); |
660 | if (ret > 0) | |
661 | return 0; | |
662 | return ret; | |
663 | } | |
664 | err: | |
665 | cleanup_a(head); | |
666 | return ret; | |
667 | } | |
668 | ||
669 | static int tcf_add_notify(struct tc_action *a, u32 pid, u32 seq, int event, | |
e431b8c0 | 670 | u16 flags) |
1da177e4 LT |
671 | { |
672 | struct tcamsg *t; | |
673 | struct nlmsghdr *nlh; | |
674 | struct sk_buff *skb; | |
675 | struct rtattr *x; | |
676 | unsigned char *b; | |
677 | int err = 0; | |
678 | ||
679 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
680 | if (!skb) | |
681 | return -ENOBUFS; | |
682 | ||
683 | b = (unsigned char *)skb->tail; | |
684 | ||
e431b8c0 | 685 | nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*t), flags); |
1da177e4 LT |
686 | t = NLMSG_DATA(nlh); |
687 | t->tca_family = AF_UNSPEC; | |
9ef1d4c7 PM |
688 | t->tca__pad1 = 0; |
689 | t->tca__pad2 = 0; | |
690 | ||
1da177e4 LT |
691 | x = (struct rtattr*) skb->tail; |
692 | RTA_PUT(skb, TCA_ACT_TAB, 0, NULL); | |
693 | ||
694 | if (tcf_action_dump(skb, a, 0, 0) < 0) | |
695 | goto rtattr_failure; | |
696 | ||
697 | x->rta_len = skb->tail - (u8*)x; | |
698 | ||
699 | nlh->nlmsg_len = skb->tail - b; | |
ac6d439d | 700 | NETLINK_CB(skb).dst_group = RTNLGRP_TC; |
1da177e4 | 701 | |
ac6d439d | 702 | err = rtnetlink_send(skb, pid, RTNLGRP_TC, flags&NLM_F_ECHO); |
1da177e4 LT |
703 | if (err > 0) |
704 | err = 0; | |
705 | return err; | |
706 | ||
707 | rtattr_failure: | |
708 | nlmsg_failure: | |
f6e57464 | 709 | kfree_skb(skb); |
1da177e4 LT |
710 | return -1; |
711 | } | |
712 | ||
713 | ||
714 | static int | |
715 | tcf_action_add(struct rtattr *rta, struct nlmsghdr *n, u32 pid, int ovr) | |
716 | { | |
717 | int ret = 0; | |
718 | struct tc_action *act; | |
719 | struct tc_action *a; | |
720 | u32 seq = n->nlmsg_seq; | |
721 | ||
722 | act = tcf_action_init(rta, NULL, NULL, ovr, 0, &ret); | |
723 | if (act == NULL) | |
724 | goto done; | |
725 | ||
726 | /* dump then free all the actions after update; inserted policy | |
727 | * stays intact | |
728 | * */ | |
729 | ret = tcf_add_notify(act, pid, seq, RTM_NEWACTION, n->nlmsg_flags); | |
730 | for (a = act; a; a = act) { | |
731 | act = a->next; | |
732 | kfree(a); | |
733 | } | |
734 | done: | |
735 | return ret; | |
736 | } | |
737 | ||
738 | static int tc_ctl_action(struct sk_buff *skb, struct nlmsghdr *n, void *arg) | |
739 | { | |
740 | struct rtattr **tca = arg; | |
741 | u32 pid = skb ? NETLINK_CB(skb).pid : 0; | |
742 | int ret = 0, ovr = 0; | |
743 | ||
744 | if (tca[TCA_ACT_TAB-1] == NULL) { | |
745 | printk("tc_ctl_action: received NO action attribs\n"); | |
746 | return -EINVAL; | |
747 | } | |
748 | ||
749 | /* n->nlmsg_flags&NLM_F_CREATE | |
750 | * */ | |
751 | switch (n->nlmsg_type) { | |
752 | case RTM_NEWACTION: | |
753 | /* we are going to assume all other flags | |
754 | * imply create only if it doesnt exist | |
755 | * Note that CREATE | EXCL implies that | |
756 | * but since we want avoid ambiguity (eg when flags | |
757 | * is zero) then just set this | |
758 | */ | |
759 | if (n->nlmsg_flags&NLM_F_REPLACE) | |
760 | ovr = 1; | |
761 | replay: | |
762 | ret = tcf_action_add(tca[TCA_ACT_TAB-1], n, pid, ovr); | |
763 | if (ret == -EAGAIN) | |
764 | goto replay; | |
765 | break; | |
766 | case RTM_DELACTION: | |
767 | ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_DELACTION); | |
768 | break; | |
769 | case RTM_GETACTION: | |
770 | ret = tca_action_gd(tca[TCA_ACT_TAB-1], n, pid, RTM_GETACTION); | |
771 | break; | |
772 | default: | |
773 | BUG(); | |
774 | } | |
775 | ||
776 | return ret; | |
777 | } | |
778 | ||
779 | static char * | |
780 | find_dump_kind(struct nlmsghdr *n) | |
781 | { | |
782 | struct rtattr *tb1, *tb2[TCA_ACT_MAX+1]; | |
783 | struct rtattr *tb[TCA_ACT_MAX_PRIO + 1]; | |
784 | struct rtattr *rta[TCAA_MAX + 1]; | |
785 | struct rtattr *kind; | |
786 | int min_len = NLMSG_LENGTH(sizeof(struct tcamsg)); | |
787 | int attrlen = n->nlmsg_len - NLMSG_ALIGN(min_len); | |
788 | struct rtattr *attr = (void *) n + NLMSG_ALIGN(min_len); | |
789 | ||
790 | if (rtattr_parse(rta, TCAA_MAX, attr, attrlen) < 0) | |
791 | return NULL; | |
792 | tb1 = rta[TCA_ACT_TAB - 1]; | |
793 | if (tb1 == NULL) | |
794 | return NULL; | |
795 | ||
796 | if (rtattr_parse(tb, TCA_ACT_MAX_PRIO, RTA_DATA(tb1), | |
797 | NLMSG_ALIGN(RTA_PAYLOAD(tb1))) < 0) | |
798 | return NULL; | |
799 | if (tb[0] == NULL) | |
800 | return NULL; | |
801 | ||
802 | if (rtattr_parse(tb2, TCA_ACT_MAX, RTA_DATA(tb[0]), | |
803 | RTA_PAYLOAD(tb[0])) < 0) | |
804 | return NULL; | |
805 | kind = tb2[TCA_ACT_KIND-1]; | |
806 | ||
807 | return (char *) RTA_DATA(kind); | |
808 | } | |
809 | ||
810 | static int | |
811 | tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb) | |
812 | { | |
813 | struct nlmsghdr *nlh; | |
814 | unsigned char *b = skb->tail; | |
815 | struct rtattr *x; | |
816 | struct tc_action_ops *a_o; | |
817 | struct tc_action a; | |
818 | int ret = 0; | |
819 | struct tcamsg *t = (struct tcamsg *) NLMSG_DATA(cb->nlh); | |
820 | char *kind = find_dump_kind(cb->nlh); | |
821 | ||
822 | if (kind == NULL) { | |
823 | printk("tc_dump_action: action bad kind\n"); | |
824 | return 0; | |
825 | } | |
826 | ||
827 | a_o = tc_lookup_action_n(kind); | |
828 | if (a_o == NULL) { | |
829 | printk("failed to find %s\n", kind); | |
830 | return 0; | |
831 | } | |
832 | ||
833 | memset(&a, 0, sizeof(struct tc_action)); | |
834 | a.ops = a_o; | |
835 | ||
836 | if (a_o->walk == NULL) { | |
837 | printk("tc_dump_action: %s !capable of dumping table\n", kind); | |
838 | goto rtattr_failure; | |
839 | } | |
840 | ||
841 | nlh = NLMSG_PUT(skb, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, | |
842 | cb->nlh->nlmsg_type, sizeof(*t)); | |
843 | t = NLMSG_DATA(nlh); | |
844 | t->tca_family = AF_UNSPEC; | |
9ef1d4c7 PM |
845 | t->tca__pad1 = 0; |
846 | t->tca__pad2 = 0; | |
1da177e4 LT |
847 | |
848 | x = (struct rtattr *) skb->tail; | |
849 | RTA_PUT(skb, TCA_ACT_TAB, 0, NULL); | |
850 | ||
851 | ret = a_o->walk(skb, cb, RTM_GETACTION, &a); | |
852 | if (ret < 0) | |
853 | goto rtattr_failure; | |
854 | ||
855 | if (ret > 0) { | |
856 | x->rta_len = skb->tail - (u8 *) x; | |
857 | ret = skb->len; | |
858 | } else | |
859 | skb_trim(skb, (u8*)x - skb->data); | |
860 | ||
861 | nlh->nlmsg_len = skb->tail - b; | |
862 | if (NETLINK_CB(cb->skb).pid && ret) | |
863 | nlh->nlmsg_flags |= NLM_F_MULTI; | |
864 | module_put(a_o->owner); | |
865 | return skb->len; | |
866 | ||
867 | rtattr_failure: | |
868 | nlmsg_failure: | |
869 | module_put(a_o->owner); | |
870 | skb_trim(skb, b - skb->data); | |
871 | return skb->len; | |
872 | } | |
873 | ||
874 | static int __init tc_action_init(void) | |
875 | { | |
876 | struct rtnetlink_link *link_p = rtnetlink_links[PF_UNSPEC]; | |
877 | ||
878 | if (link_p) { | |
879 | link_p[RTM_NEWACTION-RTM_BASE].doit = tc_ctl_action; | |
880 | link_p[RTM_DELACTION-RTM_BASE].doit = tc_ctl_action; | |
881 | link_p[RTM_GETACTION-RTM_BASE].doit = tc_ctl_action; | |
882 | link_p[RTM_GETACTION-RTM_BASE].dumpit = tc_dump_action; | |
883 | } | |
884 | ||
979b6c13 | 885 | printk("TC classifier action (bugs to netdev@vger.kernel.org cc " |
1da177e4 LT |
886 | "hadi@cyberus.ca)\n"); |
887 | return 0; | |
888 | } | |
889 | ||
890 | subsys_initcall(tc_action_init); | |
891 | ||
892 | EXPORT_SYMBOL(tcf_register_action); | |
893 | EXPORT_SYMBOL(tcf_unregister_action); | |
894 | EXPORT_SYMBOL(tcf_action_exec); | |
895 | EXPORT_SYMBOL(tcf_action_dump_1); |