cls_basic.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. struct basic_head {
  21. struct list_head flist;
  22. struct idr handle_idr;
  23. struct rcu_head rcu;
  24. };
  25. struct basic_filter {
  26. u32 handle;
  27. struct tcf_exts exts;
  28. struct tcf_ematch_tree ematches;
  29. struct tcf_result res;
  30. struct tcf_proto *tp;
  31. struct list_head link;
  32. struct tc_basic_pcnt __percpu *pf;
  33. struct rcu_work rwork;
  34. };
  35. static int basic_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  36. struct tcf_result *res)
  37. {
  38. int r;
  39. struct basic_head *head = rcu_dereference_bh(tp->root);
  40. struct basic_filter *f;
  41. list_for_each_entry_rcu(f, &head->flist, link) {
  42. __this_cpu_inc(f->pf->rcnt);
  43. if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  44. continue;
  45. __this_cpu_inc(f->pf->rhit);
  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. free_percpu(f->pf);
  82. kfree(f);
  83. }
  84. static void basic_delete_filter_work(struct work_struct *work)
  85. {
  86. struct basic_filter *f = container_of(to_rcu_work(work),
  87. struct basic_filter,
  88. rwork);
  89. rtnl_lock();
  90. __basic_delete_filter(f);
  91. rtnl_unlock();
  92. }
  93. static void basic_destroy(struct tcf_proto *tp, bool rtnl_held,
  94. struct netlink_ext_ack *extack)
  95. {
  96. struct basic_head *head = rtnl_dereference(tp->root);
  97. struct basic_filter *f, *n;
  98. list_for_each_entry_safe(f, n, &head->flist, link) {
  99. list_del_rcu(&f->link);
  100. tcf_unbind_filter(tp, &f->res);
  101. idr_remove(&head->handle_idr, f->handle);
  102. if (tcf_exts_get_net(&f->exts))
  103. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  104. else
  105. __basic_delete_filter(f);
  106. }
  107. idr_destroy(&head->handle_idr);
  108. kfree_rcu(head, rcu);
  109. }
  110. static int basic_delete(struct tcf_proto *tp, void *arg, bool *last,
  111. bool rtnl_held, struct netlink_ext_ack *extack)
  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. idr_remove(&head->handle_idr, f->handle);
  118. tcf_exts_get_net(&f->exts);
  119. tcf_queue_work(&f->rwork, basic_delete_filter_work);
  120. *last = list_empty(&head->flist);
  121. return 0;
  122. }
  123. static const struct nla_policy basic_policy[TCA_BASIC_MAX + 1] = {
  124. [TCA_BASIC_CLASSID] = { .type = NLA_U32 },
  125. [TCA_BASIC_EMATCHES] = { .type = NLA_NESTED },
  126. };
  127. static int basic_set_parms(struct net *net, struct tcf_proto *tp,
  128. struct basic_filter *f, unsigned long base,
  129. struct nlattr **tb,
  130. struct nlattr *est, bool ovr,
  131. struct netlink_ext_ack *extack)
  132. {
  133. int err;
  134. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, extack);
  135. if (err < 0)
  136. return err;
  137. err = tcf_em_tree_validate(tp, tb[TCA_BASIC_EMATCHES], &f->ematches);
  138. if (err < 0)
  139. return err;
  140. if (tb[TCA_BASIC_CLASSID]) {
  141. f->res.classid = nla_get_u32(tb[TCA_BASIC_CLASSID]);
  142. tcf_bind_filter(tp, &f->res, base);
  143. }
  144. f->tp = tp;
  145. return 0;
  146. }
  147. static int basic_change(struct net *net, struct sk_buff *in_skb,
  148. struct tcf_proto *tp, unsigned long base, u32 handle,
  149. struct nlattr **tca, void **arg, bool ovr,
  150. bool rtnl_held, struct netlink_ext_ack *extack)
  151. {
  152. int err;
  153. struct basic_head *head = rtnl_dereference(tp->root);
  154. struct nlattr *tb[TCA_BASIC_MAX + 1];
  155. struct basic_filter *fold = (struct basic_filter *) *arg;
  156. struct basic_filter *fnew;
  157. if (tca[TCA_OPTIONS] == NULL)
  158. return -EINVAL;
  159. err = nla_parse_nested_deprecated(tb, TCA_BASIC_MAX, tca[TCA_OPTIONS],
  160. basic_policy, NULL);
  161. if (err < 0)
  162. return err;
  163. if (fold != NULL) {
  164. if (handle && fold->handle != handle)
  165. return -EINVAL;
  166. }
  167. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  168. if (!fnew)
  169. return -ENOBUFS;
  170. err = tcf_exts_init(&fnew->exts, net, TCA_BASIC_ACT, TCA_BASIC_POLICE);
  171. if (err < 0)
  172. goto errout;
  173. if (!handle) {
  174. handle = 1;
  175. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  176. INT_MAX, GFP_KERNEL);
  177. } else if (!fold) {
  178. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  179. handle, GFP_KERNEL);
  180. }
  181. if (err)
  182. goto errout;
  183. fnew->handle = handle;
  184. fnew->pf = alloc_percpu(struct tc_basic_pcnt);
  185. if (!fnew->pf) {
  186. err = -ENOMEM;
  187. goto errout;
  188. }
  189. err = basic_set_parms(net, tp, fnew, base, tb, tca[TCA_RATE], ovr,
  190. extack);
  191. if (err < 0) {
  192. if (!fold)
  193. idr_remove(&head->handle_idr, fnew->handle);
  194. goto errout;
  195. }
  196. *arg = fnew;
  197. if (fold) {
  198. idr_replace(&head->handle_idr, fnew, fnew->handle);
  199. list_replace_rcu(&fold->link, &fnew->link);
  200. tcf_unbind_filter(tp, &fold->res);
  201. tcf_exts_get_net(&fold->exts);
  202. tcf_queue_work(&fold->rwork, basic_delete_filter_work);
  203. } else {
  204. list_add_rcu(&fnew->link, &head->flist);
  205. }
  206. return 0;
  207. errout:
  208. free_percpu(fnew->pf);
  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. bool rtnl_held)
  215. {
  216. struct basic_head *head = rtnl_dereference(tp->root);
  217. struct basic_filter *f;
  218. list_for_each_entry(f, &head->flist, link) {
  219. if (arg->count < arg->skip)
  220. goto skip;
  221. if (arg->fn(tp, f, arg) < 0) {
  222. arg->stop = 1;
  223. break;
  224. }
  225. skip:
  226. arg->count++;
  227. }
  228. }
  229. static void basic_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  230. unsigned long base)
  231. {
  232. struct basic_filter *f = fh;
  233. if (f && f->res.classid == classid) {
  234. if (cl)
  235. __tcf_bind_filter(q, &f->res, base);
  236. else
  237. __tcf_unbind_filter(q, &f->res);
  238. }
  239. }
  240. static int basic_dump(struct net *net, struct tcf_proto *tp, void *fh,
  241. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  242. {
  243. struct tc_basic_pcnt gpf = {};
  244. struct basic_filter *f = fh;
  245. struct nlattr *nest;
  246. int cpu;
  247. if (f == NULL)
  248. return skb->len;
  249. t->tcm_handle = f->handle;
  250. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  251. if (nest == NULL)
  252. goto nla_put_failure;
  253. if (f->res.classid &&
  254. nla_put_u32(skb, TCA_BASIC_CLASSID, f->res.classid))
  255. goto nla_put_failure;
  256. for_each_possible_cpu(cpu) {
  257. struct tc_basic_pcnt *pf = per_cpu_ptr(f->pf, cpu);
  258. gpf.rcnt += pf->rcnt;
  259. gpf.rhit += pf->rhit;
  260. }
  261. if (nla_put_64bit(skb, TCA_BASIC_PCNT,
  262. sizeof(struct tc_basic_pcnt),
  263. &gpf, TCA_BASIC_PAD))
  264. goto nla_put_failure;
  265. if (tcf_exts_dump(skb, &f->exts) < 0 ||
  266. tcf_em_tree_dump(skb, &f->ematches, TCA_BASIC_EMATCHES) < 0)
  267. goto nla_put_failure;
  268. nla_nest_end(skb, nest);
  269. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  270. goto nla_put_failure;
  271. return skb->len;
  272. nla_put_failure:
  273. nla_nest_cancel(skb, nest);
  274. return -1;
  275. }
  276. static struct tcf_proto_ops cls_basic_ops __read_mostly = {
  277. .kind = "basic",
  278. .classify = basic_classify,
  279. .init = basic_init,
  280. .destroy = basic_destroy,
  281. .get = basic_get,
  282. .change = basic_change,
  283. .delete = basic_delete,
  284. .walk = basic_walk,
  285. .dump = basic_dump,
  286. .bind_class = basic_bind_class,
  287. .owner = THIS_MODULE,
  288. };
  289. static int __init init_basic(void)
  290. {
  291. return register_tcf_proto_ops(&cls_basic_ops);
  292. }
  293. static void __exit exit_basic(void)
  294. {
  295. unregister_tcf_proto_ops(&cls_basic_ops);
  296. }
  297. module_init(init_basic)
  298. module_exit(exit_basic)
  299. MODULE_LICENSE("GPL");