cls_basic.c 6.7 KB

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