xref: /OK3568_Linux_fs/kernel/net/sched/em_ipt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * net/sched/em_ipt.c IPtables matches Ematch
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (c) 2018 Eyal Birger <eyal.birger@gmail.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/gfp.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/types.h>
11*4882a593Smuzhiyun #include <linux/kernel.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun #include <linux/skbuff.h>
14*4882a593Smuzhiyun #include <linux/tc_ematch/tc_em_ipt.h>
15*4882a593Smuzhiyun #include <linux/netfilter.h>
16*4882a593Smuzhiyun #include <linux/netfilter/x_tables.h>
17*4882a593Smuzhiyun #include <linux/netfilter_ipv4/ip_tables.h>
18*4882a593Smuzhiyun #include <linux/netfilter_ipv6/ip6_tables.h>
19*4882a593Smuzhiyun #include <net/pkt_cls.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct em_ipt_match {
22*4882a593Smuzhiyun 	const struct xt_match *match;
23*4882a593Smuzhiyun 	u32 hook;
24*4882a593Smuzhiyun 	u8 nfproto;
25*4882a593Smuzhiyun 	u8 match_data[] __aligned(8);
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun struct em_ipt_xt_match {
29*4882a593Smuzhiyun 	char *match_name;
30*4882a593Smuzhiyun 	int (*validate_match_data)(struct nlattr **tb, u8 mrev);
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun static const struct nla_policy em_ipt_policy[TCA_EM_IPT_MAX + 1] = {
34*4882a593Smuzhiyun 	[TCA_EM_IPT_MATCH_NAME]		= { .type = NLA_STRING,
35*4882a593Smuzhiyun 					    .len = XT_EXTENSION_MAXNAMELEN },
36*4882a593Smuzhiyun 	[TCA_EM_IPT_MATCH_REVISION]	= { .type = NLA_U8 },
37*4882a593Smuzhiyun 	[TCA_EM_IPT_HOOK]		= { .type = NLA_U32 },
38*4882a593Smuzhiyun 	[TCA_EM_IPT_NFPROTO]		= { .type = NLA_U8 },
39*4882a593Smuzhiyun 	[TCA_EM_IPT_MATCH_DATA]		= { .type = NLA_UNSPEC },
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
check_match(struct net * net,struct em_ipt_match * im,int mdata_len)42*4882a593Smuzhiyun static int check_match(struct net *net, struct em_ipt_match *im, int mdata_len)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct xt_mtchk_param mtpar = {};
45*4882a593Smuzhiyun 	union {
46*4882a593Smuzhiyun 		struct ipt_entry e4;
47*4882a593Smuzhiyun 		struct ip6t_entry e6;
48*4882a593Smuzhiyun 	} e = {};
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	mtpar.net	= net;
51*4882a593Smuzhiyun 	mtpar.table	= "filter";
52*4882a593Smuzhiyun 	mtpar.hook_mask	= 1 << im->hook;
53*4882a593Smuzhiyun 	mtpar.family	= im->match->family;
54*4882a593Smuzhiyun 	mtpar.match	= im->match;
55*4882a593Smuzhiyun 	mtpar.entryinfo = &e;
56*4882a593Smuzhiyun 	mtpar.matchinfo	= (void *)im->match_data;
57*4882a593Smuzhiyun 	return xt_check_match(&mtpar, mdata_len, 0, 0);
58*4882a593Smuzhiyun }
59*4882a593Smuzhiyun 
policy_validate_match_data(struct nlattr ** tb,u8 mrev)60*4882a593Smuzhiyun static int policy_validate_match_data(struct nlattr **tb, u8 mrev)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	if (mrev != 0) {
63*4882a593Smuzhiyun 		pr_err("only policy match revision 0 supported");
64*4882a593Smuzhiyun 		return -EINVAL;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (nla_get_u32(tb[TCA_EM_IPT_HOOK]) != NF_INET_PRE_ROUTING) {
68*4882a593Smuzhiyun 		pr_err("policy can only be matched on NF_INET_PRE_ROUTING");
69*4882a593Smuzhiyun 		return -EINVAL;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
addrtype_validate_match_data(struct nlattr ** tb,u8 mrev)75*4882a593Smuzhiyun static int addrtype_validate_match_data(struct nlattr **tb, u8 mrev)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	if (mrev != 1) {
78*4882a593Smuzhiyun 		pr_err("only addrtype match revision 1 supported");
79*4882a593Smuzhiyun 		return -EINVAL;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static const struct em_ipt_xt_match em_ipt_xt_matches[] = {
86*4882a593Smuzhiyun 	{
87*4882a593Smuzhiyun 		.match_name = "policy",
88*4882a593Smuzhiyun 		.validate_match_data = policy_validate_match_data
89*4882a593Smuzhiyun 	},
90*4882a593Smuzhiyun 	{
91*4882a593Smuzhiyun 		.match_name = "addrtype",
92*4882a593Smuzhiyun 		.validate_match_data = addrtype_validate_match_data
93*4882a593Smuzhiyun 	},
94*4882a593Smuzhiyun 	{}
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
get_xt_match(struct nlattr ** tb)97*4882a593Smuzhiyun static struct xt_match *get_xt_match(struct nlattr **tb)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	const struct em_ipt_xt_match *m;
100*4882a593Smuzhiyun 	struct nlattr *mname_attr;
101*4882a593Smuzhiyun 	u8 nfproto, mrev = 0;
102*4882a593Smuzhiyun 	int ret;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	mname_attr = tb[TCA_EM_IPT_MATCH_NAME];
105*4882a593Smuzhiyun 	for (m = em_ipt_xt_matches; m->match_name; m++) {
106*4882a593Smuzhiyun 		if (!nla_strcmp(mname_attr, m->match_name))
107*4882a593Smuzhiyun 			break;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	if (!m->match_name) {
111*4882a593Smuzhiyun 		pr_err("Unsupported xt match");
112*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (tb[TCA_EM_IPT_MATCH_REVISION])
116*4882a593Smuzhiyun 		mrev = nla_get_u8(tb[TCA_EM_IPT_MATCH_REVISION]);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	ret = m->validate_match_data(tb, mrev);
119*4882a593Smuzhiyun 	if (ret < 0)
120*4882a593Smuzhiyun 		return ERR_PTR(ret);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	nfproto = nla_get_u8(tb[TCA_EM_IPT_NFPROTO]);
123*4882a593Smuzhiyun 	return xt_request_find_match(nfproto, m->match_name, mrev);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun 
em_ipt_change(struct net * net,void * data,int data_len,struct tcf_ematch * em)126*4882a593Smuzhiyun static int em_ipt_change(struct net *net, void *data, int data_len,
127*4882a593Smuzhiyun 			 struct tcf_ematch *em)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct nlattr *tb[TCA_EM_IPT_MAX + 1];
130*4882a593Smuzhiyun 	struct em_ipt_match *im = NULL;
131*4882a593Smuzhiyun 	struct xt_match *match;
132*4882a593Smuzhiyun 	int mdata_len, ret;
133*4882a593Smuzhiyun 	u8 nfproto;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	ret = nla_parse_deprecated(tb, TCA_EM_IPT_MAX, data, data_len,
136*4882a593Smuzhiyun 				   em_ipt_policy, NULL);
137*4882a593Smuzhiyun 	if (ret < 0)
138*4882a593Smuzhiyun 		return ret;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	if (!tb[TCA_EM_IPT_HOOK] || !tb[TCA_EM_IPT_MATCH_NAME] ||
141*4882a593Smuzhiyun 	    !tb[TCA_EM_IPT_MATCH_DATA] || !tb[TCA_EM_IPT_NFPROTO])
142*4882a593Smuzhiyun 		return -EINVAL;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	nfproto = nla_get_u8(tb[TCA_EM_IPT_NFPROTO]);
145*4882a593Smuzhiyun 	switch (nfproto) {
146*4882a593Smuzhiyun 	case NFPROTO_IPV4:
147*4882a593Smuzhiyun 	case NFPROTO_IPV6:
148*4882a593Smuzhiyun 		break;
149*4882a593Smuzhiyun 	default:
150*4882a593Smuzhiyun 		return -EINVAL;
151*4882a593Smuzhiyun 	}
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	match = get_xt_match(tb);
154*4882a593Smuzhiyun 	if (IS_ERR(match)) {
155*4882a593Smuzhiyun 		pr_err("unable to load match\n");
156*4882a593Smuzhiyun 		return PTR_ERR(match);
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	mdata_len = XT_ALIGN(nla_len(tb[TCA_EM_IPT_MATCH_DATA]));
160*4882a593Smuzhiyun 	im = kzalloc(sizeof(*im) + mdata_len, GFP_KERNEL);
161*4882a593Smuzhiyun 	if (!im) {
162*4882a593Smuzhiyun 		ret = -ENOMEM;
163*4882a593Smuzhiyun 		goto err;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	im->match = match;
167*4882a593Smuzhiyun 	im->hook = nla_get_u32(tb[TCA_EM_IPT_HOOK]);
168*4882a593Smuzhiyun 	im->nfproto = nfproto;
169*4882a593Smuzhiyun 	nla_memcpy(im->match_data, tb[TCA_EM_IPT_MATCH_DATA], mdata_len);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	ret = check_match(net, im, mdata_len);
172*4882a593Smuzhiyun 	if (ret)
173*4882a593Smuzhiyun 		goto err;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	em->datalen = sizeof(*im) + mdata_len;
176*4882a593Smuzhiyun 	em->data = (unsigned long)im;
177*4882a593Smuzhiyun 	return 0;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun err:
180*4882a593Smuzhiyun 	kfree(im);
181*4882a593Smuzhiyun 	module_put(match->me);
182*4882a593Smuzhiyun 	return ret;
183*4882a593Smuzhiyun }
184*4882a593Smuzhiyun 
em_ipt_destroy(struct tcf_ematch * em)185*4882a593Smuzhiyun static void em_ipt_destroy(struct tcf_ematch *em)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct em_ipt_match *im = (void *)em->data;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (!im)
190*4882a593Smuzhiyun 		return;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (im->match->destroy) {
193*4882a593Smuzhiyun 		struct xt_mtdtor_param par = {
194*4882a593Smuzhiyun 			.net = em->net,
195*4882a593Smuzhiyun 			.match = im->match,
196*4882a593Smuzhiyun 			.matchinfo = im->match_data,
197*4882a593Smuzhiyun 			.family = im->match->family
198*4882a593Smuzhiyun 		};
199*4882a593Smuzhiyun 		im->match->destroy(&par);
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 	module_put(im->match->me);
202*4882a593Smuzhiyun 	kfree(im);
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun 
em_ipt_match(struct sk_buff * skb,struct tcf_ematch * em,struct tcf_pkt_info * info)205*4882a593Smuzhiyun static int em_ipt_match(struct sk_buff *skb, struct tcf_ematch *em,
206*4882a593Smuzhiyun 			struct tcf_pkt_info *info)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	const struct em_ipt_match *im = (const void *)em->data;
209*4882a593Smuzhiyun 	struct xt_action_param acpar = {};
210*4882a593Smuzhiyun 	struct net_device *indev = NULL;
211*4882a593Smuzhiyun 	u8 nfproto = im->match->family;
212*4882a593Smuzhiyun 	struct nf_hook_state state;
213*4882a593Smuzhiyun 	int ret;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	switch (skb_protocol(skb, true)) {
216*4882a593Smuzhiyun 	case htons(ETH_P_IP):
217*4882a593Smuzhiyun 		if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
218*4882a593Smuzhiyun 			return 0;
219*4882a593Smuzhiyun 		if (nfproto == NFPROTO_UNSPEC)
220*4882a593Smuzhiyun 			nfproto = NFPROTO_IPV4;
221*4882a593Smuzhiyun 		break;
222*4882a593Smuzhiyun 	case htons(ETH_P_IPV6):
223*4882a593Smuzhiyun 		if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
224*4882a593Smuzhiyun 			return 0;
225*4882a593Smuzhiyun 		if (nfproto == NFPROTO_UNSPEC)
226*4882a593Smuzhiyun 			nfproto = NFPROTO_IPV6;
227*4882a593Smuzhiyun 		break;
228*4882a593Smuzhiyun 	default:
229*4882a593Smuzhiyun 		return 0;
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	rcu_read_lock();
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (skb->skb_iif)
235*4882a593Smuzhiyun 		indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	nf_hook_state_init(&state, im->hook, nfproto,
238*4882a593Smuzhiyun 			   indev ?: skb->dev, skb->dev, NULL, em->net, NULL);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	acpar.match = im->match;
241*4882a593Smuzhiyun 	acpar.matchinfo = im->match_data;
242*4882a593Smuzhiyun 	acpar.state = &state;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	ret = im->match->match(skb, &acpar);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	rcu_read_unlock();
247*4882a593Smuzhiyun 	return ret;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun 
em_ipt_dump(struct sk_buff * skb,struct tcf_ematch * em)250*4882a593Smuzhiyun static int em_ipt_dump(struct sk_buff *skb, struct tcf_ematch *em)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	struct em_ipt_match *im = (void *)em->data;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	if (nla_put_string(skb, TCA_EM_IPT_MATCH_NAME, im->match->name) < 0)
255*4882a593Smuzhiyun 		return -EMSGSIZE;
256*4882a593Smuzhiyun 	if (nla_put_u32(skb, TCA_EM_IPT_HOOK, im->hook) < 0)
257*4882a593Smuzhiyun 		return -EMSGSIZE;
258*4882a593Smuzhiyun 	if (nla_put_u8(skb, TCA_EM_IPT_MATCH_REVISION, im->match->revision) < 0)
259*4882a593Smuzhiyun 		return -EMSGSIZE;
260*4882a593Smuzhiyun 	if (nla_put_u8(skb, TCA_EM_IPT_NFPROTO, im->nfproto) < 0)
261*4882a593Smuzhiyun 		return -EMSGSIZE;
262*4882a593Smuzhiyun 	if (nla_put(skb, TCA_EM_IPT_MATCH_DATA,
263*4882a593Smuzhiyun 		    im->match->usersize ?: im->match->matchsize,
264*4882a593Smuzhiyun 		    im->match_data) < 0)
265*4882a593Smuzhiyun 		return -EMSGSIZE;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return 0;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun static struct tcf_ematch_ops em_ipt_ops = {
271*4882a593Smuzhiyun 	.kind	  = TCF_EM_IPT,
272*4882a593Smuzhiyun 	.change	  = em_ipt_change,
273*4882a593Smuzhiyun 	.destroy  = em_ipt_destroy,
274*4882a593Smuzhiyun 	.match	  = em_ipt_match,
275*4882a593Smuzhiyun 	.dump	  = em_ipt_dump,
276*4882a593Smuzhiyun 	.owner	  = THIS_MODULE,
277*4882a593Smuzhiyun 	.link	  = LIST_HEAD_INIT(em_ipt_ops.link)
278*4882a593Smuzhiyun };
279*4882a593Smuzhiyun 
init_em_ipt(void)280*4882a593Smuzhiyun static int __init init_em_ipt(void)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	return tcf_em_register(&em_ipt_ops);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun 
exit_em_ipt(void)285*4882a593Smuzhiyun static void __exit exit_em_ipt(void)
286*4882a593Smuzhiyun {
287*4882a593Smuzhiyun 	tcf_em_unregister(&em_ipt_ops);
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun MODULE_LICENSE("GPL");
291*4882a593Smuzhiyun MODULE_AUTHOR("Eyal Birger <eyal.birger@gmail.com>");
292*4882a593Smuzhiyun MODULE_DESCRIPTION("TC extended match for IPtables matches");
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun module_init(init_em_ipt);
295*4882a593Smuzhiyun module_exit(exit_em_ipt);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPT);
298