act_ctinfo.c 10 KB

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