cls_basic.c 6.4 KB

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