xref: /openbmc/linux/net/sched/act_connmark.c (revision 9a234a2a)
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 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/pkt_cls.h>
15 #include <linux/ip.h>
16 #include <linux/ipv6.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
19 #include <net/act_api.h>
20 #include <net/pkt_cls.h>
21 #include <uapi/linux/tc_act/tc_connmark.h>
22 #include <net/tc_act/tc_connmark.h>
23 
24 #include <net/netfilter/nf_conntrack.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/nf_conntrack_zones.h>
27 
28 static struct tc_action_ops act_connmark_ops;
29 
30 static int tcf_connmark_act(struct sk_buff *skb, const struct tc_action *a,
31 			    struct tcf_result *res)
32 {
33 	const struct nf_conntrack_tuple_hash *thash;
34 	struct nf_conntrack_tuple tuple;
35 	enum ip_conntrack_info ctinfo;
36 	struct tcf_connmark_info *ca = to_connmark(a);
37 	struct nf_conntrack_zone zone;
38 	struct nf_conn *c;
39 	int proto;
40 
41 	spin_lock(&ca->tcf_lock);
42 	tcf_lastuse_update(&ca->tcf_tm);
43 	bstats_update(&ca->tcf_bstats, skb);
44 
45 	switch (skb_protocol(skb, true)) {
46 	case htons(ETH_P_IP):
47 		if (skb->len < sizeof(struct iphdr))
48 			goto out;
49 
50 		proto = NFPROTO_IPV4;
51 		break;
52 	case htons(ETH_P_IPV6):
53 		if (skb->len < sizeof(struct ipv6hdr))
54 			goto out;
55 
56 		proto = NFPROTO_IPV6;
57 		break;
58 	default:
59 		goto out;
60 	}
61 
62 	c = nf_ct_get(skb, &ctinfo);
63 	if (c) {
64 		skb->mark = READ_ONCE(c->mark);
65 		/* using overlimits stats to count how many packets marked */
66 		ca->tcf_qstats.overlimits++;
67 		goto out;
68 	}
69 
70 	if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
71 			       proto, ca->net, &tuple))
72 		goto out;
73 
74 	zone.id = ca->zone;
75 	zone.dir = NF_CT_DEFAULT_ZONE_DIR;
76 
77 	thash = nf_conntrack_find_get(ca->net, &zone, &tuple);
78 	if (!thash)
79 		goto out;
80 
81 	c = nf_ct_tuplehash_to_ctrack(thash);
82 	/* using overlimits stats to count how many packets marked */
83 	ca->tcf_qstats.overlimits++;
84 	skb->mark = READ_ONCE(c->mark);
85 	nf_ct_put(c);
86 
87 out:
88 	spin_unlock(&ca->tcf_lock);
89 	return ca->tcf_action;
90 }
91 
92 static const struct nla_policy connmark_policy[TCA_CONNMARK_MAX + 1] = {
93 	[TCA_CONNMARK_PARMS] = { .len = sizeof(struct tc_connmark) },
94 };
95 
96 static int tcf_connmark_init(struct net *net, struct nlattr *nla,
97 			     struct nlattr *est, struct tc_action **a,
98 			     struct tcf_proto *tp, u32 flags,
99 			     struct netlink_ext_ack *extack)
100 {
101 	struct tc_action_net *tn = net_generic(net, act_connmark_ops.net_id);
102 	struct nlattr *tb[TCA_CONNMARK_MAX + 1];
103 	bool bind = flags & TCA_ACT_FLAGS_BIND;
104 	struct tcf_chain *goto_ch = NULL;
105 	struct tcf_connmark_info *ci;
106 	struct tc_connmark *parm;
107 	int ret = 0, err;
108 	u32 index;
109 
110 	if (!nla)
111 		return -EINVAL;
112 
113 	ret = nla_parse_nested_deprecated(tb, TCA_CONNMARK_MAX, nla,
114 					  connmark_policy, NULL);
115 	if (ret < 0)
116 		return ret;
117 
118 	if (!tb[TCA_CONNMARK_PARMS])
119 		return -EINVAL;
120 
121 	parm = nla_data(tb[TCA_CONNMARK_PARMS]);
122 	index = parm->index;
123 	ret = tcf_idr_check_alloc(tn, &index, a, bind);
124 	if (!ret) {
125 		ret = tcf_idr_create(tn, index, est, a,
126 				     &act_connmark_ops, bind, false, flags);
127 		if (ret) {
128 			tcf_idr_cleanup(tn, index);
129 			return ret;
130 		}
131 
132 		ci = to_connmark(*a);
133 		err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch,
134 					       extack);
135 		if (err < 0)
136 			goto release_idr;
137 		tcf_action_set_ctrlact(*a, parm->action, goto_ch);
138 		ci->net = net;
139 		ci->zone = parm->zone;
140 
141 		ret = ACT_P_CREATED;
142 	} else if (ret > 0) {
143 		ci = to_connmark(*a);
144 		if (bind)
145 			return 0;
146 		if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
147 			tcf_idr_release(*a, bind);
148 			return -EEXIST;
149 		}
150 		err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch,
151 					       extack);
152 		if (err < 0)
153 			goto release_idr;
154 		/* replacing action and zone */
155 		spin_lock_bh(&ci->tcf_lock);
156 		goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
157 		ci->zone = parm->zone;
158 		spin_unlock_bh(&ci->tcf_lock);
159 		if (goto_ch)
160 			tcf_chain_put_by_act(goto_ch);
161 		ret = 0;
162 	}
163 
164 	return ret;
165 release_idr:
166 	tcf_idr_release(*a, bind);
167 	return err;
168 }
169 
170 static inline int tcf_connmark_dump(struct sk_buff *skb, struct tc_action *a,
171 				    int bind, int ref)
172 {
173 	unsigned char *b = skb_tail_pointer(skb);
174 	struct tcf_connmark_info *ci = to_connmark(a);
175 	struct tc_connmark opt = {
176 		.index   = ci->tcf_index,
177 		.refcnt  = refcount_read(&ci->tcf_refcnt) - ref,
178 		.bindcnt = atomic_read(&ci->tcf_bindcnt) - bind,
179 	};
180 	struct tcf_t t;
181 
182 	spin_lock_bh(&ci->tcf_lock);
183 	opt.action = ci->tcf_action;
184 	opt.zone = ci->zone;
185 	if (nla_put(skb, TCA_CONNMARK_PARMS, sizeof(opt), &opt))
186 		goto nla_put_failure;
187 
188 	tcf_tm_dump(&t, &ci->tcf_tm);
189 	if (nla_put_64bit(skb, TCA_CONNMARK_TM, sizeof(t), &t,
190 			  TCA_CONNMARK_PAD))
191 		goto nla_put_failure;
192 	spin_unlock_bh(&ci->tcf_lock);
193 
194 	return skb->len;
195 
196 nla_put_failure:
197 	spin_unlock_bh(&ci->tcf_lock);
198 	nlmsg_trim(skb, b);
199 	return -1;
200 }
201 
202 static struct tc_action_ops act_connmark_ops = {
203 	.kind		=	"connmark",
204 	.id		=	TCA_ID_CONNMARK,
205 	.owner		=	THIS_MODULE,
206 	.act		=	tcf_connmark_act,
207 	.dump		=	tcf_connmark_dump,
208 	.init		=	tcf_connmark_init,
209 	.size		=	sizeof(struct tcf_connmark_info),
210 };
211 
212 static __net_init int connmark_init_net(struct net *net)
213 {
214 	struct tc_action_net *tn = net_generic(net, act_connmark_ops.net_id);
215 
216 	return tc_action_net_init(net, tn, &act_connmark_ops);
217 }
218 
219 static void __net_exit connmark_exit_net(struct list_head *net_list)
220 {
221 	tc_action_net_exit(net_list, act_connmark_ops.net_id);
222 }
223 
224 static struct pernet_operations connmark_net_ops = {
225 	.init = connmark_init_net,
226 	.exit_batch = connmark_exit_net,
227 	.id   = &act_connmark_ops.net_id,
228 	.size = sizeof(struct tc_action_net),
229 };
230 
231 static int __init connmark_init_module(void)
232 {
233 	return tcf_register_action(&act_connmark_ops, &connmark_net_ops);
234 }
235 
236 static void __exit connmark_cleanup_module(void)
237 {
238 	tcf_unregister_action(&act_connmark_ops, &connmark_net_ops);
239 }
240 
241 module_init(connmark_init_module);
242 module_exit(connmark_cleanup_module);
243 MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
244 MODULE_DESCRIPTION("Connection tracking mark restoring");
245 MODULE_LICENSE("GPL");
246