cls_basic.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. 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 inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  113. unsigned long base, struct rtattr **tb,
  114. struct rtattr *est)
  115. {
  116. int err = -EINVAL;
  117. struct tcf_exts e;
  118. struct tcf_ematch_tree t;
  119. if (tb[TCA_BASIC_CLASSID-1])
  120. if (RTA_PAYLOAD(tb[TCA_BASIC_CLASSID-1]) < sizeof(u32))
  121. return err;
  122. err = tcf_exts_validate(tp, tb, est, &e, &basic_ext_map);
  123. if (err < 0)
  124. return err;
  125. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES-1], &t);
  126. if (err < 0)
  127. goto errout;
  128. if (tb[TCA_BASIC_CLASSID-1]) {
  129. f->res.classid = *(u32*)RTA_DATA(tb[TCA_BASIC_CLASSID-1]);
  130. tcf_bind_filter(tp, &f->res, base);
  131. }
  132. tcf_exts_change(tp, &f->exts, &e);
  133. tcf_em_tree_change(tp, &f->ematches, &t);
  134. return 0;
  135. errout:
  136. tcf_exts_destroy(tp, &e);
  137. return err;
  138. }
  139. static int basic_change(struct tcf_proto *tp, unsigned long base, u32 handle,
  140. struct rtattr **tca, unsigned long *arg)
  141. {
  142. int err = -EINVAL;
  143. struct basic_head *head = (struct basic_head *) tp->root;
  144. struct rtattr *tb[TCA_BASIC_MAX];
  145. struct basic_filter *f = (struct basic_filter *) *arg;
  146. if (tca[TCA_OPTIONS-1] == NULL)
  147. return -EINVAL;
  148. if (rtattr_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS-1]) < 0)
  149. return -EINVAL;
  150. if (f != NULL) {
  151. if (handle && f->handle != handle)
  152. return -EINVAL;
  153. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  154. }
  155. err = -ENOBUFS;
  156. f = kzalloc(sizeof(*f), GFP_KERNEL);
  157. if (f == NULL)
  158. goto errout;
  159. err = -EINVAL;
  160. if (handle)
  161. f->handle = handle;
  162. else {
  163. unsigned int i = 0x80000000;
  164. do {
  165. if (++head->hgenerator == 0x7FFFFFFF)
  166. head->hgenerator = 1;
  167. } while (--i > 0 && basic_get(tp, head->hgenerator));
  168. if (i <= 0) {
  169. printk(KERN_ERR "Insufficient number of handles\n");
  170. goto errout;
  171. }
  172. f->handle = head->hgenerator;
  173. }
  174. err = basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  175. if (err < 0)
  176. goto errout;
  177. tcf_tree_lock(tp);
  178. list_add(&f->link, &head->flist);
  179. tcf_tree_unlock(tp);
  180. *arg = (unsigned long) f;
  181. return 0;
  182. errout:
  183. if (*arg == 0UL && f)
  184. kfree(f);
  185. return err;
  186. }
  187. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  188. {
  189. struct basic_head *head = (struct basic_head *) tp->root;
  190. struct basic_filter *f;
  191. list_for_each_entry(f, &head->flist, link) {
  192. if (arg->count < arg->skip)
  193. goto skip;
  194. if (arg->fn(tp, (unsigned long) f, arg) < 0) {
  195. arg->stop = 1;
  196. break;
  197. }
  198. skip:
  199. arg->count++;
  200. }
  201. }
  202. static int basic_dump(struct tcf_proto *tp, unsigned long fh,
  203. struct sk_buff *skb, struct tcmsg *t)
  204. {
  205. struct basic_filter *f = (struct basic_filter *) fh;
  206. unsigned char *b = skb->tail;
  207. struct rtattr *rta;
  208. if (f == NULL)
  209. return skb->len;
  210. t->tcm_handle = f->handle;
  211. rta = (struct rtattr *) b;
  212. RTA_PUT(skb, TCA_OPTIONS, 0, NULL);
  213. if (f->res.classid)
  214. RTA_PUT(skb, TCA_BASIC_CLASSID, sizeof(u32), &f->res.classid);
  215. if (tcf_exts_dump(skb, &f->exts, &basic_ext_map) < 0 ||
  216. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  217. goto rtattr_failure;
  218. rta->rta_len = (skb->tail - b);
  219. return skb->len;
  220. rtattr_failure:
  221. skb_trim(skb, b - skb->data);
  222. return -1;
  223. }
  224. static struct tcf_proto_ops cls_basic_ops = {
  225. .kind = "basic",
  226. .classify = basic_classify,
  227. .init = basic_init,
  228. .destroy = basic_destroy,
  229. .get = basic_get,
  230. .put = basic_put,
  231. .change = basic_change,
  232. .delete = basic_delete,
  233. .walk = basic_walk,
  234. .dump = basic_dump,
  235. .owner = THIS_MODULE,
  236. };
  237. static int __init init_basic(void)
  238. {
  239. return register_tcf_proto_ops(&cls_basic_ops);
  240. }
  241. static void __exit exit_basic(void)
  242. {
  243. unregister_tcf_proto_ops(&cls_basic_ops);
  244. }
  245. module_init(init_basic)
  246. module_exit(exit_basic)
  247. MODULE_LICENSE("GPL");