cls_cgroup.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/cls_cgroup.c Control Group Classifier
  4. *
  5. * Authors: Thomas Graf <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/rcupdate.h>
  11. #include <net/rtnetlink.h>
  12. #include <net/pkt_cls.h>
  13. #include <net/sock.h>
  14. #include <net/cls_cgroup.h>
  15. struct cls_cgroup_head {
  16. u32 handle;
  17. struct tcf_exts exts;
  18. struct tcf_ematch_tree ematches;
  19. struct tcf_proto *tp;
  20. struct rcu_work rwork;
  21. };
  22. static int cls_cgroup_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  23. struct tcf_result *res)
  24. {
  25. struct cls_cgroup_head *head = rcu_dereference_bh(tp->root);
  26. u32 classid = task_get_classid(skb);
  27. if (unlikely(!head))
  28. return -1;
  29. if (!classid)
  30. return -1;
  31. if (!tcf_em_tree_match(skb, &head->ematches, NULL))
  32. return -1;
  33. res->classid = classid;
  34. res->class = 0;
  35. return tcf_exts_exec(skb, &head->exts, res);
  36. }
  37. static void *cls_cgroup_get(struct tcf_proto *tp, u32 handle)
  38. {
  39. return NULL;
  40. }
  41. static int cls_cgroup_init(struct tcf_proto *tp)
  42. {
  43. return 0;
  44. }
  45. static const struct nla_policy cgroup_policy[TCA_CGROUP_MAX + 1] = {
  46. [TCA_CGROUP_EMATCHES] = { .type = NLA_NESTED },
  47. };
  48. static void __cls_cgroup_destroy(struct cls_cgroup_head *head)
  49. {
  50. tcf_exts_destroy(&head->exts);
  51. tcf_em_tree_destroy(&head->ematches);
  52. tcf_exts_put_net(&head->exts);
  53. kfree(head);
  54. }
  55. static void cls_cgroup_destroy_work(struct work_struct *work)
  56. {
  57. struct cls_cgroup_head *head = container_of(to_rcu_work(work),
  58. struct cls_cgroup_head,
  59. rwork);
  60. rtnl_lock();
  61. __cls_cgroup_destroy(head);
  62. rtnl_unlock();
  63. }
  64. static int cls_cgroup_change(struct net *net, struct sk_buff *in_skb,
  65. struct tcf_proto *tp, unsigned long base,
  66. u32 handle, struct nlattr **tca,
  67. void **arg, u32 flags,
  68. struct netlink_ext_ack *extack)
  69. {
  70. struct nlattr *tb[TCA_CGROUP_MAX + 1];
  71. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  72. struct cls_cgroup_head *new;
  73. int err;
  74. if (!tca[TCA_OPTIONS])
  75. return -EINVAL;
  76. if (!head && !handle)
  77. return -EINVAL;
  78. if (head && handle != head->handle)
  79. return -ENOENT;
  80. new = kzalloc(sizeof(*head), GFP_KERNEL);
  81. if (!new)
  82. return -ENOBUFS;
  83. err = tcf_exts_init(&new->exts, net, TCA_CGROUP_ACT, TCA_CGROUP_POLICE);
  84. if (err < 0)
  85. goto errout;
  86. new->handle = handle;
  87. new->tp = tp;
  88. err = nla_parse_nested_deprecated(tb, TCA_CGROUP_MAX,
  89. tca[TCA_OPTIONS], cgroup_policy,
  90. NULL);
  91. if (err < 0)
  92. goto errout;
  93. err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &new->exts, flags,
  94. extack);
  95. if (err < 0)
  96. goto errout;
  97. err = tcf_em_tree_validate(tp, tb[TCA_CGROUP_EMATCHES], &new->ematches);
  98. if (err < 0)
  99. goto errout;
  100. rcu_assign_pointer(tp->root, new);
  101. if (head) {
  102. tcf_exts_get_net(&head->exts);
  103. tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
  104. }
  105. return 0;
  106. errout:
  107. tcf_exts_destroy(&new->exts);
  108. kfree(new);
  109. return err;
  110. }
  111. static void cls_cgroup_destroy(struct tcf_proto *tp, bool rtnl_held,
  112. struct netlink_ext_ack *extack)
  113. {
  114. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  115. /* Head can still be NULL due to cls_cgroup_init(). */
  116. if (head) {
  117. if (tcf_exts_get_net(&head->exts))
  118. tcf_queue_work(&head->rwork, cls_cgroup_destroy_work);
  119. else
  120. __cls_cgroup_destroy(head);
  121. }
  122. }
  123. static int cls_cgroup_delete(struct tcf_proto *tp, void *arg, bool *last,
  124. bool rtnl_held, struct netlink_ext_ack *extack)
  125. {
  126. return -EOPNOTSUPP;
  127. }
  128. static void cls_cgroup_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  129. bool rtnl_held)
  130. {
  131. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  132. if (arg->count < arg->skip)
  133. goto skip;
  134. if (!head)
  135. return;
  136. if (arg->fn(tp, head, arg) < 0) {
  137. arg->stop = 1;
  138. return;
  139. }
  140. skip:
  141. arg->count++;
  142. }
  143. static int cls_cgroup_dump(struct net *net, struct tcf_proto *tp, void *fh,
  144. struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
  145. {
  146. struct cls_cgroup_head *head = rtnl_dereference(tp->root);
  147. struct nlattr *nest;
  148. t->tcm_handle = head->handle;
  149. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  150. if (nest == NULL)
  151. goto nla_put_failure;
  152. if (tcf_exts_dump(skb, &head->exts) < 0 ||
  153. tcf_em_tree_dump(skb, &head->ematches, TCA_CGROUP_EMATCHES) < 0)
  154. goto nla_put_failure;
  155. nla_nest_end(skb, nest);
  156. if (tcf_exts_dump_stats(skb, &head->exts) < 0)
  157. goto nla_put_failure;
  158. return skb->len;
  159. nla_put_failure:
  160. nla_nest_cancel(skb, nest);
  161. return -1;
  162. }
  163. static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
  164. .kind = "cgroup",
  165. .init = cls_cgroup_init,
  166. .change = cls_cgroup_change,
  167. .classify = cls_cgroup_classify,
  168. .destroy = cls_cgroup_destroy,
  169. .get = cls_cgroup_get,
  170. .delete = cls_cgroup_delete,
  171. .walk = cls_cgroup_walk,
  172. .dump = cls_cgroup_dump,
  173. .owner = THIS_MODULE,
  174. };
  175. static int __init init_cgroup_cls(void)
  176. {
  177. return register_tcf_proto_ops(&cls_cgroup_ops);
  178. }
  179. static void __exit exit_cgroup_cls(void)
  180. {
  181. unregister_tcf_proto_ops(&cls_cgroup_ops);
  182. }
  183. module_init(init_cgroup_cls);
  184. module_exit(exit_cgroup_cls);
  185. MODULE_LICENSE("GPL");