cls_basic.c 6.4 KB

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