xref: /openbmc/linux/net/sched/act_ctinfo.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
124ec483cSKevin 'ldir' Darbyshire-Bryant // SPDX-License-Identifier: GPL-2.0+
224ec483cSKevin 'ldir' Darbyshire-Bryant /* net/sched/act_ctinfo.c  netfilter ctinfo connmark actions
324ec483cSKevin 'ldir' Darbyshire-Bryant  *
424ec483cSKevin 'ldir' Darbyshire-Bryant  * Copyright (c) 2019 Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
524ec483cSKevin 'ldir' Darbyshire-Bryant  */
624ec483cSKevin 'ldir' Darbyshire-Bryant 
724ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/module.h>
824ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/init.h>
924ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/kernel.h>
1024ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/skbuff.h>
1124ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/rtnetlink.h>
1224ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/pkt_cls.h>
1324ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/ip.h>
1424ec483cSKevin 'ldir' Darbyshire-Bryant #include <linux/ipv6.h>
1524ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/netlink.h>
1624ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/pkt_sched.h>
1724ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/act_api.h>
1824ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/pkt_cls.h>
1924ec483cSKevin 'ldir' Darbyshire-Bryant #include <uapi/linux/tc_act/tc_ctinfo.h>
2024ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/tc_act/tc_ctinfo.h>
21871cf386SPedro Tammela #include <net/tc_wrapper.h>
2224ec483cSKevin 'ldir' Darbyshire-Bryant 
2324ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/netfilter/nf_conntrack.h>
2424ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/netfilter/nf_conntrack_core.h>
2524ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/netfilter/nf_conntrack_ecache.h>
2624ec483cSKevin 'ldir' Darbyshire-Bryant #include <net/netfilter/nf_conntrack_zones.h>
2724ec483cSKevin 'ldir' Darbyshire-Bryant 
2824ec483cSKevin 'ldir' Darbyshire-Bryant static struct tc_action_ops act_ctinfo_ops;
2924ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_dscp_set(struct nf_conn * ct,struct tcf_ctinfo * ca,struct tcf_ctinfo_params * cp,struct sk_buff * skb,int wlen,int proto)3024ec483cSKevin 'ldir' Darbyshire-Bryant static void tcf_ctinfo_dscp_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
3124ec483cSKevin 'ldir' Darbyshire-Bryant 				struct tcf_ctinfo_params *cp,
3224ec483cSKevin 'ldir' Darbyshire-Bryant 				struct sk_buff *skb, int wlen, int proto)
3324ec483cSKevin 'ldir' Darbyshire-Bryant {
3424ec483cSKevin 'ldir' Darbyshire-Bryant 	u8 dscp, newdscp;
3524ec483cSKevin 'ldir' Darbyshire-Bryant 
3652d1aa8bSDaniel Xu 	newdscp = (((READ_ONCE(ct->mark) & cp->dscpmask) >> cp->dscpmaskshift) << 2) &
3724ec483cSKevin 'ldir' Darbyshire-Bryant 		     ~INET_ECN_MASK;
3824ec483cSKevin 'ldir' Darbyshire-Bryant 
3924ec483cSKevin 'ldir' Darbyshire-Bryant 	switch (proto) {
4024ec483cSKevin 'ldir' Darbyshire-Bryant 	case NFPROTO_IPV4:
4124ec483cSKevin 'ldir' Darbyshire-Bryant 		dscp = ipv4_get_dsfield(ip_hdr(skb)) & ~INET_ECN_MASK;
4224ec483cSKevin 'ldir' Darbyshire-Bryant 		if (dscp != newdscp) {
4324ec483cSKevin 'ldir' Darbyshire-Bryant 			if (likely(!skb_try_make_writable(skb, wlen))) {
4424ec483cSKevin 'ldir' Darbyshire-Bryant 				ipv4_change_dsfield(ip_hdr(skb),
4524ec483cSKevin 'ldir' Darbyshire-Bryant 						    INET_ECN_MASK,
4624ec483cSKevin 'ldir' Darbyshire-Bryant 						    newdscp);
4724ec483cSKevin 'ldir' Darbyshire-Bryant 				ca->stats_dscp_set++;
4824ec483cSKevin 'ldir' Darbyshire-Bryant 			} else {
4924ec483cSKevin 'ldir' Darbyshire-Bryant 				ca->stats_dscp_error++;
5024ec483cSKevin 'ldir' Darbyshire-Bryant 			}
5124ec483cSKevin 'ldir' Darbyshire-Bryant 		}
5224ec483cSKevin 'ldir' Darbyshire-Bryant 		break;
5324ec483cSKevin 'ldir' Darbyshire-Bryant 	case NFPROTO_IPV6:
5424ec483cSKevin 'ldir' Darbyshire-Bryant 		dscp = ipv6_get_dsfield(ipv6_hdr(skb)) & ~INET_ECN_MASK;
5524ec483cSKevin 'ldir' Darbyshire-Bryant 		if (dscp != newdscp) {
5624ec483cSKevin 'ldir' Darbyshire-Bryant 			if (likely(!skb_try_make_writable(skb, wlen))) {
5724ec483cSKevin 'ldir' Darbyshire-Bryant 				ipv6_change_dsfield(ipv6_hdr(skb),
5824ec483cSKevin 'ldir' Darbyshire-Bryant 						    INET_ECN_MASK,
5924ec483cSKevin 'ldir' Darbyshire-Bryant 						    newdscp);
6024ec483cSKevin 'ldir' Darbyshire-Bryant 				ca->stats_dscp_set++;
6124ec483cSKevin 'ldir' Darbyshire-Bryant 			} else {
6224ec483cSKevin 'ldir' Darbyshire-Bryant 				ca->stats_dscp_error++;
6324ec483cSKevin 'ldir' Darbyshire-Bryant 			}
6424ec483cSKevin 'ldir' Darbyshire-Bryant 		}
6524ec483cSKevin 'ldir' Darbyshire-Bryant 		break;
6624ec483cSKevin 'ldir' Darbyshire-Bryant 	default:
6724ec483cSKevin 'ldir' Darbyshire-Bryant 		break;
6824ec483cSKevin 'ldir' Darbyshire-Bryant 	}
6924ec483cSKevin 'ldir' Darbyshire-Bryant }
7024ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_cpmark_set(struct nf_conn * ct,struct tcf_ctinfo * ca,struct tcf_ctinfo_params * cp,struct sk_buff * skb)7124ec483cSKevin 'ldir' Darbyshire-Bryant static void tcf_ctinfo_cpmark_set(struct nf_conn *ct, struct tcf_ctinfo *ca,
7224ec483cSKevin 'ldir' Darbyshire-Bryant 				  struct tcf_ctinfo_params *cp,
7324ec483cSKevin 'ldir' Darbyshire-Bryant 				  struct sk_buff *skb)
7424ec483cSKevin 'ldir' Darbyshire-Bryant {
7524ec483cSKevin 'ldir' Darbyshire-Bryant 	ca->stats_cpmark_set++;
7652d1aa8bSDaniel Xu 	skb->mark = READ_ONCE(ct->mark) & cp->cpmarkmask;
7724ec483cSKevin 'ldir' Darbyshire-Bryant }
7824ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)79871cf386SPedro Tammela TC_INDIRECT_SCOPE int tcf_ctinfo_act(struct sk_buff *skb,
80871cf386SPedro Tammela 				     const struct tc_action *a,
8124ec483cSKevin 'ldir' Darbyshire-Bryant 				     struct tcf_result *res)
8224ec483cSKevin 'ldir' Darbyshire-Bryant {
8324ec483cSKevin 'ldir' Darbyshire-Bryant 	const struct nf_conntrack_tuple_hash *thash = NULL;
8424ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo *ca = to_ctinfo(a);
8524ec483cSKevin 'ldir' Darbyshire-Bryant 	struct nf_conntrack_tuple tuple;
8624ec483cSKevin 'ldir' Darbyshire-Bryant 	struct nf_conntrack_zone zone;
8724ec483cSKevin 'ldir' Darbyshire-Bryant 	enum ip_conntrack_info ctinfo;
8824ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo_params *cp;
8924ec483cSKevin 'ldir' Darbyshire-Bryant 	struct nf_conn *ct;
9024ec483cSKevin 'ldir' Darbyshire-Bryant 	int proto, wlen;
9124ec483cSKevin 'ldir' Darbyshire-Bryant 	int action;
9224ec483cSKevin 'ldir' Darbyshire-Bryant 
9324ec483cSKevin 'ldir' Darbyshire-Bryant 	cp = rcu_dereference_bh(ca->params);
9424ec483cSKevin 'ldir' Darbyshire-Bryant 
9524ec483cSKevin 'ldir' Darbyshire-Bryant 	tcf_lastuse_update(&ca->tcf_tm);
96*21c167aaSPedro Tammela 	tcf_action_update_bstats(&ca->common, skb);
9724ec483cSKevin 'ldir' Darbyshire-Bryant 	action = READ_ONCE(ca->tcf_action);
9824ec483cSKevin 'ldir' Darbyshire-Bryant 
9924ec483cSKevin 'ldir' Darbyshire-Bryant 	wlen = skb_network_offset(skb);
100d7bf2ebeSToke Høiland-Jørgensen 	switch (skb_protocol(skb, true)) {
101d7bf2ebeSToke Høiland-Jørgensen 	case htons(ETH_P_IP):
10224ec483cSKevin 'ldir' Darbyshire-Bryant 		wlen += sizeof(struct iphdr);
10324ec483cSKevin 'ldir' Darbyshire-Bryant 		if (!pskb_may_pull(skb, wlen))
10424ec483cSKevin 'ldir' Darbyshire-Bryant 			goto out;
10524ec483cSKevin 'ldir' Darbyshire-Bryant 
10624ec483cSKevin 'ldir' Darbyshire-Bryant 		proto = NFPROTO_IPV4;
107d7bf2ebeSToke Høiland-Jørgensen 		break;
108d7bf2ebeSToke Høiland-Jørgensen 	case htons(ETH_P_IPV6):
10924ec483cSKevin 'ldir' Darbyshire-Bryant 		wlen += sizeof(struct ipv6hdr);
11024ec483cSKevin 'ldir' Darbyshire-Bryant 		if (!pskb_may_pull(skb, wlen))
11124ec483cSKevin 'ldir' Darbyshire-Bryant 			goto out;
11224ec483cSKevin 'ldir' Darbyshire-Bryant 
11324ec483cSKevin 'ldir' Darbyshire-Bryant 		proto = NFPROTO_IPV6;
114d7bf2ebeSToke Høiland-Jørgensen 		break;
115d7bf2ebeSToke Høiland-Jørgensen 	default:
11624ec483cSKevin 'ldir' Darbyshire-Bryant 		goto out;
11724ec483cSKevin 'ldir' Darbyshire-Bryant 	}
11824ec483cSKevin 'ldir' Darbyshire-Bryant 
11924ec483cSKevin 'ldir' Darbyshire-Bryant 	ct = nf_ct_get(skb, &ctinfo);
12024ec483cSKevin 'ldir' Darbyshire-Bryant 	if (!ct) { /* look harder, usually ingress */
12124ec483cSKevin 'ldir' Darbyshire-Bryant 		if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
12224ec483cSKevin 'ldir' Darbyshire-Bryant 				       proto, cp->net, &tuple))
12324ec483cSKevin 'ldir' Darbyshire-Bryant 			goto out;
12424ec483cSKevin 'ldir' Darbyshire-Bryant 		zone.id = cp->zone;
12524ec483cSKevin 'ldir' Darbyshire-Bryant 		zone.dir = NF_CT_DEFAULT_ZONE_DIR;
12624ec483cSKevin 'ldir' Darbyshire-Bryant 
12724ec483cSKevin 'ldir' Darbyshire-Bryant 		thash = nf_conntrack_find_get(cp->net, &zone, &tuple);
12824ec483cSKevin 'ldir' Darbyshire-Bryant 		if (!thash)
12924ec483cSKevin 'ldir' Darbyshire-Bryant 			goto out;
13024ec483cSKevin 'ldir' Darbyshire-Bryant 
13124ec483cSKevin 'ldir' Darbyshire-Bryant 		ct = nf_ct_tuplehash_to_ctrack(thash);
13224ec483cSKevin 'ldir' Darbyshire-Bryant 	}
13324ec483cSKevin 'ldir' Darbyshire-Bryant 
13424ec483cSKevin 'ldir' Darbyshire-Bryant 	if (cp->mode & CTINFO_MODE_DSCP)
13552d1aa8bSDaniel Xu 		if (!cp->dscpstatemask || (READ_ONCE(ct->mark) & cp->dscpstatemask))
13624ec483cSKevin 'ldir' Darbyshire-Bryant 			tcf_ctinfo_dscp_set(ct, ca, cp, skb, wlen, proto);
13724ec483cSKevin 'ldir' Darbyshire-Bryant 
13824ec483cSKevin 'ldir' Darbyshire-Bryant 	if (cp->mode & CTINFO_MODE_CPMARK)
13924ec483cSKevin 'ldir' Darbyshire-Bryant 		tcf_ctinfo_cpmark_set(ct, ca, cp, skb);
14024ec483cSKevin 'ldir' Darbyshire-Bryant 
14124ec483cSKevin 'ldir' Darbyshire-Bryant 	if (thash)
14224ec483cSKevin 'ldir' Darbyshire-Bryant 		nf_ct_put(ct);
14324ec483cSKevin 'ldir' Darbyshire-Bryant out:
14424ec483cSKevin 'ldir' Darbyshire-Bryant 	return action;
14524ec483cSKevin 'ldir' Darbyshire-Bryant }
14624ec483cSKevin 'ldir' Darbyshire-Bryant 
14724ec483cSKevin 'ldir' Darbyshire-Bryant static const struct nla_policy ctinfo_policy[TCA_CTINFO_MAX + 1] = {
1488140860cSJohannes Berg 	[TCA_CTINFO_ACT]		  =
1498140860cSJohannes Berg 		NLA_POLICY_EXACT_LEN(sizeof(struct tc_ctinfo)),
15024ec483cSKevin 'ldir' Darbyshire-Bryant 	[TCA_CTINFO_ZONE]		  = { .type = NLA_U16 },
15124ec483cSKevin 'ldir' Darbyshire-Bryant 	[TCA_CTINFO_PARMS_DSCP_MASK]	  = { .type = NLA_U32 },
15224ec483cSKevin 'ldir' Darbyshire-Bryant 	[TCA_CTINFO_PARMS_DSCP_STATEMASK] = { .type = NLA_U32 },
15324ec483cSKevin 'ldir' Darbyshire-Bryant 	[TCA_CTINFO_PARMS_CPMARK_MASK]	  = { .type = NLA_U32 },
15424ec483cSKevin 'ldir' Darbyshire-Bryant };
15524ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)15624ec483cSKevin 'ldir' Darbyshire-Bryant static int tcf_ctinfo_init(struct net *net, struct nlattr *nla,
15724ec483cSKevin 'ldir' Darbyshire-Bryant 			   struct nlattr *est, struct tc_action **a,
158abbb0d33SVlad Buslov 			   struct tcf_proto *tp, u32 flags,
15924ec483cSKevin 'ldir' Darbyshire-Bryant 			   struct netlink_ext_ack *extack)
16024ec483cSKevin 'ldir' Darbyshire-Bryant {
161acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_ctinfo_ops.net_id);
162695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
1637be8ef2cSDmytro Linkin 	u32 dscpmask = 0, dscpstatemask, index;
16424ec483cSKevin 'ldir' Darbyshire-Bryant 	struct nlattr *tb[TCA_CTINFO_MAX + 1];
16524ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo_params *cp_new;
16624ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_chain *goto_ch = NULL;
16724ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tc_ctinfo *actparm;
16824ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo *ci;
16924ec483cSKevin 'ldir' Darbyshire-Bryant 	u8 dscpmaskshift;
17024ec483cSKevin 'ldir' Darbyshire-Bryant 	int ret = 0, err;
17124ec483cSKevin 'ldir' Darbyshire-Bryant 
172733f0766SKevin Darbyshire-Bryant 	if (!nla) {
173733f0766SKevin Darbyshire-Bryant 		NL_SET_ERR_MSG_MOD(extack, "ctinfo requires attributes to be passed");
17424ec483cSKevin 'ldir' Darbyshire-Bryant 		return -EINVAL;
175733f0766SKevin Darbyshire-Bryant 	}
17624ec483cSKevin 'ldir' Darbyshire-Bryant 
177733f0766SKevin Darbyshire-Bryant 	err = nla_parse_nested(tb, TCA_CTINFO_MAX, nla, ctinfo_policy, extack);
17824ec483cSKevin 'ldir' Darbyshire-Bryant 	if (err < 0)
17924ec483cSKevin 'ldir' Darbyshire-Bryant 		return err;
18024ec483cSKevin 'ldir' Darbyshire-Bryant 
181733f0766SKevin Darbyshire-Bryant 	if (!tb[TCA_CTINFO_ACT]) {
182733f0766SKevin Darbyshire-Bryant 		NL_SET_ERR_MSG_MOD(extack,
183733f0766SKevin Darbyshire-Bryant 				   "Missing required TCA_CTINFO_ACT attribute");
18424ec483cSKevin 'ldir' Darbyshire-Bryant 		return -EINVAL;
185733f0766SKevin Darbyshire-Bryant 	}
18624ec483cSKevin 'ldir' Darbyshire-Bryant 	actparm = nla_data(tb[TCA_CTINFO_ACT]);
18724ec483cSKevin 'ldir' Darbyshire-Bryant 
18824ec483cSKevin 'ldir' Darbyshire-Bryant 	/* do some basic validation here before dynamically allocating things */
18924ec483cSKevin 'ldir' Darbyshire-Bryant 	/* that we would otherwise have to clean up.			      */
19024ec483cSKevin 'ldir' Darbyshire-Bryant 	if (tb[TCA_CTINFO_PARMS_DSCP_MASK]) {
19124ec483cSKevin 'ldir' Darbyshire-Bryant 		dscpmask = nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_MASK]);
19224ec483cSKevin 'ldir' Darbyshire-Bryant 		/* need contiguous 6 bit mask */
19324ec483cSKevin 'ldir' Darbyshire-Bryant 		dscpmaskshift = dscpmask ? __ffs(dscpmask) : 0;
194733f0766SKevin Darbyshire-Bryant 		if ((~0 & (dscpmask >> dscpmaskshift)) != 0x3f) {
195733f0766SKevin Darbyshire-Bryant 			NL_SET_ERR_MSG_ATTR(extack,
196733f0766SKevin Darbyshire-Bryant 					    tb[TCA_CTINFO_PARMS_DSCP_MASK],
197733f0766SKevin Darbyshire-Bryant 					    "dscp mask must be 6 contiguous bits");
19824ec483cSKevin 'ldir' Darbyshire-Bryant 			return -EINVAL;
199733f0766SKevin Darbyshire-Bryant 		}
20024ec483cSKevin 'ldir' Darbyshire-Bryant 		dscpstatemask = tb[TCA_CTINFO_PARMS_DSCP_STATEMASK] ?
20124ec483cSKevin 'ldir' Darbyshire-Bryant 			nla_get_u32(tb[TCA_CTINFO_PARMS_DSCP_STATEMASK]) : 0;
20224ec483cSKevin 'ldir' Darbyshire-Bryant 		/* mask & statemask must not overlap */
203733f0766SKevin Darbyshire-Bryant 		if (dscpmask & dscpstatemask) {
204733f0766SKevin Darbyshire-Bryant 			NL_SET_ERR_MSG_ATTR(extack,
205733f0766SKevin Darbyshire-Bryant 					    tb[TCA_CTINFO_PARMS_DSCP_STATEMASK],
206733f0766SKevin Darbyshire-Bryant 					    "dscp statemask must not overlap dscp mask");
20724ec483cSKevin 'ldir' Darbyshire-Bryant 			return -EINVAL;
20824ec483cSKevin 'ldir' Darbyshire-Bryant 		}
209733f0766SKevin Darbyshire-Bryant 	}
21024ec483cSKevin 'ldir' Darbyshire-Bryant 
21124ec483cSKevin 'ldir' Darbyshire-Bryant 	/* done the validation:now to the actual action allocation */
2127be8ef2cSDmytro Linkin 	index = actparm->index;
2137be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
21424ec483cSKevin 'ldir' Darbyshire-Bryant 	if (!err) {
215*21c167aaSPedro Tammela 		ret = tcf_idr_create_from_flags(tn, index, est, a,
216*21c167aaSPedro Tammela 						&act_ctinfo_ops, bind, flags);
21724ec483cSKevin 'ldir' Darbyshire-Bryant 		if (ret) {
2187be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
21924ec483cSKevin 'ldir' Darbyshire-Bryant 			return ret;
22024ec483cSKevin 'ldir' Darbyshire-Bryant 		}
221a658c2e4SKevin Darbyshire-Bryant 		ret = ACT_P_CREATED;
22224ec483cSKevin 'ldir' Darbyshire-Bryant 	} else if (err > 0) {
22324ec483cSKevin 'ldir' Darbyshire-Bryant 		if (bind) /* don't override defaults */
22424ec483cSKevin 'ldir' Darbyshire-Bryant 			return 0;
225695176bfSCong Wang 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
22624ec483cSKevin 'ldir' Darbyshire-Bryant 			tcf_idr_release(*a, bind);
22724ec483cSKevin 'ldir' Darbyshire-Bryant 			return -EEXIST;
22824ec483cSKevin 'ldir' Darbyshire-Bryant 		}
22924ec483cSKevin 'ldir' Darbyshire-Bryant 	} else {
23024ec483cSKevin 'ldir' Darbyshire-Bryant 		return err;
23124ec483cSKevin 'ldir' Darbyshire-Bryant 	}
23224ec483cSKevin 'ldir' Darbyshire-Bryant 
23324ec483cSKevin 'ldir' Darbyshire-Bryant 	err = tcf_action_check_ctrlact(actparm->action, tp, &goto_ch, extack);
23424ec483cSKevin 'ldir' Darbyshire-Bryant 	if (err < 0)
23524ec483cSKevin 'ldir' Darbyshire-Bryant 		goto release_idr;
23624ec483cSKevin 'ldir' Darbyshire-Bryant 
23724ec483cSKevin 'ldir' Darbyshire-Bryant 	ci = to_ctinfo(*a);
23824ec483cSKevin 'ldir' Darbyshire-Bryant 
23924ec483cSKevin 'ldir' Darbyshire-Bryant 	cp_new = kzalloc(sizeof(*cp_new), GFP_KERNEL);
24024ec483cSKevin 'ldir' Darbyshire-Bryant 	if (unlikely(!cp_new)) {
24124ec483cSKevin 'ldir' Darbyshire-Bryant 		err = -ENOMEM;
24224ec483cSKevin 'ldir' Darbyshire-Bryant 		goto put_chain;
24324ec483cSKevin 'ldir' Darbyshire-Bryant 	}
24424ec483cSKevin 'ldir' Darbyshire-Bryant 
24524ec483cSKevin 'ldir' Darbyshire-Bryant 	cp_new->net = net;
24624ec483cSKevin 'ldir' Darbyshire-Bryant 	cp_new->zone = tb[TCA_CTINFO_ZONE] ?
24724ec483cSKevin 'ldir' Darbyshire-Bryant 			nla_get_u16(tb[TCA_CTINFO_ZONE]) : 0;
24824ec483cSKevin 'ldir' Darbyshire-Bryant 	if (dscpmask) {
24924ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->dscpmask = dscpmask;
25024ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->dscpmaskshift = dscpmaskshift;
25124ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->dscpstatemask = dscpstatemask;
25224ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->mode |= CTINFO_MODE_DSCP;
25324ec483cSKevin 'ldir' Darbyshire-Bryant 	}
25424ec483cSKevin 'ldir' Darbyshire-Bryant 
25524ec483cSKevin 'ldir' Darbyshire-Bryant 	if (tb[TCA_CTINFO_PARMS_CPMARK_MASK]) {
25624ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->cpmarkmask =
25724ec483cSKevin 'ldir' Darbyshire-Bryant 				nla_get_u32(tb[TCA_CTINFO_PARMS_CPMARK_MASK]);
25824ec483cSKevin 'ldir' Darbyshire-Bryant 		cp_new->mode |= CTINFO_MODE_CPMARK;
25924ec483cSKevin 'ldir' Darbyshire-Bryant 	}
26024ec483cSKevin 'ldir' Darbyshire-Bryant 
26124ec483cSKevin 'ldir' Darbyshire-Bryant 	spin_lock_bh(&ci->tcf_lock);
26224ec483cSKevin 'ldir' Darbyshire-Bryant 	goto_ch = tcf_action_set_ctrlact(*a, actparm->action, goto_ch);
263445d3749SPaul E. McKenney 	cp_new = rcu_replace_pointer(ci->params, cp_new,
26424ec483cSKevin 'ldir' Darbyshire-Bryant 				     lockdep_is_held(&ci->tcf_lock));
26524ec483cSKevin 'ldir' Darbyshire-Bryant 	spin_unlock_bh(&ci->tcf_lock);
26624ec483cSKevin 'ldir' Darbyshire-Bryant 
26724ec483cSKevin 'ldir' Darbyshire-Bryant 	if (goto_ch)
26824ec483cSKevin 'ldir' Darbyshire-Bryant 		tcf_chain_put_by_act(goto_ch);
26924ec483cSKevin 'ldir' Darbyshire-Bryant 	if (cp_new)
27024ec483cSKevin 'ldir' Darbyshire-Bryant 		kfree_rcu(cp_new, rcu);
27124ec483cSKevin 'ldir' Darbyshire-Bryant 
27224ec483cSKevin 'ldir' Darbyshire-Bryant 	return ret;
27324ec483cSKevin 'ldir' Darbyshire-Bryant 
27424ec483cSKevin 'ldir' Darbyshire-Bryant put_chain:
27524ec483cSKevin 'ldir' Darbyshire-Bryant 	if (goto_ch)
27624ec483cSKevin 'ldir' Darbyshire-Bryant 		tcf_chain_put_by_act(goto_ch);
27724ec483cSKevin 'ldir' Darbyshire-Bryant release_idr:
27824ec483cSKevin 'ldir' Darbyshire-Bryant 	tcf_idr_release(*a, bind);
27924ec483cSKevin 'ldir' Darbyshire-Bryant 	return err;
28024ec483cSKevin 'ldir' Darbyshire-Bryant }
28124ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)28224ec483cSKevin 'ldir' Darbyshire-Bryant static int tcf_ctinfo_dump(struct sk_buff *skb, struct tc_action *a,
28324ec483cSKevin 'ldir' Darbyshire-Bryant 			   int bind, int ref)
28424ec483cSKevin 'ldir' Darbyshire-Bryant {
28524ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo *ci = to_ctinfo(a);
28624ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tc_ctinfo opt = {
28724ec483cSKevin 'ldir' Darbyshire-Bryant 		.index   = ci->tcf_index,
28824ec483cSKevin 'ldir' Darbyshire-Bryant 		.refcnt  = refcount_read(&ci->tcf_refcnt) - ref,
28924ec483cSKevin 'ldir' Darbyshire-Bryant 		.bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
29024ec483cSKevin 'ldir' Darbyshire-Bryant 	};
29124ec483cSKevin 'ldir' Darbyshire-Bryant 	unsigned char *b = skb_tail_pointer(skb);
29224ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_ctinfo_params *cp;
29324ec483cSKevin 'ldir' Darbyshire-Bryant 	struct tcf_t t;
29424ec483cSKevin 'ldir' Darbyshire-Bryant 
29524ec483cSKevin 'ldir' Darbyshire-Bryant 	spin_lock_bh(&ci->tcf_lock);
29624ec483cSKevin 'ldir' Darbyshire-Bryant 	cp = rcu_dereference_protected(ci->params,
29724ec483cSKevin 'ldir' Darbyshire-Bryant 				       lockdep_is_held(&ci->tcf_lock));
29824ec483cSKevin 'ldir' Darbyshire-Bryant 
29924ec483cSKevin 'ldir' Darbyshire-Bryant 	tcf_tm_dump(&t, &ci->tcf_tm);
30024ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put_64bit(skb, TCA_CTINFO_TM, sizeof(t), &t, TCA_CTINFO_PAD))
30124ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
30224ec483cSKevin 'ldir' Darbyshire-Bryant 
30324ec483cSKevin 'ldir' Darbyshire-Bryant 	opt.action = ci->tcf_action;
30424ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put(skb, TCA_CTINFO_ACT, sizeof(opt), &opt))
30524ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
30624ec483cSKevin 'ldir' Darbyshire-Bryant 
30724ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put_u16(skb, TCA_CTINFO_ZONE, cp->zone))
30824ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
30924ec483cSKevin 'ldir' Darbyshire-Bryant 
31024ec483cSKevin 'ldir' Darbyshire-Bryant 	if (cp->mode & CTINFO_MODE_DSCP) {
31124ec483cSKevin 'ldir' Darbyshire-Bryant 		if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_MASK,
31224ec483cSKevin 'ldir' Darbyshire-Bryant 				cp->dscpmask))
31324ec483cSKevin 'ldir' Darbyshire-Bryant 			goto nla_put_failure;
31424ec483cSKevin 'ldir' Darbyshire-Bryant 		if (nla_put_u32(skb, TCA_CTINFO_PARMS_DSCP_STATEMASK,
31524ec483cSKevin 'ldir' Darbyshire-Bryant 				cp->dscpstatemask))
31624ec483cSKevin 'ldir' Darbyshire-Bryant 			goto nla_put_failure;
31724ec483cSKevin 'ldir' Darbyshire-Bryant 	}
31824ec483cSKevin 'ldir' Darbyshire-Bryant 
31924ec483cSKevin 'ldir' Darbyshire-Bryant 	if (cp->mode & CTINFO_MODE_CPMARK) {
32024ec483cSKevin 'ldir' Darbyshire-Bryant 		if (nla_put_u32(skb, TCA_CTINFO_PARMS_CPMARK_MASK,
32124ec483cSKevin 'ldir' Darbyshire-Bryant 				cp->cpmarkmask))
32224ec483cSKevin 'ldir' Darbyshire-Bryant 			goto nla_put_failure;
32324ec483cSKevin 'ldir' Darbyshire-Bryant 	}
32424ec483cSKevin 'ldir' Darbyshire-Bryant 
32524ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_SET,
32624ec483cSKevin 'ldir' Darbyshire-Bryant 			      ci->stats_dscp_set, TCA_CTINFO_PAD))
32724ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
32824ec483cSKevin 'ldir' Darbyshire-Bryant 
32924ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_DSCP_ERROR,
33024ec483cSKevin 'ldir' Darbyshire-Bryant 			      ci->stats_dscp_error, TCA_CTINFO_PAD))
33124ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
33224ec483cSKevin 'ldir' Darbyshire-Bryant 
33324ec483cSKevin 'ldir' Darbyshire-Bryant 	if (nla_put_u64_64bit(skb, TCA_CTINFO_STATS_CPMARK_SET,
33424ec483cSKevin 'ldir' Darbyshire-Bryant 			      ci->stats_cpmark_set, TCA_CTINFO_PAD))
33524ec483cSKevin 'ldir' Darbyshire-Bryant 		goto nla_put_failure;
33624ec483cSKevin 'ldir' Darbyshire-Bryant 
33724ec483cSKevin 'ldir' Darbyshire-Bryant 	spin_unlock_bh(&ci->tcf_lock);
33824ec483cSKevin 'ldir' Darbyshire-Bryant 	return skb->len;
33924ec483cSKevin 'ldir' Darbyshire-Bryant 
34024ec483cSKevin 'ldir' Darbyshire-Bryant nla_put_failure:
34124ec483cSKevin 'ldir' Darbyshire-Bryant 	spin_unlock_bh(&ci->tcf_lock);
34224ec483cSKevin 'ldir' Darbyshire-Bryant 	nlmsg_trim(skb, b);
34324ec483cSKevin 'ldir' Darbyshire-Bryant 	return -1;
34424ec483cSKevin 'ldir' Darbyshire-Bryant }
34524ec483cSKevin 'ldir' Darbyshire-Bryant 
tcf_ctinfo_cleanup(struct tc_action * a)34609d4f10aSEric Dumazet static void tcf_ctinfo_cleanup(struct tc_action *a)
34709d4f10aSEric Dumazet {
34809d4f10aSEric Dumazet 	struct tcf_ctinfo *ci = to_ctinfo(a);
34909d4f10aSEric Dumazet 	struct tcf_ctinfo_params *cp;
35009d4f10aSEric Dumazet 
35109d4f10aSEric Dumazet 	cp = rcu_dereference_protected(ci->params, 1);
35209d4f10aSEric Dumazet 	if (cp)
35309d4f10aSEric Dumazet 		kfree_rcu(cp, rcu);
35409d4f10aSEric Dumazet }
35509d4f10aSEric Dumazet 
35624ec483cSKevin 'ldir' Darbyshire-Bryant static struct tc_action_ops act_ctinfo_ops = {
35724ec483cSKevin 'ldir' Darbyshire-Bryant 	.kind	= "ctinfo",
35824ec483cSKevin 'ldir' Darbyshire-Bryant 	.id	= TCA_ID_CTINFO,
35924ec483cSKevin 'ldir' Darbyshire-Bryant 	.owner	= THIS_MODULE,
36024ec483cSKevin 'ldir' Darbyshire-Bryant 	.act	= tcf_ctinfo_act,
36124ec483cSKevin 'ldir' Darbyshire-Bryant 	.dump	= tcf_ctinfo_dump,
36224ec483cSKevin 'ldir' Darbyshire-Bryant 	.init	= tcf_ctinfo_init,
36309d4f10aSEric Dumazet 	.cleanup= tcf_ctinfo_cleanup,
36424ec483cSKevin 'ldir' Darbyshire-Bryant 	.size	= sizeof(struct tcf_ctinfo),
36524ec483cSKevin 'ldir' Darbyshire-Bryant };
36624ec483cSKevin 'ldir' Darbyshire-Bryant 
ctinfo_init_net(struct net * net)36724ec483cSKevin 'ldir' Darbyshire-Bryant static __net_init int ctinfo_init_net(struct net *net)
36824ec483cSKevin 'ldir' Darbyshire-Bryant {
369acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_ctinfo_ops.net_id);
37024ec483cSKevin 'ldir' Darbyshire-Bryant 
371981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_ctinfo_ops);
37224ec483cSKevin 'ldir' Darbyshire-Bryant }
37324ec483cSKevin 'ldir' Darbyshire-Bryant 
ctinfo_exit_net(struct list_head * net_list)37424ec483cSKevin 'ldir' Darbyshire-Bryant static void __net_exit ctinfo_exit_net(struct list_head *net_list)
37524ec483cSKevin 'ldir' Darbyshire-Bryant {
376acd0a7abSZhengchao Shao 	tc_action_net_exit(net_list, act_ctinfo_ops.net_id);
37724ec483cSKevin 'ldir' Darbyshire-Bryant }
37824ec483cSKevin 'ldir' Darbyshire-Bryant 
37924ec483cSKevin 'ldir' Darbyshire-Bryant static struct pernet_operations ctinfo_net_ops = {
38024ec483cSKevin 'ldir' Darbyshire-Bryant 	.init		= ctinfo_init_net,
38124ec483cSKevin 'ldir' Darbyshire-Bryant 	.exit_batch	= ctinfo_exit_net,
382acd0a7abSZhengchao Shao 	.id		= &act_ctinfo_ops.net_id,
38324ec483cSKevin 'ldir' Darbyshire-Bryant 	.size		= sizeof(struct tc_action_net),
38424ec483cSKevin 'ldir' Darbyshire-Bryant };
38524ec483cSKevin 'ldir' Darbyshire-Bryant 
ctinfo_init_module(void)38624ec483cSKevin 'ldir' Darbyshire-Bryant static int __init ctinfo_init_module(void)
38724ec483cSKevin 'ldir' Darbyshire-Bryant {
38824ec483cSKevin 'ldir' Darbyshire-Bryant 	return tcf_register_action(&act_ctinfo_ops, &ctinfo_net_ops);
38924ec483cSKevin 'ldir' Darbyshire-Bryant }
39024ec483cSKevin 'ldir' Darbyshire-Bryant 
ctinfo_cleanup_module(void)39124ec483cSKevin 'ldir' Darbyshire-Bryant static void __exit ctinfo_cleanup_module(void)
39224ec483cSKevin 'ldir' Darbyshire-Bryant {
39324ec483cSKevin 'ldir' Darbyshire-Bryant 	tcf_unregister_action(&act_ctinfo_ops, &ctinfo_net_ops);
39424ec483cSKevin 'ldir' Darbyshire-Bryant }
39524ec483cSKevin 'ldir' Darbyshire-Bryant 
39624ec483cSKevin 'ldir' Darbyshire-Bryant module_init(ctinfo_init_module);
39724ec483cSKevin 'ldir' Darbyshire-Bryant module_exit(ctinfo_cleanup_module);
39824ec483cSKevin 'ldir' Darbyshire-Bryant MODULE_AUTHOR("Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>");
39924ec483cSKevin 'ldir' Darbyshire-Bryant MODULE_DESCRIPTION("Connection tracking mark actions");
40024ec483cSKevin 'ldir' Darbyshire-Bryant MODULE_LICENSE("GPL");
401