cls_basic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/netlink.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. struct basic_head *head;
  74. head = kzalloc(sizeof(*head), GFP_KERNEL);
  75. if (head == NULL)
  76. return -ENOBUFS;
  77. INIT_LIST_HEAD(&head->flist);
  78. tp->root = head;
  79. return 0;
  80. }
  81. static inline void basic_delete_filter(struct tcf_proto *tp,
  82. struct basic_filter *f)
  83. {
  84. tcf_unbind_filter(tp, &f->res);
  85. tcf_exts_destroy(tp, &f->exts);
  86. tcf_em_tree_destroy(tp, &f->ematches);
  87. kfree(f);
  88. }
  89. static void basic_destroy(struct tcf_proto *tp)
  90. {
  91. struct basic_head *head = (struct basic_head *) xchg(&tp->root, NULL);
  92. struct basic_filter *f, *n;
  93. list_for_each_entry_safe(f, n, &head->flist, link) {
  94. list_del(&f->link);
  95. basic_delete_filter(tp, f);
  96. }
  97. kfree(head);
  98. }
  99. static int basic_delete(struct tcf_proto *tp, unsigned long arg)
  100. {
  101. struct basic_head *head = (struct basic_head *) tp->root;
  102. struct basic_filter *t, *f = (struct basic_filter *) arg;
  103. list_for_each_entry(t, &head->flist, link)
  104. if (t == f) {
  105. tcf_tree_lock(tp);
  106. list_del(&t->link);
  107. tcf_tree_unlock(tp);
  108. basic_delete_filter(tp, t);
  109. return 0;
  110. }
  111. return -ENOENT;
  112. }
  113. static inline int basic_set_parms(struct tcf_proto *tp, struct basic_filter *f,
  114. unsigned long base, struct rtattr **tb,
  115. struct rtattr *est)
  116. {
  117. int err = -EINVAL;
  118. struct tcf_exts e;
  119. struct tcf_ematch_tree t;
  120. if (tb[TCA_BASIC_CLASSID-1])
  121. if (RTA_PAYLOAD(tb[TCA_BASIC_CLASSID-1]) < sizeof(u32))
  122. return err;
  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-1], &t);
  127. if (err < 0)
  128. goto errout;
  129. if (tb[TCA_BASIC_CLASSID-1]) {
  130. f->res.classid = *(u32*)RTA_DATA(tb[TCA_BASIC_CLASSID-1]);
  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 rtattr **tca, unsigned long *arg)
  142. {
  143. int err = -EINVAL;
  144. struct basic_head *head = (struct basic_head *) tp->root;
  145. struct rtattr *tb[TCA_BASIC_MAX];
  146. struct basic_filter *f = (struct basic_filter *) *arg;
  147. if (tca[TCA_OPTIONS-1] == NULL)
  148. return -EINVAL;
  149. if (rtattr_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS-1]) < 0)
  150. return -EINVAL;
  151. if (f != NULL) {
  152. if (handle && f->handle != handle)
  153. return -EINVAL;
  154. return basic_set_parms(tp, f, base, tb, tca[TCA_RATE-1]);
  155. }
  156. err = -ENOBUFS;
  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_pointer(skb);
  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_pointer(skb) - b;
  220. return skb->len;
  221. rtattr_failure:
  222. nlmsg_trim(skb, b);
  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");