cls_basic.c 6.9 KB

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