cls_basic.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_work rwork;
  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 basic_filter *f)
  77. {
  78. tcf_exts_destroy(&f->exts);
  79. tcf_em_tree_destroy(&f->ematches);
  80. tcf_exts_put_net(&f->exts);
  81. kfree(f);
  82. }
  83. static void basic_delete_filter_work(struct work_struct *work)
  84. {
  85. struct basic_filter *f = container_of(to_rcu_work(work),
  86. struct basic_filter,
  87. rwork);
  88. rtnl_lock();
  89. __basic_delete_filter(f);
  90. rtnl_unlock();
  91. }
  92. static void basic_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
  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. idr_remove(&head->handle_idr, f->handle);
  100. if (tcf_exts_get_net(&f->exts))
  101. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  102. else
  103. __basic_delete_filter(f);
  104. }
  105. idr_destroy(&head->handle_idr);
  106. kfree_rcu(head, rcu);
  107. }
  108. static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
  109. struct netlink_ext_ack *extack)
  110. {
  111. struct basic_head *head = rtnl_dereference(tp->root);
  112. struct basic_filter *f = arg;
  113. list_del_rcu(&f->link);
  114. tcf_unbind_filter(tp, &f->res);
  115. idr_remove(&head->handle_idr, f->handle);
  116. tcf_exts_get_net(&f->exts);
  117. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  118. *last = list_empty(&head->flist);
  119. return 0;
  120. }
  121. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  122. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  123. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  124. };
  125. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  126. struct basic_filter *f, unsigned long base,
  127. struct nlattr **tb,
  128. struct nlattr *est, bool ovr,
  129. struct netlink_ext_ack *extack)
  130. {
  131. int err;
  132. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
  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. struct netlink_ext_ack *extack)
  149. {
  150. int err;
  151. struct basic_head *head = rtnl_dereference(tp->root);
  152. struct nlattr *tb[TCA_BASIC_MAX + 1];
  153. struct basic_filter *fold = (struct basic_filter *) *arg;
  154. struct basic_filter *fnew;
  155. if (tca[TCA_OPTIONS] == NULL)
  156. return -EINVAL;
  157. err = nla_parse_nested(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  158. basic_policy, NULL);
  159. if (err < 0)
  160. return err;
  161. if (fold != NULL) {
  162. if (handle && fold->handle != handle)
  163. return -EINVAL;
  164. }
  165. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  166. if (!fnew)
  167. return -ENOBUFS;
  168. err = tcf_exts_init(&fnew->exts, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  169. if (err < 0)
  170. goto errout;
  171. if (!handle) {
  172. handle = 1;
  173. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  174. INT_MAX, GFP_KERNEL);
  175. } else if (!fold) {
  176. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  177. handle, GFP_KERNEL);
  178. }
  179. if (err)
  180. goto errout;
  181. fnew->handle = handle;
  182. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
  183. extack);
  184. if (err < 0) {
  185. if (!fold)
  186. idr_remove(&head->handle_idr, fnew->handle);
  187. goto errout;
  188. }
  189. *arg = fnew;
  190. if (fold) {
  191. idr_replace(&head->handle_idr, fnew, fnew->handle);
  192. list_replace_rcu(&fold->link, &fnew->link);
  193. tcf_unbind_filter(tp, &fold->res);
  194. tcf_exts_get_net(&fold->exts);
  195. tcf_queue_work(&fold->rwork, basic_delete_filter_work);
  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, void *q,
  221. unsigned long base)
  222. {
  223. struct basic_filter *f = fh;
  224. if (f && f->res.classid == classid) {
  225. if (cl)
  226. __tcf_bind_filter(q, &f->res, base);
  227. else
  228. __tcf_unbind_filter(q, &f->res);
  229. }
  230. }
  231. static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
  232. struct sk_buff *skb, struct tcmsg *t)
  233. {
  234. struct basic_filter *f = fh;
  235. struct nlattr *nest;
  236. if (f == NULL)
  237. return skb->len;
  238. t->tcm_handle = f->handle;
  239. nest = nla_nest_start(skb, TCA_OPTIONS);
  240. if (nest == NULL)
  241. goto nla_put_failure;
  242. if (f->res.classid &&
  243. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  244. goto nla_put_failure;
  245. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  246. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  247. goto nla_put_failure;
  248. nla_nest_end(skb, nest);
  249. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  250. goto nla_put_failure;
  251. return skb->len;
  252. nla_put_failure:
  253. nla_nest_cancel(skb, nest);
  254. return -1;
  255. }
  256. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  257. .kind = "basic",
  258. .classify = basic_classify,
  259. .init = basic_init,
  260. .destroy = basic_destroy,
  261. .get = basic_get,
  262. .change = basic_change,
  263. .delete = basic_delete,
  264. .walk = basic_walk,
  265. .dump = basic_dump,
  266. .bind_class = basic_bind_class,
  267. .owner = THIS_MODULE,
  268. };
  269. static int __init init_basic(void)
  270. {
  271. return register_tcf_proto_ops(&cls_basic_ops);
  272. }
  273. static void __exit exit_basic(void)
  274. {
  275. unregister_tcf_proto_ops(&cls_basic_ops);
  276. }
  277. module_init(init_basic)
  278. module_exit(exit_basic)
  279. MODULE_LICENSE("GPL");