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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/errno.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/skbuff.h>
  21. #include <net/act_api.h>
  22. #include <net/pkt_cls.h>
  23. struct basic_head
  24. {
  25. u32 hgenerator;
  26. struct list_head flist;
  27. };
  28. struct basic_filter
  29. {
  30. u32 handle;
  31. struct tcf_exts exts;
  32. struct tcf_ematch_tree ematches;
  33. struct tcf_result res;
  34. struct list_head link;
  35. };
  36. static struct tcf_ext_map basic_ext_map = {
  37. .action = TCA_BASIC_ACT,
  38. .police = TCA_BASIC_POLICE
  39. };
  40. static int basic_classify(struct sk_buff *skb, struct tcf_proto *tp,
  41. struct tcf_result *res)
  42. {
  43. int r;
  44. struct basic_head *head = (struct basic_head *) tp->root;
  45. struct basic_filter *f;
  46. list_for_each_entry(f, &head->flist, link) {
  47. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  48. continue;
  49. *res = f->res;
  50. r = tcf_exts_exec(skb, &f->exts, res);
  51. if (r < 0)
  52. continue;
  53. return r;
  54. }
  55. return -1;
  56. }
  57. static unsigned long basic_get(struct tcf_proto *tp, u32 handle)
  58. {
  59. unsigned long l = 0UL;
  60. struct basic_head *head = (struct basic_head *) tp->root;
  61. struct basic_filter *f;
  62. if (head == NULL)
  63. return 0UL;
  64. list_for_each_entry(f, &head->flist, link)
  65. if (f->handle == handle)
  66. l = (unsigned long) f;
  67. return l;
  68. }
  69. static void basic_put(struct tcf_proto *tp, unsigned long f)
  70. {
  71. }
  72. static int basic_init(struct tcf_proto *tp)
  73. {
  74. return 0;
  75. }
  76. static inline void basic_delete_filter(struct tcf_proto *tp,
  77. struct basic_filter *f)
  78. {
  79. tcf_unbind_filter(tp, &f->res);
  80. tcf_exts_destroy(tp, &f->exts);
  81. tcf_em_tree_destroy(tp, &f->ematches);
  82. kfree(f);
  83. }
  84. static void basic_destroy(struct tcf_proto *tp)
  85. {
  86. struct basic_head *head = (struct basic_head *) xchg(&tp->root, NULL);
  87. struct basic_filter *f, *n;
  88. list_for_each_entry_safe(f, n, &head->flist, link) {
  89. list_del(&f->link);
  90. basic_delete_filter(tp, f);
  91. }
  92. }
  93. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  94. {
  95. struct basic_head *head = (struct basic_head *) tp->root;
  96. struct basic_filter *t, *f = (struct basic_filter *) arg;
  97. list_for_each_entry(t, &head->flist, link)
  98. if (t == f) {
  99. tcf_tree_lock(tp);
  100. list_del(&t->link);
  101. tcf_tree_unlock(tp);
  102. basic_delete_filter(tp, t);
  103. return 0;
  104. }
  105. return -ENOENT;
  106. }
  107. static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  108. unsigned long base, struct rtattr **tb,
  109. struct rtattr *est)
  110. {
  111. int err = -EINVAL;
  112. struct tcf_exts e;
  113. struct tcf_ematch_tree t;
  114. if (tb[TCA_BASIC_CLASSID-1])
  115. if (RTA_PAYLOAD(tb[TCA_BASIC_CLASSID-1]) < sizeof(u32))
  116. return err;
  117. err = tcf_exts_validate(tp, tb, est, &e, &basic_ext_map);
  118. if (err < 0)
  119. return err;
  120. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES-1], &t);
  121. if (err < 0)
  122. goto errout;
  123. if (tb[TCA_BASIC_CLASSID-1]) {
  124. f->res.classid = *(u32*)RTA_DATA(tb[TCA_BASIC_CLASSID-1]);
  125. tcf_bind_filter(tp, &f->res, base);
  126. }
  127. tcf_exts_change(tp, &f->exts, &e);
  128. tcf_em_tree_change(tp, &f->ematches, &t);
  129. return 0;
  130. errout:
  131. tcf_exts_destroy(tp, &e);
  132. return err;
  133. }
  134. static int basic_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  135. struct rtattr **tca, unsigned long *arg)
  136. {
  137. int err = -EINVAL;
  138. struct basic_head *head = (struct basic_head *) tp->root;
  139. struct rtattr *tb[TCA_BASIC_MAX];
  140. struct basic_filter *f = (struct basic_filter *) *arg;
  141. if (tca[TCA_OPTIONS-1] == NULL)
  142. return -EINVAL;
  143. if (rtattr_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS-1]) < 0)
  144. return -EINVAL;
  145. if (f != NULL) {
  146. if (handle && f->handle != handle)
  147. return -EINVAL;
  148. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  149. }
  150. err = -ENOBUFS;
  151. if (head == NULL) {
  152. head = kmalloc(sizeof(*head), GFP_KERNEL);
  153. if (head == NULL)
  154. goto errout;
  155. memset(head, 0, sizeof(*head));
  156. INIT_LIST_HEAD(&head->flist);
  157. tp->root = head;
  158. }
  159. f = kmalloc(sizeof(*f), GFP_KERNEL);
  160. if (f == NULL)
  161. goto errout;
  162. memset(f, 0, sizeof(*f));
  163. err = -EINVAL;
  164. if (handle)
  165. f->handle = handle;
  166. else {
  167. 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-1]);
  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. unsigned char *b = skb->tail;
  211. struct rtattr *rta;
  212. if (f == NULL)
  213. return skb->len;
  214. t->tcm_handle = f->handle;
  215. rta = (struct rtattr *) b;
  216. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  217. if (f->res.classid)
  218. RTA_PUT(skb, TCA_BASIC_CLASSID, sizeof(u32), &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 rtattr_failure;
  222. rta->rta_len = (skb->tail - b);
  223. return skb->len;
  224. rtattr_failure:
  225. skb_trim(skb, b - skb->data);
  226. return -1;
  227. }
  228. static struct tcf_proto_ops cls_basic_ops = {
  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");