cls_basic.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/mm.h>
  16. #include <linux/errno.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.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 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 = (struct basic_head *) xchg(&tp->root, NULL);
  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. }
  97. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  98. {
  99. struct basic_head *head = (struct basic_head *) tp->root;
  100. struct basic_filter *t, *f = (struct basic_filter *) arg;
  101. list_for_each_entry(t, &head->flist, link)
  102. if (t == f) {
  103. tcf_tree_lock(tp);
  104. list_del(&t->link);
  105. tcf_tree_unlock(tp);
  106. basic_delete_filter(tp, t);
  107. return 0;
  108. }
  109. return -ENOENT;
  110. }
  111. static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  112. unsigned long base, struct rtattr **tb,
  113. struct rtattr *est)
  114. {
  115. int err = -EINVAL;
  116. struct tcf_exts e;
  117. struct tcf_ematch_tree t;
  118. if (tb[TCA_BASIC_CLASSID-1])
  119. if (RTA_PAYLOAD(tb[TCA_BASIC_CLASSID-1]) < sizeof(u32))
  120. return err;
  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-1], &t);
  125. if (err < 0)
  126. goto errout;
  127. if (tb[TCA_BASIC_CLASSID-1]) {
  128. f->res.classid = *(u32*)RTA_DATA(tb[TCA_BASIC_CLASSID-1]);
  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 rtattr **tca, unsigned long *arg)
  140. {
  141. int err = -EINVAL;
  142. struct basic_head *head = (struct basic_head *) tp->root;
  143. struct rtattr *tb[TCA_BASIC_MAX];
  144. struct basic_filter *f = (struct basic_filter *) *arg;
  145. if (tca[TCA_OPTIONS-1] == NULL)
  146. return -EINVAL;
  147. if (rtattr_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS-1]) < 0)
  148. return -EINVAL;
  149. if (f != NULL) {
  150. if (handle && f->handle != handle)
  151. return -EINVAL;
  152. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  153. }
  154. err = -ENOBUFS;
  155. f = kzalloc(sizeof(*f), GFP_KERNEL);
  156. if (f == NULL)
  157. goto errout;
  158. err = -EINVAL;
  159. if (handle)
  160. f->handle = handle;
  161. else {
  162. unsigned int i = 0x80000000;
  163. do {
  164. if (++head->hgenerator == 0x7FFFFFFF)
  165. head->hgenerator = 1;
  166. } while (--i > 0 && basic_get(tp, head->hgenerator));
  167. if (i <= 0) {
  168. printk(KERN_ERR "Insufficient number of handles\n");
  169. goto errout;
  170. }
  171. f->handle = head->hgenerator;
  172. }
  173. err = basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  174. if (err < 0)
  175. goto errout;
  176. tcf_tree_lock(tp);
  177. list_add(&f->link, &head->flist);
  178. tcf_tree_unlock(tp);
  179. *arg = (unsigned long) f;
  180. return 0;
  181. errout:
  182. if (*arg == 0UL && f)
  183. kfree(f);
  184. return err;
  185. }
  186. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  187. {
  188. struct basic_head *head = (struct basic_head *) tp->root;
  189. struct basic_filter *f;
  190. list_for_each_entry(f, &head->flist, link) {
  191. if (arg->count < arg->skip)
  192. goto skip;
  193. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  194. arg->stop = 1;
  195. break;
  196. }
  197. skip:
  198. arg->count++;
  199. }
  200. }
  201. static int basic_dump(struct tcf_proto *tp, unsigned long fh,
  202. struct sk_buff *skb, struct tcmsg *t)
  203. {
  204. struct basic_filter *f = (struct basic_filter *) fh;
  205. unsigned char *b = skb->tail;
  206. struct rtattr *rta;
  207. if (f == NULL)
  208. return skb->len;
  209. t->tcm_handle = f->handle;
  210. rta = (struct rtattr *) b;
  211. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  212. if (f->res.classid)
  213. RTA_PUT(skb, TCA_BASIC_CLASSID, sizeof(u32), &f->res.classid);
  214. if (tcf_exts_dump(skb, &f->exts, &basic_ext_map) < 0 ||
  215. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  216. goto rtattr_failure;
  217. rta->rta_len = (skb->tail - b);
  218. return skb->len;
  219. rtattr_failure:
  220. skb_trim(skb, b - skb->data);
  221. return -1;
  222. }
  223. static struct tcf_proto_ops cls_basic_ops = {
  224. .kind = "basic",
  225. .classify = basic_classify,
  226. .init = basic_init,
  227. .destroy = basic_destroy,
  228. .get = basic_get,
  229. .put = basic_put,
  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");