cls_basic.c 7.4 KB

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