~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sched/act_api.c

Version: ~ [ linux-5.15-rc7 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp