act_connmark.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 READ_ONCE(ca->tcf_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(sizeof(*nparms), GFP_KERNEL);
  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. spin_lock_bh(&ci->tcf_lock);
  142. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  143. oparms = rcu_replace_pointer(ci->parms, nparms, lockdep_is_held(&ci->tcf_lock));
  144. spin_unlock_bh(&ci->tcf_lock);
  145. if (goto_ch)
  146. tcf_chain_put_by_act(goto_ch);
  147. if (oparms)
  148. kfree_rcu(oparms, rcu);
  149. return ret;
  150. release_idr:
  151. tcf_idr_release(*a, bind);
  152. out_free:
  153. kfree(nparms);
  154. return err;
  155. }
  156. static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
  157. int bind, int ref)
  158. {
  159. unsigned char *b = skb_tail_pointer(skb);
  160. struct tcf_connmark_info *ci = to_connmark(a);
  161. struct tc_connmark opt = {
  162. .index = ci->tcf_index,
  163. .refcnt = refcount_read(&ci->tcf_refcnt) - ref,
  164. .bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
  165. };
  166. struct tcf_connmark_parms *parms;
  167. struct tcf_t t;
  168. spin_lock_bh(&ci->tcf_lock);
  169. parms = rcu_dereference_protected(ci->parms, lockdep_is_held(&ci->tcf_lock));
  170. opt.action = ci->tcf_action;
  171. opt.zone = parms->zone;
  172. if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
  173. goto nla_put_failure;
  174. tcf_tm_dump(&t, &ci->tcf_tm);
  175. if (nla_put_64bit(skb, TCA_CONNMARK_TM, sizeof(t), &t,
  176. TCA_CONNMARK_PAD))
  177. goto nla_put_failure;
  178. spin_unlock_bh(&ci->tcf_lock);
  179. return skb->len;
  180. nla_put_failure:
  181. spin_unlock_bh(&ci->tcf_lock);
  182. nlmsg_trim(skb, b);
  183. return -1;
  184. }
  185. static void tcf_connmark_cleanup(struct tc_action *a)
  186. {
  187. struct tcf_connmark_info *ci = to_connmark(a);
  188. struct tcf_connmark_parms *parms;
  189. parms = rcu_dereference_protected(ci->parms, 1);
  190. if (parms)
  191. kfree_rcu(parms, rcu);
  192. }
  193. static struct tc_action_ops act_connmark_ops = {
  194. .kind = "connmark",
  195. .id = TCA_ID_CONNMARK,
  196. .owner = THIS_MODULE,
  197. .act = tcf_connmark_act,
  198. .dump = tcf_connmark_dump,
  199. .init = tcf_connmark_init,
  200. .cleanup = tcf_connmark_cleanup,
  201. .size = sizeof(struct tcf_connmark_info),
  202. };
  203. MODULE_ALIAS_NET_ACT("connmark");
  204. static __net_init int connmark_init_net(struct net *net)
  205. {
  206. struct tc_action_net *tn = net_generic(net, act_connmark_ops.net_id);
  207. return tc_action_net_init(net, tn, &act_connmark_ops);
  208. }
  209. static void __net_exit connmark_exit_net(struct list_head *net_list)
  210. {
  211. tc_action_net_exit(net_list, act_connmark_ops.net_id);
  212. }
  213. static struct pernet_operations connmark_net_ops = {
  214. .init = connmark_init_net,
  215. .exit_batch = connmark_exit_net,
  216. .id = &act_connmark_ops.net_id,
  217. .size = sizeof(struct tc_action_net),
  218. };
  219. static int __init connmark_init_module(void)
  220. {
  221. return tcf_register_action(&act_connmark_ops, &connmark_net_ops);
  222. }
  223. static void __exit connmark_cleanup_module(void)
  224. {
  225. tcf_unregister_action(&act_connmark_ops, &connmark_net_ops);
  226. }
  227. module_init(connmark_init_module);
  228. module_exit(connmark_cleanup_module);
  229. MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  230. MODULE_DESCRIPTION("Connection tracking mark restoring");
  231. MODULE_LICENSE("GPL");