act_ctinfo.c 10 KB

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