act_ctinfo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* net/sched/act_ctinfo.c netfilter ctinfo connmark actions
  3. *
  4. * Copyright (c) 2019 Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/rtnetlink.h>
  11. #include <linux/pkt_cls.h>
  12. #include <linux/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <net/netlink.h>
  15. #include <net/pkt_sched.h>
  16. #include <net/act_api.h>
  17. #include <net/pkt_cls.h>
  18. #include <net/inet_ecn.h>
  19. #include <uapi/linux/tc_act/tc_ctinfo.h>
  20. #include <net/tc_act/tc_ctinfo.h>
  21. #include <net/tc_wrapper.h>
  22. #include <net/netfilter/nf_conntrack.h>
  23. #include <net/netfilter/nf_conntrack_core.h>
  24. #include <net/netfilter/nf_conntrack_ecache.h>
  25. #include <net/netfilter/nf_conntrack_zones.h>
  26. static struct tc_action_ops act_ctinfo_ops;
  27. static void tcf_ctinfo_dscp_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
  28. struct tcf_ctinfo_params *cp,
  29. struct sk_buff *skb, int wlen, int proto)
  30. {
  31. u8 dscp, newdscp;
  32. newdscp = (((READ_ONCE(ct->mark) & cp->dscpmask) >> cp->dscpmaskshift) << 2) &
  33. ~INET_ECN_MASK;
  34. switch (proto) {
  35. case NFPROTO_IPV4:
  36. dscp = ipv4_get_dsfield(ip_hdr(skb)) & ~INET_ECN_MASK;
  37. if (dscp != newdscp) {
  38. if (likely(!skb_try_make_writable(skb, wlen))) {
  39. ipv4_change_dsfield(ip_hdr(skb),
  40. INET_ECN_MASK,
  41. newdscp);
  42. atomic64_inc(&ca->stats_dscp_set);
  43. } else {
  44. atomic64_inc(&ca->stats_dscp_error);
  45. }
  46. }
  47. break;
  48. case NFPROTO_IPV6:
  49. dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & ~INET_ECN_MASK;
  50. if (dscp != newdscp) {
  51. if (likely(!skb_try_make_writable(skb, wlen))) {
  52. ipv6_change_dsfield(ipv6_hdr(skb),
  53. INET_ECN_MASK,
  54. newdscp);
  55. atomic64_inc(&ca->stats_dscp_set);
  56. } else {
  57. atomic64_inc(&ca->stats_dscp_error);
  58. }
  59. }
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. static void tcf_ctinfo_cpmark_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
  66. struct tcf_ctinfo_params *cp,
  67. struct sk_buff *skb)
  68. {
  69. atomic64_inc(&ca->stats_cpmark_set);
  70. skb->mark = READ_ONCE(ct->mark) & cp->cpmarkmask;
  71. }
  72. TC_INDIRECT_SCOPE int tcf_ctinfo_act(struct sk_buff *skb,
  73. const struct tc_action *a,
  74. struct tcf_result *res)
  75. {
  76. const struct nf_conntrack_tuple_hash *thash = NULL;
  77. struct tcf_ctinfo *ca = to_ctinfo(a);
  78. struct nf_conntrack_tuple tuple;
  79. struct nf_conntrack_zone zone;
  80. enum ip_conntrack_info ctinfo;
  81. struct tcf_ctinfo_params *cp;
  82. struct nf_conn *ct;
  83. int proto, wlen;
  84. cp = rcu_dereference_bh(ca->params);
  85. tcf_lastuse_update(&ca->tcf_tm);
  86. tcf_action_update_bstats(&ca->common, skb);
  87. wlen = skb_network_offset(skb);
  88. switch (skb_protocol(skb, true)) {
  89. case htons(ETH_P_IP):
  90. wlen += sizeof(struct iphdr);
  91. if (!pskb_may_pull(skb, wlen))
  92. goto out;
  93. proto = NFPROTO_IPV4;
  94. break;
  95. case htons(ETH_P_IPV6):
  96. wlen += sizeof(struct ipv6hdr);
  97. if (!pskb_may_pull(skb, wlen))
  98. goto out;
  99. proto = NFPROTO_IPV6;
  100. break;
  101. default:
  102. goto out;
  103. }
  104. ct = nf_ct_get(skb, &ctinfo);
  105. if (!ct) { /* look harder, usually ingress */
  106. if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
  107. proto, cp->net, &tuple))
  108. goto out;
  109. zone.id = cp->zone;
  110. zone.dir = NF_CT_DEFAULT_ZONE_DIR;
  111. thash = nf_conntrack_find_get(cp->net, &zone, &tuple);
  112. if (!thash)
  113. goto out;
  114. ct = nf_ct_tuplehash_to_ctrack(thash);
  115. }
  116. if (cp->mode & CTINFO_MODE_DSCP)
  117. if (!cp->dscpstatemask || (READ_ONCE(ct->mark) & cp->dscpstatemask))
  118. tcf_ctinfo_dscp_set(ct, ca, cp, skb, wlen, proto);
  119. if (cp->mode & CTINFO_MODE_CPMARK)
  120. tcf_ctinfo_cpmark_set(ct, ca, cp, skb);
  121. if (thash)
  122. nf_ct_put(ct);
  123. out:
  124. return cp->action;
  125. }
  126. static const struct nla_policy ctinfo_policy[TCA_CTINFO_MAX + 1] = {
  127. [TCA_CTINFO_ACT] =
  128. NLA_POLICY_EXACT_LEN(sizeof(struct tc_ctinfo)),
  129. [TCA_CTINFO_ZONE] = { .type = NLA_U16 },
  130. [TCA_CTINFO_PARMS_DSCP_MASK] = { .type = NLA_U32 },
  131. [TCA_CTINFO_PARMS_DSCP_STATEMASK] = { .type = NLA_U32 },
  132. [TCA_CTINFO_PARMS_CPMARK_MASK] = { .type = NLA_U32 },
  133. };
  134. static int tcf_ctinfo_init(struct net *net, struct nlattr *nla,
  135. struct nlattr *est, struct tc_action **a,
  136. struct tcf_proto *tp, u32 flags,
  137. struct netlink_ext_ack *extack)
  138. {
  139. struct tc_action_net *tn = net_generic(net, act_ctinfo_ops.net_id);
  140. bool bind = flags & TCA_ACT_FLAGS_BIND;
  141. u32 dscpmask = 0, dscpstatemask, index;
  142. struct nlattr *tb[TCA_CTINFO_MAX + 1];
  143. struct tcf_ctinfo_params *cp_new;
  144. struct tcf_chain *goto_ch = NULL;
  145. struct tc_ctinfo *actparm;
  146. struct tcf_ctinfo *ci;
  147. u8 dscpmaskshift;
  148. int ret = 0, err;
  149. if (!nla) {
  150. NL_SET_ERR_MSG_MOD(extack, "ctinfo requires attributes to be passed");
  151. return -EINVAL;
  152. }
  153. err = nla_parse_nested(tb, TCA_CTINFO_MAX, nla, ctinfo_policy, extack);
  154. if (err < 0)
  155. return err;
  156. if (!tb[TCA_CTINFO_ACT]) {
  157. NL_SET_ERR_MSG_MOD(extack,
  158. "Missing required TCA_CTINFO_ACT attribute");
  159. return -EINVAL;
  160. }
  161. actparm = nla_data(tb[TCA_CTINFO_ACT]);
  162. /* do some basic validation here before dynamically allocating things */
  163. /* that we would otherwise have to clean up. */
  164. if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
  165. dscpmask = nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_MASK]);
  166. /* need contiguous 6 bit mask */
  167. dscpmaskshift = dscpmask ? __ffs(dscpmask) : 0;
  168. if ((~0 & (dscpmask >> dscpmaskshift)) != 0x3f) {
  169. NL_SET_ERR_MSG_ATTR(extack,
  170. tb[TCA_CTINFO_PARMS_DSCP_MASK],
  171. "dscp mask must be 6 contiguous bits");
  172. return -EINVAL;
  173. }
  174. dscpstatemask =
  175. nla_get_u32_default(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK],
  176. 0);
  177. /* mask & statemask must not overlap */
  178. if (dscpmask & dscpstatemask) {
  179. NL_SET_ERR_MSG_ATTR(extack,
  180. tb[TCA_CTINFO_PARMS_DSCP_STATEMASK],
  181. "dscp statemask must not overlap dscp mask");
  182. return -EINVAL;
  183. }
  184. }
  185. /* done the validation:now to the actual action allocation */
  186. index = actparm->index;
  187. err = tcf_idr_check_alloc(tn, &index, a, bind);
  188. if (!err) {
  189. ret = tcf_idr_create_from_flags(tn, index, est, a,
  190. &act_ctinfo_ops, bind, flags);
  191. if (ret) {
  192. tcf_idr_cleanup(tn, index);
  193. return ret;
  194. }
  195. ret = ACT_P_CREATED;
  196. } else if (err > 0) {
  197. if (bind) /* don't override defaults */
  198. return ACT_P_BOUND;
  199. if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  200. tcf_idr_release(*a, bind);
  201. return -EEXIST;
  202. }
  203. } else {
  204. return err;
  205. }
  206. err = tcf_action_check_ctrlact(actparm->action, tp, &goto_ch, extack);
  207. if (err < 0)
  208. goto release_idr;
  209. ci = to_ctinfo(*a);
  210. cp_new = kzalloc_obj(*cp_new);
  211. if (unlikely(!cp_new)) {
  212. err = -ENOMEM;
  213. goto put_chain;
  214. }
  215. cp_new->net = net;
  216. cp_new->zone = nla_get_u16_default(tb[TCA_CTINFO_ZONE], 0);
  217. if (dscpmask) {
  218. cp_new->dscpmask = dscpmask;
  219. cp_new->dscpmaskshift = dscpmaskshift;
  220. cp_new->dscpstatemask = dscpstatemask;
  221. cp_new->mode |= CTINFO_MODE_DSCP;
  222. }
  223. if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) {
  224. cp_new->cpmarkmask =
  225. nla_get_u32(tb[TCA_CTINFO_PARMS_CPMARK_MASK]);
  226. cp_new->mode |= CTINFO_MODE_CPMARK;
  227. }
  228. cp_new->action = actparm->action;
  229. spin_lock_bh(&ci->tcf_lock);
  230. goto_ch = tcf_action_set_ctrlact(*a, actparm->action, goto_ch);
  231. cp_new = rcu_replace_pointer(ci->params, cp_new,
  232. lockdep_is_held(&ci->tcf_lock));
  233. spin_unlock_bh(&ci->tcf_lock);
  234. if (goto_ch)
  235. tcf_chain_put_by_act(goto_ch);
  236. if (cp_new)
  237. kfree_rcu(cp_new, rcu);
  238. return ret;
  239. put_chain:
  240. if (goto_ch)
  241. tcf_chain_put_by_act(goto_ch);
  242. release_idr:
  243. tcf_idr_release(*a, bind);
  244. return err;
  245. }
  246. static int tcf_ctinfo_dump(struct sk_buff *skb, struct tc_action *a,
  247. int bind, int ref)
  248. {
  249. const struct tcf_ctinfo *ci = to_ctinfo(a);
  250. unsigned char *b = skb_tail_pointer(skb);
  251. const struct tcf_ctinfo_params *cp;
  252. struct tc_ctinfo opt = {
  253. .index = ci->tcf_index,
  254. .refcnt = refcount_read(&ci->tcf_refcnt) - ref,
  255. .bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
  256. };
  257. struct tcf_t t;
  258. rcu_read_lock();
  259. cp = rcu_dereference(ci->params);
  260. tcf_tm_dump(&t, &ci->tcf_tm);
  261. if (nla_put_64bit(skb, TCA_CTINFO_TM, sizeof(t), &t, TCA_CTINFO_PAD))
  262. goto nla_put_failure;
  263. opt.action = cp->action;
  264. if (nla_put(skb, TCA_CTINFO_ACT, sizeof(opt), &opt))
  265. goto nla_put_failure;
  266. if (nla_put_u16(skb, TCA_CTINFO_ZONE, cp->zone))
  267. goto nla_put_failure;
  268. if (cp->mode & CTINFO_MODE_DSCP) {
  269. if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_MASK,
  270. cp->dscpmask))
  271. goto nla_put_failure;
  272. if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_STATEMASK,
  273. cp->dscpstatemask))
  274. goto nla_put_failure;
  275. }
  276. if (cp->mode & CTINFO_MODE_CPMARK) {
  277. if (nla_put_u32(skb, TCA_CTINFO_PARMS_CPMARK_MASK,
  278. cp->cpmarkmask))
  279. goto nla_put_failure;
  280. }
  281. if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_SET,
  282. atomic64_read(&ci->stats_dscp_set),
  283. TCA_CTINFO_PAD))
  284. goto nla_put_failure;
  285. if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_ERROR,
  286. atomic64_read(&ci->stats_dscp_error),
  287. TCA_CTINFO_PAD))
  288. goto nla_put_failure;
  289. if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_CPMARK_SET,
  290. atomic64_read(&ci->stats_cpmark_set),
  291. TCA_CTINFO_PAD))
  292. goto nla_put_failure;
  293. rcu_read_unlock();
  294. return skb->len;
  295. nla_put_failure:
  296. rcu_read_unlock();
  297. nlmsg_trim(skb, b);
  298. return -1;
  299. }
  300. static void tcf_ctinfo_cleanup(struct tc_action *a)
  301. {
  302. struct tcf_ctinfo *ci = to_ctinfo(a);
  303. struct tcf_ctinfo_params *cp;
  304. cp = rcu_dereference_protected(ci->params, 1);
  305. if (cp)
  306. kfree_rcu(cp, rcu);
  307. }
  308. static struct tc_action_ops act_ctinfo_ops = {
  309. .kind = "ctinfo",
  310. .id = TCA_ID_CTINFO,
  311. .owner = THIS_MODULE,
  312. .act = tcf_ctinfo_act,
  313. .dump = tcf_ctinfo_dump,
  314. .init = tcf_ctinfo_init,
  315. .cleanup= tcf_ctinfo_cleanup,
  316. .size = sizeof(struct tcf_ctinfo),
  317. };
  318. MODULE_ALIAS_NET_ACT("ctinfo");
  319. static __net_init int ctinfo_init_net(struct net *net)
  320. {
  321. struct tc_action_net *tn = net_generic(net, act_ctinfo_ops.net_id);
  322. return tc_action_net_init(net, tn, &act_ctinfo_ops);
  323. }
  324. static void __net_exit ctinfo_exit_net(struct list_head *net_list)
  325. {
  326. tc_action_net_exit(net_list, act_ctinfo_ops.net_id);
  327. }
  328. static struct pernet_operations ctinfo_net_ops = {
  329. .init = ctinfo_init_net,
  330. .exit_batch = ctinfo_exit_net,
  331. .id = &act_ctinfo_ops.net_id,
  332. .size = sizeof(struct tc_action_net),
  333. };
  334. static int __init ctinfo_init_module(void)
  335. {
  336. return tcf_register_action(&act_ctinfo_ops, &ctinfo_net_ops);
  337. }
  338. static void __exit ctinfo_cleanup_module(void)
  339. {
  340. tcf_unregister_action(&act_ctinfo_ops, &ctinfo_net_ops);
  341. }
  342. module_init(ctinfo_init_module);
  343. module_exit(ctinfo_cleanup_module);
  344. MODULE_AUTHOR("Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>");
  345. MODULE_DESCRIPTION("Connection tracking mark actions");
  346. MODULE_LICENSE("GPL");