act_connmark.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * net/sched/act_connmark.c netfilter connmark retriever action
  4. * skb mark is over-written
  5. *
  6. * Copyright (c) 2011 Felix Fietkau <nbd@openwrt.org>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/pkt_cls.h>
  14. #include <linux/ip.h>
  15. #include <linux/ipv6.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <net/act_api.h>
  19. #include <net/pkt_cls.h>
  20. #include <uapi/linux/tc_act/tc_connmark.h>
  21. #include <net/tc_act/tc_connmark.h>
  22. #include <net/tc_wrapper.h>
  23. #include <net/netfilter/nf_conntrack.h>
  24. #include <net/netfilter/nf_conntrack_core.h>
  25. #include <net/netfilter/nf_conntrack_zones.h>
  26. static struct tc_action_ops act_connmark_ops;
  27. TC_INDIRECT_SCOPE int tcf_connmark_act(struct sk_buff *skb,
  28. const struct tc_action *a,
  29. struct tcf_result *res)
  30. {
  31. const struct nf_conntrack_tuple_hash *thash;
  32. struct nf_conntrack_tuple tuple;
  33. enum ip_conntrack_info ctinfo;
  34. struct tcf_connmark_info *ca = to_connmark(a);
  35. struct tcf_connmark_parms *parms;
  36. struct nf_conntrack_zone zone;
  37. struct nf_conn *c;
  38. int proto;
  39. tcf_lastuse_update(&ca->tcf_tm);
  40. tcf_action_update_bstats(&ca->common, skb);
  41. parms = rcu_dereference_bh(ca->parms);
  42. switch (skb_protocol(skb, true)) {
  43. case htons(ETH_P_IP):
  44. if (skb->len < sizeof(struct iphdr))
  45. goto out;
  46. proto = NFPROTO_IPV4;
  47. break;
  48. case htons(ETH_P_IPV6):
  49. if (skb->len < sizeof(struct ipv6hdr))
  50. goto out;
  51. proto = NFPROTO_IPV6;
  52. break;
  53. default:
  54. goto out;
  55. }
  56. c = nf_ct_get(skb, &ctinfo);
  57. if (c) {
  58. skb->mark = READ_ONCE(c->mark);
  59. goto count;
  60. }
  61. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, parms->net,
  62. &tuple))
  63. goto out;
  64. zone.id = parms->zone;
  65. zone.dir = NF_CT_DEFAULT_ZONE_DIR;
  66. thash = nf_conntrack_find_get(parms->net, &zone, &tuple);
  67. if (!thash)
  68. goto out;
  69. c = nf_ct_tuplehash_to_ctrack(thash);
  70. skb->mark = READ_ONCE(c->mark);
  71. nf_ct_put(c);
  72. count:
  73. /* using overlimits stats to count how many packets marked */
  74. tcf_action_inc_overlimit_qstats(&ca->common);
  75. out:
  76. return parms->action;
  77. }
  78. static const struct nla_policy connmark_policy[TCA_CONNMARK_MAX + 1] = {
  79. [TCA_CONNMARK_PARMS] = { .len = sizeof(struct tc_connmark) },
  80. };
  81. static int tcf_connmark_init(struct net *net, struct nlattr *nla,
  82. struct nlattr *est, struct tc_action **a,
  83. struct tcf_proto *tp, u32 flags,
  84. struct netlink_ext_ack *extack)
  85. {
  86. struct tc_action_net *tn = net_generic(net, act_connmark_ops.net_id);
  87. struct tcf_connmark_parms *nparms, *oparms;
  88. struct nlattr *tb[TCA_CONNMARK_MAX + 1];
  89. bool bind = flags & TCA_ACT_FLAGS_BIND;
  90. struct tcf_chain *goto_ch = NULL;
  91. struct tcf_connmark_info *ci;
  92. struct tc_connmark *parm;
  93. int ret = 0, err;
  94. u32 index;
  95. if (!nla)
  96. return -EINVAL;
  97. ret = nla_parse_nested_deprecated(tb, TCA_CONNMARK_MAX, nla,
  98. connmark_policy, NULL);
  99. if (ret < 0)
  100. return ret;
  101. if (!tb[TCA_CONNMARK_PARMS])
  102. return -EINVAL;
  103. nparms = kzalloc_obj(*nparms);
  104. if (!nparms)
  105. return -ENOMEM;
  106. parm = nla_data(tb[TCA_CONNMARK_PARMS]);
  107. index = parm->index;
  108. ret = tcf_idr_check_alloc(tn, &index, a, bind);
  109. if (!ret) {
  110. ret = tcf_idr_create_from_flags(tn, index, est, a,
  111. &act_connmark_ops, bind, flags);
  112. if (ret) {
  113. tcf_idr_cleanup(tn, index);
  114. err = ret;
  115. goto out_free;
  116. }
  117. ci = to_connmark(*a);
  118. nparms->net = net;
  119. nparms->zone = parm->zone;
  120. ret = ACT_P_CREATED;
  121. } else if (ret > 0) {
  122. ci = to_connmark(*a);
  123. if (bind) {
  124. err = ACT_P_BOUND;
  125. goto out_free;
  126. }
  127. if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  128. err = -EEXIST;
  129. goto release_idr;
  130. }
  131. nparms->net = rtnl_dereference(ci->parms)->net;
  132. nparms->zone = parm->zone;
  133. ret = 0;
  134. } else {
  135. err = ret;
  136. goto out_free;
  137. }
  138. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  139. if (err < 0)
  140. goto release_idr;
  141. nparms->action = parm->action;
  142. spin_lock_bh(&ci->tcf_lock);
  143. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  144. oparms = rcu_replace_pointer(ci->parms, nparms, lockdep_is_held(&ci->tcf_lock));
  145. spin_unlock_bh(&ci->tcf_lock);
  146. if (goto_ch)
  147. tcf_chain_put_by_act(goto_ch);
  148. if (oparms)
  149. kfree_rcu(oparms, rcu);
  150. return ret;
  151. release_idr:
  152. tcf_idr_release(*a, bind);
  153. out_free:
  154. kfree(nparms);
  155. return err;
  156. }
  157. static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  158. int bind, int ref)
  159. {
  160. const struct tcf_connmark_info *ci = to_connmark(a);
  161. unsigned char *b = skb_tail_pointer(skb);
  162. const struct tcf_connmark_parms *parms;
  163. struct tc_connmark opt;
  164. struct tcf_t t;
  165. memset(&opt, 0, sizeof(opt));
  166. opt.index = ci->tcf_index;
  167. opt.refcnt = refcount_read(&ci->tcf_refcnt) - ref;
  168. opt.bindcnt = atomic_read(&ci->tcf_bindcnt) - bind;
  169. rcu_read_lock();
  170. parms = rcu_dereference(ci->parms);
  171. opt.action = parms->action;
  172. opt.zone = parms->zone;
  173. if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
  174. goto nla_put_failure;
  175. tcf_tm_dump(&t, &ci->tcf_tm);
  176. if (nla_put_64bit(skb, TCA_CONNMARK_TM, sizeof(t), &t,
  177. TCA_CONNMARK_PAD))
  178. goto nla_put_failure;
  179. rcu_read_unlock();
  180. return skb->len;
  181. nla_put_failure:
  182. rcu_read_unlock();
  183. nlmsg_trim(skb, b);
  184. return -1;
  185. }
  186. static void tcf_connmark_cleanup(struct tc_action *a)
  187. {
  188. struct tcf_connmark_info *ci = to_connmark(a);
  189. struct tcf_connmark_parms *parms;
  190. parms = rcu_dereference_protected(ci->parms, 1);
  191. if (parms)
  192. kfree_rcu(parms, rcu);
  193. }
  194. static struct tc_action_ops act_connmark_ops = {
  195. .kind = "connmark",
  196. .id = TCA_ID_CONNMARK,
  197. .owner = THIS_MODULE,
  198. .act = tcf_connmark_act,
  199. .dump = tcf_connmark_dump,
  200. .init = tcf_connmark_init,
  201. .cleanup = tcf_connmark_cleanup,
  202. .size = sizeof(struct tcf_connmark_info),
  203. };
  204. MODULE_ALIAS_NET_ACT("connmark");
  205. static __net_init int connmark_init_net(struct net *net)
  206. {
  207. struct tc_action_net *tn = net_generic(net, act_connmark_ops.net_id);
  208. return tc_action_net_init(net, tn, &act_connmark_ops);
  209. }
  210. static void __net_exit connmark_exit_net(struct list_head *net_list)
  211. {
  212. tc_action_net_exit(net_list, act_connmark_ops.net_id);
  213. }
  214. static struct pernet_operations connmark_net_ops = {
  215. .init = connmark_init_net,
  216. .exit_batch = connmark_exit_net,
  217. .id = &act_connmark_ops.net_id,
  218. .size = sizeof(struct tc_action_net),
  219. };
  220. static int __init connmark_init_module(void)
  221. {
  222. return tcf_register_action(&act_connmark_ops, &connmark_net_ops);
  223. }
  224. static void __exit connmark_cleanup_module(void)
  225. {
  226. tcf_unregister_action(&act_connmark_ops, &connmark_net_ops);
  227. }
  228. module_init(connmark_init_module);
  229. module_exit(connmark_cleanup_module);
  230. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  231. MODULE_DESCRIPTION("Connection tracking mark restoring");
  232. MODULE_LICENSE("GPL");