cls_basic.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * net/sched/cls_basic.c Basic Packet Classifier.
  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. * Authors: Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.h>
  19. #include <net/netlink.h>
  20. #include <net/act_api.h>
  21. #include <net/pkt_cls.h>
  22. struct basic_head {
  23. u32 hgenerator;
  24. struct list_head flist;
  25. struct rcu_head rcu;
  26. };
  27. struct basic_filter {
  28. u32 handle;
  29. struct tcf_exts exts;
  30. struct tcf_ematch_tree ematches;
  31. struct tcf_result res;
  32. struct tcf_proto *tp;
  33. struct list_head link;
  34. struct rcu_head rcu;
  35. };
  36. static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  37. struct tcf_result *res)
  38. {
  39. int r;
  40. struct basic_head *head = rcu_dereference_bh(tp->root);
  41. struct basic_filter *f;
  42. list_for_each_entry_rcu(f, &head->flist, link) {
  43. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  44. continue;
  45. *res = f->res;
  46. r = tcf_exts_exec(skb, &f->exts, res);
  47. if (r < 0)
  48. continue;
  49. return r;
  50. }
  51. return -1;
  52. }
  53. static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
  54. {
  55. unsigned long l = 0UL;
  56. struct basic_head *head = rtnl_dereference(tp->root);
  57. struct basic_filter *f;
  58. if (head == NULL)
  59. return 0UL;
  60. list_for_each_entry(f, &head->flist, link)
  61. if (f->handle == handle)
  62. l = (unsigned long) f;
  63. return l;
  64. }
  65. static int basic_init(struct tcf_proto *tp)
  66. {
  67. struct basic_head *head;
  68. head = kzalloc(sizeof(*head), GFP_KERNEL);
  69. if (head == NULL)
  70. return -ENOBUFS;
  71. INIT_LIST_HEAD(&head->flist);
  72. rcu_assign_pointer(tp->root, head);
  73. return 0;
  74. }
  75. static void basic_delete_filter(struct rcu_head *head)
  76. {
  77. struct basic_filter *f = container_of(head, struct basic_filter, rcu);
  78. tcf_exts_destroy(&f->exts);
  79. tcf_em_tree_destroy(&f->ematches);
  80. kfree(f);
  81. }
  82. static void basic_destroy(struct tcf_proto *tp)
  83. {
  84. struct basic_head *head = rtnl_dereference(tp->root);
  85. struct basic_filter *f, *n;
  86. list_for_each_entry_safe(f, n, &head->flist, link) {
  87. list_del_rcu(&f->link);
  88. tcf_unbind_filter(tp, &f->res);
  89. call_rcu(&f->rcu, basic_delete_filter);
  90. }
  91. RCU_INIT_POINTER(tp->root, NULL);
  92. kfree_rcu(head, rcu);
  93. }
  94. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  95. {
  96. struct basic_filter *f = (struct basic_filter *) arg;
  97. list_del_rcu(&f->link);
  98. tcf_unbind_filter(tp, &f->res);
  99. call_rcu(&f->rcu, basic_delete_filter);
  100. return 0;
  101. }
  102. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  103. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  104. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  105. };
  106. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  107. struct basic_filter *f, unsigned long base,
  108. struct nlattr **tb,
  109. struct nlattr *est, bool ovr)
  110. {
  111. int err;
  112. struct tcf_exts e;
  113. struct tcf_ematch_tree t;
  114. tcf_exts_init(&e, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  115. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  116. if (err < 0)
  117. return err;
  118. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
  119. if (err < 0)
  120. goto errout;
  121. if (tb[TCA_BASIC_CLASSID]) {
  122. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  123. tcf_bind_filter(tp, &f->res, base);
  124. }
  125. tcf_exts_change(tp, &f->exts, &e);
  126. tcf_em_tree_change(tp, &f->ematches, &t);
  127. f->tp = tp;
  128. return 0;
  129. errout:
  130. tcf_exts_destroy(&e);
  131. return err;
  132. }
  133. static int basic_change(struct net *net, struct sk_buff *in_skb,
  134. struct tcf_proto *tp, unsigned long base, u32 handle,
  135. struct nlattr **tca, unsigned long *arg, bool ovr)
  136. {
  137. int err;
  138. struct basic_head *head = rtnl_dereference(tp->root);
  139. struct nlattr *tb[TCA_BASIC_MAX + 1];
  140. struct basic_filter *fold = (struct basic_filter *) *arg;
  141. struct basic_filter *fnew;
  142. if (tca[TCA_OPTIONS] == NULL)
  143. return -EINVAL;
  144. err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  145. basic_policy);
  146. if (err < 0)
  147. return err;
  148. if (fold != NULL) {
  149. if (handle && fold->handle != handle)
  150. return -EINVAL;
  151. }
  152. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  153. if (!fnew)
  154. return -ENOBUFS;
  155. tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  156. err = -EINVAL;
  157. if (handle) {
  158. fnew->handle = handle;
  159. } else if (fold) {
  160. fnew->handle = fold->handle;
  161. } else {
  162. unsigned int i = 0x80000000;
  163. do {
  164. if (++head->hgenerator == 0x7FFFFFFF)
  165. head->hgenerator = 1;
  166. } while (--i > 0 && basic_get(tp, head->hgenerator));
  167. if (i <= 0) {
  168. pr_err("Insufficient number of handles\n");
  169. goto errout;
  170. }
  171. fnew->handle = head->hgenerator;
  172. }
  173. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
  174. if (err < 0)
  175. goto errout;
  176. *arg = (unsigned long)fnew;
  177. if (fold) {
  178. list_replace_rcu(&fold->link, &fnew->link);
  179. tcf_unbind_filter(tp, &fold->res);
  180. call_rcu(&fold->rcu, basic_delete_filter);
  181. } else {
  182. list_add_rcu(&fnew->link, &head->flist);
  183. }
  184. return 0;
  185. errout:
  186. kfree(fnew);
  187. return err;
  188. }
  189. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  190. {
  191. struct basic_head *head = rtnl_dereference(tp->root);
  192. struct basic_filter *f;
  193. list_for_each_entry(f, &head->flist, link) {
  194. if (arg->count < arg->skip)
  195. goto skip;
  196. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  197. arg->stop = 1;
  198. break;
  199. }
  200. skip:
  201. arg->count++;
  202. }
  203. }
  204. static int basic_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  205. struct sk_buff *skb, struct tcmsg *t)
  206. {
  207. struct basic_filter *f = (struct basic_filter *) fh;
  208. struct nlattr *nest;
  209. if (f == NULL)
  210. return skb->len;
  211. t->tcm_handle = f->handle;
  212. nest = nla_nest_start(skb, TCA_OPTIONS);
  213. if (nest == NULL)
  214. goto nla_put_failure;
  215. if (f->res.classid &&
  216. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  217. goto nla_put_failure;
  218. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  219. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  220. goto nla_put_failure;
  221. nla_nest_end(skb, nest);
  222. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  223. goto nla_put_failure;
  224. return skb->len;
  225. nla_put_failure:
  226. nla_nest_cancel(skb, nest);
  227. return -1;
  228. }
  229. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  230. .kind = "basic",
  231. .classify = basic_classify,
  232. .init = basic_init,
  233. .destroy = basic_destroy,
  234. .get = basic_get,
  235. .change = basic_change,
  236. .delete = basic_delete,
  237. .walk = basic_walk,
  238. .dump = basic_dump,
  239. .owner = THIS_MODULE,
  240. };
  241. static int __init init_basic(void)
  242. {
  243. return register_tcf_proto_ops(&cls_basic_ops);
  244. }
  245. static void __exit exit_basic(void)
  246. {
  247. unregister_tcf_proto_ops(&cls_basic_ops);
  248. }
  249. module_init(init_basic)
  250. module_exit(exit_basic)
  251. MODULE_LICENSE("GPL");