cls_basic.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_basic.c Basic Packet Classifier.
  4. *
  5. * Authors: Thomas Graf <tgraf@suug.ch>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/errno.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/idr.h>
  16. #include <linux/percpu.h>
  17. #include <net/netlink.h>
  18. #include <net/act_api.h>
  19. #include <net/pkt_cls.h>
  20. #include <net/tc_wrapper.h>
  21. struct basic_head {
  22. struct list_head flist;
  23. struct idr handle_idr;
  24. struct rcu_head rcu;
  25. };
  26. struct basic_filter {
  27. u32 handle;
  28. struct tcf_exts exts;
  29. struct tcf_ematch_tree ematches;
  30. struct tcf_result res;
  31. struct tcf_proto *tp;
  32. struct list_head link;
  33. struct tc_basic_pcnt __percpu *pf;
  34. struct rcu_work rwork;
  35. };
  36. TC_INDIRECT_SCOPE int basic_classify(struct sk_buff *skb,
  37. 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. __this_cpu_inc(f->pf->rcnt);
  45. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  46. continue;
  47. __this_cpu_inc(f->pf->rhit);
  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. idr_init(&head->handle_idr);
  75. rcu_assign_pointer(tp->root, head);
  76. return 0;
  77. }
  78. static void __basic_delete_filter(struct basic_filter *f)
  79. {
  80. tcf_exts_destroy(&f->exts);
  81. tcf_em_tree_destroy(&f->ematches);
  82. tcf_exts_put_net(&f->exts);
  83. free_percpu(f->pf);
  84. kfree(f);
  85. }
  86. static void basic_delete_filter_work(struct work_struct *work)
  87. {
  88. struct basic_filter *f = container_of(to_rcu_work(work),
  89. struct basic_filter,
  90. rwork);
  91. rtnl_lock();
  92. __basic_delete_filter(f);
  93. rtnl_unlock();
  94. }
  95. static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
  96. struct netlink_ext_ack *extack)
  97. {
  98. struct basic_head *head = rtnl_dereference(tp->root);
  99. struct basic_filter *f, *n;
  100. list_for_each_entry_safe(f, n, &head->flist, link) {
  101. list_del_rcu(&f->link);
  102. tcf_unbind_filter(tp, &f->res);
  103. idr_remove(&head->handle_idr, f->handle);
  104. if (tcf_exts_get_net(&f->exts))
  105. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  106. else
  107. __basic_delete_filter(f);
  108. }
  109. idr_destroy(&head->handle_idr);
  110. kfree_rcu(head, rcu);
  111. }
  112. static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
  113. bool rtnl_held, struct netlink_ext_ack *extack)
  114. {
  115. struct basic_head *head = rtnl_dereference(tp->root);
  116. struct basic_filter *f = arg;
  117. list_del_rcu(&f->link);
  118. tcf_unbind_filter(tp, &f->res);
  119. idr_remove(&head->handle_idr, f->handle);
  120. tcf_exts_get_net(&f->exts);
  121. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  122. *last = list_empty(&head->flist);
  123. return 0;
  124. }
  125. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  126. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  127. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  128. };
  129. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  130. struct basic_filter *f, unsigned long base,
  131. struct nlattr **tb,
  132. struct nlattr *est, u32 flags,
  133. struct netlink_ext_ack *extack)
  134. {
  135. int err;
  136. err = tcf_exts_validate(net, tp, tb, est, &f->exts, flags, extack);
  137. if (err < 0)
  138. return err;
  139. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
  140. if (err < 0)
  141. return err;
  142. if (tb[TCA_BASIC_CLASSID]) {
  143. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  144. tcf_bind_filter(tp, &f->res, base);
  145. }
  146. f->tp = tp;
  147. return 0;
  148. }
  149. static int basic_change(struct net *net, struct sk_buff *in_skb,
  150. struct tcf_proto *tp, unsigned long base, u32 handle,
  151. struct nlattr **tca, void **arg,
  152. u32 flags, struct netlink_ext_ack *extack)
  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. if (tca[TCA_OPTIONS] == NULL)
  160. return -EINVAL;
  161. err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  162. basic_policy, NULL);
  163. if (err < 0)
  164. return err;
  165. if (fold != NULL) {
  166. if (handle && fold->handle != handle)
  167. return -EINVAL;
  168. }
  169. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  170. if (!fnew)
  171. return -ENOBUFS;
  172. err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  173. if (err < 0)
  174. goto errout;
  175. if (!handle) {
  176. handle = 1;
  177. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  178. INT_MAX, GFP_KERNEL);
  179. } else if (!fold) {
  180. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  181. handle, GFP_KERNEL);
  182. }
  183. if (err)
  184. goto errout;
  185. fnew->handle = handle;
  186. fnew->pf = alloc_percpu(struct tc_basic_pcnt);
  187. if (!fnew->pf) {
  188. err = -ENOMEM;
  189. goto errout;
  190. }
  191. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], flags,
  192. extack);
  193. if (err < 0) {
  194. if (!fold)
  195. idr_remove(&head->handle_idr, fnew->handle);
  196. goto errout;
  197. }
  198. *arg = fnew;
  199. if (fold) {
  200. idr_replace(&head->handle_idr, fnew, fnew->handle);
  201. list_replace_rcu(&fold->link, &fnew->link);
  202. tcf_unbind_filter(tp, &fold->res);
  203. tcf_exts_get_net(&fold->exts);
  204. tcf_queue_work(&fold->rwork, basic_delete_filter_work);
  205. } else {
  206. list_add_rcu(&fnew->link, &head->flist);
  207. }
  208. return 0;
  209. errout:
  210. free_percpu(fnew->pf);
  211. tcf_exts_destroy(&fnew->exts);
  212. kfree(fnew);
  213. return err;
  214. }
  215. static void basic_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  216. bool rtnl_held)
  217. {
  218. struct basic_head *head = rtnl_dereference(tp->root);
  219. struct basic_filter *f;
  220. list_for_each_entry(f, &head->flist, link) {
  221. if (!tc_cls_stats_dump(tp, arg, f))
  222. break;
  223. }
  224. }
  225. static void basic_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  226. unsigned long base)
  227. {
  228. struct basic_filter *f = fh;
  229. tc_cls_bind_class(classid, cl, q, &f->res, base);
  230. }
  231. static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
  232. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  233. {
  234. struct tc_basic_pcnt gpf = {};
  235. struct basic_filter *f = fh;
  236. struct nlattr *nest;
  237. int cpu;
  238. if (f == NULL)
  239. return skb->len;
  240. t->tcm_handle = f->handle;
  241. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  242. if (nest == NULL)
  243. goto nla_put_failure;
  244. if (f->res.classid &&
  245. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  246. goto nla_put_failure;
  247. for_each_possible_cpu(cpu) {
  248. struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
  249. gpf.rcnt += pf->rcnt;
  250. gpf.rhit += pf->rhit;
  251. }
  252. if (nla_put_64bit(skb, TCA_BASIC_PCNT,
  253. sizeof(struct tc_basic_pcnt),
  254. &gpf, TCA_BASIC_PAD))
  255. goto nla_put_failure;
  256. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  257. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  258. goto nla_put_failure;
  259. nla_nest_end(skb, nest);
  260. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  261. goto nla_put_failure;
  262. return skb->len;
  263. nla_put_failure:
  264. nla_nest_cancel(skb, nest);
  265. return -1;
  266. }
  267. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  268. .kind = "basic",
  269. .classify = basic_classify,
  270. .init = basic_init,
  271. .destroy = basic_destroy,
  272. .get = basic_get,
  273. .change = basic_change,
  274. .delete = basic_delete,
  275. .walk = basic_walk,
  276. .dump = basic_dump,
  277. .bind_class = basic_bind_class,
  278. .owner = THIS_MODULE,
  279. };
  280. MODULE_ALIAS_NET_CLS("basic");
  281. static int __init init_basic(void)
  282. {
  283. return register_tcf_proto_ops(&cls_basic_ops);
  284. }
  285. static void __exit exit_basic(void)
  286. {
  287. unregister_tcf_proto_ops(&cls_basic_ops);
  288. }
  289. module_init(init_basic)
  290. module_exit(exit_basic)
  291. MODULE_DESCRIPTION("TC basic classifier");
  292. MODULE_LICENSE("GPL");