cls_basic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. break;
  64. }
  65. }
  66. return l;
  67. }
  68. static int basic_init(struct tcf_proto *tp)
  69. {
  70. struct basic_head *head;
  71. head = kzalloc(sizeof(*head), GFP_KERNEL);
  72. if (head == NULL)
  73. return -ENOBUFS;
  74. INIT_LIST_HEAD(&head->flist);
  75. rcu_assign_pointer(tp->root, head);
  76. return 0;
  77. }
  78. static void basic_delete_filter(struct rcu_head *head)
  79. {
  80. struct basic_filter *f = container_of(head, struct basic_filter, rcu);
  81. tcf_exts_destroy(&f->exts);
  82. tcf_em_tree_destroy(&f->ematches);
  83. kfree(f);
  84. }
  85. static bool basic_destroy(struct tcf_proto *tp, bool force)
  86. {
  87. struct basic_head *head = rtnl_dereference(tp->root);
  88. struct basic_filter *f, *n;
  89. if (!force && !list_empty(&head->flist))
  90. return false;
  91. list_for_each_entry_safe(f, n, &head->flist, link) {
  92. list_del_rcu(&f->link);
  93. tcf_unbind_filter(tp, &f->res);
  94. call_rcu(&f->rcu, basic_delete_filter);
  95. }
  96. RCU_INIT_POINTER(tp->root, NULL);
  97. kfree_rcu(head, rcu);
  98. return true;
  99. }
  100. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  101. {
  102. struct basic_filter *f = (struct basic_filter *) arg;
  103. list_del_rcu(&f->link);
  104. tcf_unbind_filter(tp, &f->res);
  105. call_rcu(&f->rcu, basic_delete_filter);
  106. return 0;
  107. }
  108. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  109. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  110. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  111. };
  112. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  113. struct basic_filter *f, unsigned long base,
  114. struct nlattr **tb,
  115. struct nlattr *est, bool ovr)
  116. {
  117. int err;
  118. struct tcf_exts e;
  119. struct tcf_ematch_tree t;
  120. err = tcf_exts_init(&e, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  121. if (err < 0)
  122. return err;
  123. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  124. if (err < 0)
  125. goto errout;
  126. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
  127. if (err < 0)
  128. goto errout;
  129. if (tb[TCA_BASIC_CLASSID]) {
  130. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  131. tcf_bind_filter(tp, &f->res, base);
  132. }
  133. tcf_exts_change(tp, &f->exts, &e);
  134. tcf_em_tree_change(tp, &f->ematches, &t);
  135. f->tp = tp;
  136. return 0;
  137. errout:
  138. tcf_exts_destroy(&e);
  139. return err;
  140. }
  141. static int basic_change(struct net *net, struct sk_buff *in_skb,
  142. struct tcf_proto *tp, unsigned long base, u32 handle,
  143. struct nlattr **tca, unsigned long *arg, bool ovr)
  144. {
  145. int err;
  146. struct basic_head *head = rtnl_dereference(tp->root);
  147. struct nlattr *tb[TCA_BASIC_MAX + 1];
  148. struct basic_filter *fold = (struct basic_filter *) *arg;
  149. struct basic_filter *fnew;
  150. if (tca[TCA_OPTIONS] == NULL)
  151. return -EINVAL;
  152. err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  153. basic_policy);
  154. if (err < 0)
  155. return err;
  156. if (fold != NULL) {
  157. if (handle && fold->handle != handle)
  158. return -EINVAL;
  159. }
  160. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  161. if (!fnew)
  162. return -ENOBUFS;
  163. err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  164. if (err < 0)
  165. goto errout;
  166. err = -EINVAL;
  167. if (handle) {
  168. fnew->handle = handle;
  169. } else if (fold) {
  170. fnew->handle = fold->handle;
  171. } else {
  172. unsigned int i = 0x80000000;
  173. do {
  174. if (++head->hgenerator == 0x7FFFFFFF)
  175. head->hgenerator = 1;
  176. } while (--i > 0 && basic_get(tp, head->hgenerator));
  177. if (i <= 0) {
  178. pr_err("Insufficient number of handles\n");
  179. goto errout;
  180. }
  181. fnew->handle = head->hgenerator;
  182. }
  183. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
  184. if (err < 0)
  185. goto errout;
  186. *arg = (unsigned long)fnew;
  187. if (fold) {
  188. list_replace_rcu(&fold->link, &fnew->link);
  189. tcf_unbind_filter(tp, &fold->res);
  190. call_rcu(&fold->rcu, basic_delete_filter);
  191. } else {
  192. list_add_rcu(&fnew->link, &head->flist);
  193. }
  194. return 0;
  195. errout:
  196. tcf_exts_destroy(&fnew->exts);
  197. kfree(fnew);
  198. return err;
  199. }
  200. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  201. {
  202. struct basic_head *head = rtnl_dereference(tp->root);
  203. struct basic_filter *f;
  204. list_for_each_entry(f, &head->flist, link) {
  205. if (arg->count < arg->skip)
  206. goto skip;
  207. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  208. arg->stop = 1;
  209. break;
  210. }
  211. skip:
  212. arg->count++;
  213. }
  214. }
  215. static int basic_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  216. struct sk_buff *skb, struct tcmsg *t)
  217. {
  218. struct basic_filter *f = (struct basic_filter *) fh;
  219. struct nlattr *nest;
  220. if (f == NULL)
  221. return skb->len;
  222. t->tcm_handle = f->handle;
  223. nest = nla_nest_start(skb, TCA_OPTIONS);
  224. if (nest == NULL)
  225. goto nla_put_failure;
  226. if (f->res.classid &&
  227. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  228. goto nla_put_failure;
  229. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  230. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  231. goto nla_put_failure;
  232. nla_nest_end(skb, nest);
  233. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  234. goto nla_put_failure;
  235. return skb->len;
  236. nla_put_failure:
  237. nla_nest_cancel(skb, nest);
  238. return -1;
  239. }
  240. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  241. .kind = "basic",
  242. .classify = basic_classify,
  243. .init = basic_init,
  244. .destroy = basic_destroy,
  245. .get = basic_get,
  246. .change = basic_change,
  247. .delete = basic_delete,
  248. .walk = basic_walk,
  249. .dump = basic_dump,
  250. .owner = THIS_MODULE,
  251. };
  252. static int __init init_basic(void)
  253. {
  254. return register_tcf_proto_ops(&cls_basic_ops);
  255. }
  256. static void __exit exit_basic(void)
  257. {
  258. unregister_tcf_proto_ops(&cls_basic_ops);
  259. }
  260. module_init(init_basic)
  261. module_exit(exit_basic)
  262. MODULE_LICENSE("GPL");