cls_basic.c 6.4 KB

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