cls_cgroup.c 5.1 KB

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