cls_basic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 const struct tcf_ext_map basic_ext_map = {
  34. .action = TCA_BASIC_ACT,
  35. .police = TCA_BASIC_POLICE
  36. };
  37. static int basic_classify(struct sk_buff *skb, struct tcf_proto *tp,
  38. struct tcf_result *res)
  39. {
  40. int r;
  41. struct basic_head *head = (struct basic_head *) tp->root;
  42. struct basic_filter *f;
  43. list_for_each_entry(f, &head->flist, link) {
  44. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  45. continue;
  46. *res = f->res;
  47. r = tcf_exts_exec(skb, &f->exts, res);
  48. if (r < 0)
  49. continue;
  50. return r;
  51. }
  52. return -1;
  53. }
  54. static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
  55. {
  56. unsigned long l = 0UL;
  57. struct basic_head *head = (struct basic_head *) tp->root;
  58. struct basic_filter *f;
  59. if (head == NULL)
  60. return 0UL;
  61. list_for_each_entry(f, &head->flist, link)
  62. if (f->handle == handle)
  63. l = (unsigned long) f;
  64. return l;
  65. }
  66. static void basic_put(struct tcf_proto *tp, unsigned long f)
  67. {
  68. }
  69. static int basic_init(struct tcf_proto *tp)
  70. {
  71. struct basic_head *head;
  72. head = kzalloc(sizeof(*head), GFP_KERNEL);
  73. if (head == NULL)
  74. return -ENOBUFS;
  75. INIT_LIST_HEAD(&head->flist);
  76. tp->root = head;
  77. return 0;
  78. }
  79. static void basic_delete_filter(struct tcf_proto *tp, struct basic_filter *f)
  80. {
  81. tcf_unbind_filter(tp, &f->res);
  82. tcf_exts_destroy(tp, &f->exts);
  83. tcf_em_tree_destroy(tp, &f->ematches);
  84. kfree(f);
  85. }
  86. static void basic_destroy(struct tcf_proto *tp)
  87. {
  88. struct basic_head *head = tp->root;
  89. struct basic_filter *f, *n;
  90. list_for_each_entry_safe(f, n, &head->flist, link) {
  91. list_del(&f->link);
  92. basic_delete_filter(tp, f);
  93. }
  94. kfree(head);
  95. }
  96. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  97. {
  98. struct basic_head *head = (struct basic_head *) tp->root;
  99. struct basic_filter *t, *f = (struct basic_filter *) arg;
  100. list_for_each_entry(t, &head->flist, link)
  101. if (t == f) {
  102. tcf_tree_lock(tp);
  103. list_del(&t->link);
  104. tcf_tree_unlock(tp);
  105. basic_delete_filter(tp, t);
  106. return 0;
  107. }
  108. return -ENOENT;
  109. }
  110. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  111. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  112. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  113. };
  114. static int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  115. unsigned long base, struct nlattr **tb,
  116. struct nlattr *est)
  117. {
  118. int err = -EINVAL;
  119. struct tcf_exts e;
  120. struct tcf_ematch_tree t;
  121. err = tcf_exts_validate(tp, tb, est, &e, &basic_ext_map);
  122. if (err < 0)
  123. return err;
  124. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &t);
  125. if (err < 0)
  126. goto errout;
  127. if (tb[TCA_BASIC_CLASSID]) {
  128. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  129. tcf_bind_filter(tp, &f->res, base);
  130. }
  131. tcf_exts_change(tp, &f->exts, &e);
  132. tcf_em_tree_change(tp, &f->ematches, &t);
  133. return 0;
  134. errout:
  135. tcf_exts_destroy(tp, &e);
  136. return err;
  137. }
  138. static int basic_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  139. struct nlattr **tca, unsigned long *arg)
  140. {
  141. int err;
  142. struct basic_head *head = (struct basic_head *) tp->root;
  143. struct nlattr *tb[TCA_BASIC_MAX + 1];
  144. struct basic_filter *f = (struct basic_filter *) *arg;
  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 (f != NULL) {
  152. if (handle && f->handle != handle)
  153. return -EINVAL;
  154. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE]);
  155. }
  156. err = -ENOBUFS;
  157. f = kzalloc(sizeof(*f), GFP_KERNEL);
  158. if (f == NULL)
  159. goto errout;
  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(tp, f, base, tb, tca[TCA_RATE]);
  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 = (struct basic_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 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. if (tcf_exts_dump(skb, &f->exts, &basic_ext_map) < 0 ||
  217. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  218. goto nla_put_failure;
  219. nla_nest_end(skb, nest);
  220. if (tcf_exts_dump_stats(skb, &f->exts, &basic_ext_map) < 0)
  221. goto nla_put_failure;
  222. return skb->len;
  223. nla_put_failure:
  224. nla_nest_cancel(skb, nest);
  225. return -1;
  226. }
  227. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  228. .kind = "basic",
  229. .classify = basic_classify,
  230. .init = basic_init,
  231. .destroy = basic_destroy,
  232. .get = basic_get,
  233. .put = basic_put,
  234. .change = basic_change,
  235. .delete = basic_delete,
  236. .walk = basic_walk,
  237. .dump = basic_dump,
  238. .owner = THIS_MODULE,
  239. };
  240. static int __init init_basic(void)
  241. {
  242. return register_tcf_proto_ops(&cls_basic_ops);
  243. }
  244. static void __exit exit_basic(void)
  245. {
  246. unregister_tcf_proto_ops(&cls_basic_ops);
  247. }
  248. module_init(init_basic)
  249. module_exit(exit_basic)
  250. MODULE_LICENSE("GPL");