cls_basic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. };
  26. struct basic_filter {
  27. u32 handle;
  28. struct tcf_exts exts;
  29. struct tcf_ematch_tree ematches;
  30. struct tcf_result res;
  31. struct list_head link;
  32. };
  33. static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  34. struct tcf_result *res)
  35. {
  36. int r;
  37. struct basic_head *head = tp->root;
  38. struct basic_filter *f;
  39. list_for_each_entry(f, &head->flist, link) {
  40. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  41. continue;
  42. *res = f->res;
  43. r = tcf_exts_exec(skb, &f->exts, res);
  44. if (r < 0)
  45. continue;
  46. return r;
  47. }
  48. return -1;
  49. }
  50. static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
  51. {
  52. unsigned long l = 0UL;
  53. struct basic_head *head = tp->root;
  54. struct basic_filter *f;
  55. if (head == NULL)
  56. return 0UL;
  57. list_for_each_entry(f, &head->flist, link)
  58. if (f->handle == handle)
  59. l = (unsigned long) f;
  60. return l;
  61. }
  62. static void basic_put(struct tcf_proto *tp, unsigned long f)
  63. {
  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. tp->root = head;
  73. return 0;
  74. }
  75. static void basic_delete_filter(struct tcf_proto *tp, struct basic_filter *f)
  76. {
  77. tcf_unbind_filter(tp, &f->res);
  78. tcf_exts_destroy(tp, &f->exts);
  79. tcf_em_tree_destroy(tp, &f->ematches);
  80. kfree(f);
  81. }
  82. static void basic_destroy(struct tcf_proto *tp)
  83. {
  84. struct basic_head *head = tp->root;
  85. struct basic_filter *f, *n;
  86. list_for_each_entry_safe(f, n, &head->flist, link) {
  87. list_del(&f->link);
  88. basic_delete_filter(tp, f);
  89. }
  90. kfree(head);
  91. }
  92. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  93. {
  94. struct basic_head *head = tp->root;
  95. struct basic_filter *t, *f = (struct basic_filter *) arg;
  96. list_for_each_entry(t, &head->flist, link)
  97. if (t == f) {
  98. tcf_tree_lock(tp);
  99. list_del(&t->link);
  100. tcf_tree_unlock(tp);
  101. basic_delete_filter(tp, t);
  102. return 0;
  103. }
  104. return -ENOENT;
  105. }
  106. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  107. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  108. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  109. };
  110. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  111. struct basic_filter *f, unsigned long base,
  112. struct nlattr **tb,
  113. struct nlattr *est, bool ovr)
  114. {
  115. int err;
  116. struct tcf_exts e;
  117. struct tcf_ematch_tree t;
  118. tcf_exts_init(&e, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  119. err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
  120. if (err < 0)
  121. return err;
  122. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
  123. if (err < 0)
  124. goto errout;
  125. if (tb[TCA_BASIC_CLASSID]) {
  126. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  127. tcf_bind_filter(tp, &f->res, base);
  128. }
  129. tcf_exts_change(tp, &f->exts, &e);
  130. tcf_em_tree_change(tp, &f->ematches, &t);
  131. return 0;
  132. errout:
  133. tcf_exts_destroy(tp, &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 = tp->root;
  142. struct nlattr *tb[TCA_BASIC_MAX + 1];
  143. struct basic_filter *f = (struct basic_filter *) *arg;
  144. if (tca[TCA_OPTIONS] == NULL)
  145. return -EINVAL;
  146. err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  147. basic_policy);
  148. if (err < 0)
  149. return err;
  150. if (f != NULL) {
  151. if (handle && f->handle != handle)
  152. return -EINVAL;
  153. return basic_set_parms(net, tp, f, base, tb, tca[TCA_RATE], ovr);
  154. }
  155. err = -ENOBUFS;
  156. f = kzalloc(sizeof(*f), GFP_KERNEL);
  157. if (f == NULL)
  158. goto errout;
  159. tcf_exts_init(&f->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  160. err = -EINVAL;
  161. if (handle)
  162. f->handle = handle;
  163. else {
  164. unsigned int i = 0x80000000;
  165. do {
  166. if (++head->hgenerator == 0x7FFFFFFF)
  167. head->hgenerator = 1;
  168. } while (--i > 0 && basic_get(tp, head->hgenerator));
  169. if (i <= 0) {
  170. pr_err("Insufficient number of handles\n");
  171. goto errout;
  172. }
  173. f->handle = head->hgenerator;
  174. }
  175. err = basic_set_parms(net, tp, f, base, tb, tca[TCA_RATE], ovr);
  176. if (err < 0)
  177. goto errout;
  178. tcf_tree_lock(tp);
  179. list_add(&f->link, &head->flist);
  180. tcf_tree_unlock(tp);
  181. *arg = (unsigned long) f;
  182. return 0;
  183. errout:
  184. if (*arg == 0UL && f)
  185. kfree(f);
  186. return err;
  187. }
  188. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  189. {
  190. struct basic_head *head = tp->root;
  191. struct basic_filter *f;
  192. list_for_each_entry(f, &head->flist, link) {
  193. if (arg->count < arg->skip)
  194. goto skip;
  195. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  196. arg->stop = 1;
  197. break;
  198. }
  199. skip:
  200. arg->count++;
  201. }
  202. }
  203. static int basic_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  204. struct sk_buff *skb, struct tcmsg *t)
  205. {
  206. struct basic_filter *f = (struct basic_filter *) fh;
  207. struct nlattr *nest;
  208. if (f == NULL)
  209. return skb->len;
  210. t->tcm_handle = f->handle;
  211. nest = nla_nest_start(skb, TCA_OPTIONS);
  212. if (nest == NULL)
  213. goto nla_put_failure;
  214. if (f->res.classid &&
  215. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  216. goto nla_put_failure;
  217. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  218. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  219. goto nla_put_failure;
  220. nla_nest_end(skb, nest);
  221. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  222. goto nla_put_failure;
  223. return skb->len;
  224. nla_put_failure:
  225. nla_nest_cancel(skb, nest);
  226. return -1;
  227. }
  228. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  229. .kind = "basic",
  230. .classify = basic_classify,
  231. .init = basic_init,
  232. .destroy = basic_destroy,
  233. .get = basic_get,
  234. .put = basic_put,
  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");