cls_basic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/idr.h>
  20. #include <net/netlink.h>
  21. #include <net/act_api.h>
  22. #include <net/pkt_cls.h>
  23. struct basic_head {
  24. struct list_head flist;
  25. struct idr handle_idr;
  26. struct rcu_head rcu;
  27. };
  28. struct basic_filter {
  29. u32 handle;
  30. struct tcf_exts exts;
  31. struct tcf_ematch_tree ematches;
  32. struct tcf_result res;
  33. struct tcf_proto *tp;
  34. struct list_head link;
  35. struct rcu_head rcu;
  36. };
  37. static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  38. struct tcf_result *res)
  39. {
  40. int r;
  41. struct basic_head *head = rcu_dereference_bh(tp->root);
  42. struct basic_filter *f;
  43. list_for_each_entry_rcu(f, &head->flist, link) {
  44. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  45. continue;
  46. *res = f->res;
  47. r = tcf_exts_exec(skb, &f->exts, res);
  48. if (r < 0)
  49. continue;
  50. return r;
  51. }
  52. return -1;
  53. }
  54. static void *basic_get(struct tcf_proto *tp, u32 handle)
  55. {
  56. struct basic_head *head = rtnl_dereference(tp->root);
  57. struct basic_filter *f;
  58. list_for_each_entry(f, &head->flist, link) {
  59. if (f->handle == handle) {
  60. return f;
  61. }
  62. }
  63. return NULL;
  64. }
  65. static int basic_init(struct tcf_proto *tp)
  66. {
  67. struct basic_head *head;
  68. head = kzalloc(sizeof(*head), GFP_KERNEL);
  69. if (head == NULL)
  70. return -ENOBUFS;
  71. INIT_LIST_HEAD(&head->flist);
  72. idr_init(&head->handle_idr);
  73. rcu_assign_pointer(tp->root, head);
  74. return 0;
  75. }
  76. static void basic_delete_filter(struct rcu_head *head)
  77. {
  78. struct basic_filter *f = container_of(head, struct basic_filter, rcu);
  79. tcf_exts_destroy(&f->exts);
  80. tcf_em_tree_destroy(&f->ematches);
  81. kfree(f);
  82. }
  83. static void basic_destroy(struct tcf_proto *tp)
  84. {
  85. struct basic_head *head = rtnl_dereference(tp->root);
  86. struct basic_filter *f, *n;
  87. list_for_each_entry_safe(f, n, &head->flist, link) {
  88. list_del_rcu(&f->link);
  89. tcf_unbind_filter(tp, &f->res);
  90. idr_remove_ext(&head->handle_idr, f->handle);
  91. call_rcu(&f->rcu, basic_delete_filter);
  92. }
  93. idr_destroy(&head->handle_idr);
  94. kfree_rcu(head, rcu);
  95. }
  96. static int basic_delete(struct tcf_proto *tp, void *arg, bool *last)
  97. {
  98. struct basic_head *head = rtnl_dereference(tp->root);
  99. struct basic_filter *f = arg;
  100. list_del_rcu(&f->link);
  101. tcf_unbind_filter(tp, &f->res);
  102. idr_remove_ext(&head->handle_idr, f->handle);
  103. call_rcu(&f->rcu, basic_delete_filter);
  104. *last = list_empty(&head->flist);
  105. return 0;
  106. }
  107. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  108. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  109. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  110. };
  111. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  112. struct basic_filter *f, unsigned long base,
  113. struct nlattr **tb,
  114. struct nlattr *est, bool ovr)
  115. {
  116. int err;
  117. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
  118. if (err < 0)
  119. return err;
  120. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
  121. if (err < 0)
  122. return err;
  123. if (tb[TCA_BASIC_CLASSID]) {
  124. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  125. tcf_bind_filter(tp, &f->res, base);
  126. }
  127. f->tp = tp;
  128. return 0;
  129. }
  130. static int basic_change(struct net *net, struct sk_buff *in_skb,
  131. struct tcf_proto *tp, unsigned long base, u32 handle,
  132. struct nlattr **tca, void **arg, bool ovr)
  133. {
  134. int err;
  135. struct basic_head *head = rtnl_dereference(tp->root);
  136. struct nlattr *tb[TCA_BASIC_MAX + 1];
  137. struct basic_filter *fold = (struct basic_filter *) *arg;
  138. struct basic_filter *fnew;
  139. unsigned long idr_index;
  140. if (tca[TCA_OPTIONS] == NULL)
  141. return -EINVAL;
  142. err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  143. basic_policy, NULL);
  144. if (err < 0)
  145. return err;
  146. if (fold != NULL) {
  147. if (handle && fold->handle != handle)
  148. return -EINVAL;
  149. }
  150. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  151. if (!fnew)
  152. return -ENOBUFS;
  153. err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  154. if (err < 0)
  155. goto errout;
  156. if (handle) {
  157. fnew->handle = handle;
  158. if (!fold) {
  159. err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
  160. handle, handle + 1, GFP_KERNEL);
  161. if (err)
  162. goto errout;
  163. }
  164. } else {
  165. err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
  166. 1, 0x7FFFFFFF, GFP_KERNEL);
  167. if (err)
  168. goto errout;
  169. fnew->handle = idr_index;
  170. }
  171. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr);
  172. if (err < 0) {
  173. if (!fold)
  174. idr_remove_ext(&head->handle_idr, fnew->handle);
  175. goto errout;
  176. }
  177. *arg = fnew;
  178. if (fold) {
  179. idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
  180. list_replace_rcu(&fold->link, &fnew->link);
  181. tcf_unbind_filter(tp, &fold->res);
  182. call_rcu(&fold->rcu, basic_delete_filter);
  183. } else {
  184. list_add_rcu(&fnew->link, &head->flist);
  185. }
  186. return 0;
  187. errout:
  188. tcf_exts_destroy(&fnew->exts);
  189. kfree(fnew);
  190. return err;
  191. }
  192. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  193. {
  194. struct basic_head *head = rtnl_dereference(tp->root);
  195. struct basic_filter *f;
  196. list_for_each_entry(f, &head->flist, link) {
  197. if (arg->count < arg->skip)
  198. goto skip;
  199. if (arg->fn(tp, f, arg) < 0) {
  200. arg->stop = 1;
  201. break;
  202. }
  203. skip:
  204. arg->count++;
  205. }
  206. }
  207. static void basic_bind_class(void *fh, u32 classid, unsigned long cl)
  208. {
  209. struct basic_filter *f = fh;
  210. if (f && f->res.classid == classid)
  211. f->res.class = cl;
  212. }
  213. static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
  214. struct sk_buff *skb, struct tcmsg *t)
  215. {
  216. struct basic_filter *f = fh;
  217. struct nlattr *nest;
  218. if (f == NULL)
  219. return skb->len;
  220. t->tcm_handle = f->handle;
  221. nest = nla_nest_start(skb, TCA_OPTIONS);
  222. if (nest == NULL)
  223. goto nla_put_failure;
  224. if (f->res.classid &&
  225. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  226. goto nla_put_failure;
  227. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  228. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  229. goto nla_put_failure;
  230. nla_nest_end(skb, nest);
  231. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  232. goto nla_put_failure;
  233. return skb->len;
  234. nla_put_failure:
  235. nla_nest_cancel(skb, nest);
  236. return -1;
  237. }
  238. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  239. .kind = "basic",
  240. .classify = basic_classify,
  241. .init = basic_init,
  242. .destroy = basic_destroy,
  243. .get = basic_get,
  244. .change = basic_change,
  245. .delete = basic_delete,
  246. .walk = basic_walk,
  247. .dump = basic_dump,
  248. .bind_class = basic_bind_class,
  249. .owner = THIS_MODULE,
  250. };
  251. static int __init init_basic(void)
  252. {
  253. return register_tcf_proto_ops(&cls_basic_ops);
  254. }
  255. static void __exit exit_basic(void)
  256. {
  257. unregister_tcf_proto_ops(&cls_basic_ops);
  258. }
  259. module_init(init_basic)
  260. module_exit(exit_basic)
  261. MODULE_LICENSE("GPL");