cls_basic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/sched.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/errno.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/skbuff.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 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. return 0;
  74. }
  75. static inline void basic_delete_filter(struct tcf_proto *tp,
  76. struct basic_filter *f)
  77. {
  78. tcf_unbind_filter(tp, &f->res);
  79. tcf_exts_destroy(tp, &f->exts);
  80. tcf_em_tree_destroy(tp, &f->ematches);
  81. kfree(f);
  82. }
  83. static void basic_destroy(struct tcf_proto *tp)
  84. {
  85. struct basic_head *head = (struct basic_head *) xchg(&tp->root, NULL);
  86. struct basic_filter *f, *n;
  87. list_for_each_entry_safe(f, n, &head->flist, link) {
  88. list_del(&f->link);
  89. basic_delete_filter(tp, f);
  90. }
  91. }
  92. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  93. {
  94. struct basic_head *head = (struct basic_head *) tp->root;
  95. struct basic_filter *t, *f = (struct basic_filter *) arg;
  96. list_for_each_entry(t, &head->flist, link)
  97. if (t == f) {
  98. tcf_tree_lock(tp);
  99. list_del(&t->link);
  100. tcf_tree_unlock(tp);
  101. basic_delete_filter(tp, t);
  102. return 0;
  103. }
  104. return -ENOENT;
  105. }
  106. static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  107. unsigned long base, struct rtattr **tb,
  108. struct rtattr *est)
  109. {
  110. int err = -EINVAL;
  111. struct tcf_exts e;
  112. struct tcf_ematch_tree t;
  113. if (tb[TCA_BASIC_CLASSID-1])
  114. if (RTA_PAYLOAD(tb[TCA_BASIC_CLASSID-1]) < sizeof(u32))
  115. return err;
  116. err = tcf_exts_validate(tp, tb, est, &e, &basic_ext_map);
  117. if (err < 0)
  118. return err;
  119. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES-1], &t);
  120. if (err < 0)
  121. goto errout;
  122. if (tb[TCA_BASIC_CLASSID-1]) {
  123. f->res.classid = *(u32*)RTA_DATA(tb[TCA_BASIC_CLASSID-1]);
  124. tcf_bind_filter(tp, &f->res, base);
  125. }
  126. tcf_exts_change(tp, &f->exts, &e);
  127. tcf_em_tree_change(tp, &f->ematches, &t);
  128. return 0;
  129. errout:
  130. tcf_exts_destroy(tp, &e);
  131. return err;
  132. }
  133. static int basic_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  134. struct rtattr **tca, unsigned long *arg)
  135. {
  136. int err = -EINVAL;
  137. struct basic_head *head = (struct basic_head *) tp->root;
  138. struct rtattr *tb[TCA_BASIC_MAX];
  139. struct basic_filter *f = (struct basic_filter *) *arg;
  140. if (tca[TCA_OPTIONS-1] == NULL)
  141. return -EINVAL;
  142. if (rtattr_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS-1]) < 0)
  143. return -EINVAL;
  144. if (f != NULL) {
  145. if (handle && f->handle != handle)
  146. return -EINVAL;
  147. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  148. }
  149. err = -ENOBUFS;
  150. if (head == NULL) {
  151. head = kzalloc(sizeof(*head), GFP_KERNEL);
  152. if (head == NULL)
  153. goto errout;
  154. INIT_LIST_HEAD(&head->flist);
  155. tp->root = head;
  156. }
  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. printk(KERN_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-1]);
  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. unsigned char *b = skb->tail;
  208. struct rtattr *rta;
  209. if (f == NULL)
  210. return skb->len;
  211. t->tcm_handle = f->handle;
  212. rta = (struct rtattr *) b;
  213. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  214. if (f->res.classid)
  215. RTA_PUT(skb, TCA_BASIC_CLASSID, sizeof(u32), &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 rtattr_failure;
  219. rta->rta_len = (skb->tail - b);
  220. return skb->len;
  221. rtattr_failure:
  222. skb_trim(skb, b - skb->data);
  223. return -1;
  224. }
  225. static struct tcf_proto_ops cls_basic_ops = {
  226. .kind = "basic",
  227. .classify = basic_classify,
  228. .init = basic_init,
  229. .destroy = basic_destroy,
  230. .get = basic_get,
  231. .put = basic_put,
  232. .change = basic_change,
  233. .delete = basic_delete,
  234. .walk = basic_walk,
  235. .dump = basic_dump,
  236. .owner = THIS_MODULE,
  237. };
  238. static int __init init_basic(void)
  239. {
  240. return register_tcf_proto_ops(&cls_basic_ops);
  241. }
  242. static void __exit exit_basic(void)
  243. {
  244. unregister_tcf_proto_ops(&cls_basic_ops);
  245. }
  246. module_init(init_basic)
  247. module_exit(exit_basic)
  248. MODULE_LICENSE("GPL");