cls_basic.c 7.1 KB

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