1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/sched/em_ipt.c IPtables matches Ematch
5 * (c) 2018 Eyal Birger <eyal.birger@gmail.com>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/skbuff.h>
14 #include <linux/tc_ematch/tc_em_ipt.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter_ipv4/ip_tables.h>
18 #include <linux/netfilter_ipv6/ip6_tables.h>
19 #include <net/pkt_cls.h>
22 const struct xt_match *match;
24 u8 match_data[0] __aligned(8);
27 struct em_ipt_xt_match {
29 int (*validate_match_data)(struct nlattr **tb, u8 mrev);
32 static const struct nla_policy em_ipt_policy[TCA_EM_IPT_MAX + 1] = {
33 [TCA_EM_IPT_MATCH_NAME] = { .type = NLA_STRING,
34 .len = XT_EXTENSION_MAXNAMELEN },
35 [TCA_EM_IPT_MATCH_REVISION] = { .type = NLA_U8 },
36 [TCA_EM_IPT_HOOK] = { .type = NLA_U32 },
37 [TCA_EM_IPT_NFPROTO] = { .type = NLA_U8 },
38 [TCA_EM_IPT_MATCH_DATA] = { .type = NLA_UNSPEC },
41 static int check_match(struct net *net, struct em_ipt_match *im, int mdata_len)
43 struct xt_mtchk_param mtpar = {};
50 mtpar.table = "filter";
51 mtpar.hook_mask = 1 << im->hook;
52 mtpar.family = im->match->family;
53 mtpar.match = im->match;
55 mtpar.matchinfo = (void *)im->match_data;
56 return xt_check_match(&mtpar, mdata_len, 0, 0);
59 static int policy_validate_match_data(struct nlattr **tb, u8 mrev)
62 pr_err("only policy match revision 0 supported");
66 if (nla_get_u32(tb[TCA_EM_IPT_HOOK]) != NF_INET_PRE_ROUTING) {
67 pr_err("policy can only be matched on NF_INET_PRE_ROUTING");
74 static const struct em_ipt_xt_match em_ipt_xt_matches[] = {
76 .match_name = "policy",
77 .validate_match_data = policy_validate_match_data
82 static struct xt_match *get_xt_match(struct nlattr **tb)
84 const struct em_ipt_xt_match *m;
85 struct nlattr *mname_attr;
89 mname_attr = tb[TCA_EM_IPT_MATCH_NAME];
90 for (m = em_ipt_xt_matches; m->match_name; m++) {
91 if (!nla_strcmp(mname_attr, m->match_name))
96 pr_err("Unsupported xt match");
97 return ERR_PTR(-EINVAL);
100 if (tb[TCA_EM_IPT_MATCH_REVISION])
101 mrev = nla_get_u8(tb[TCA_EM_IPT_MATCH_REVISION]);
103 ret = m->validate_match_data(tb, mrev);
107 nfproto = nla_get_u8(tb[TCA_EM_IPT_NFPROTO]);
108 return xt_request_find_match(nfproto, m->match_name, mrev);
111 static int em_ipt_change(struct net *net, void *data, int data_len,
112 struct tcf_ematch *em)
114 struct nlattr *tb[TCA_EM_IPT_MAX + 1];
115 struct em_ipt_match *im = NULL;
116 struct xt_match *match;
119 ret = nla_parse_deprecated(tb, TCA_EM_IPT_MAX, data, data_len,
120 em_ipt_policy, NULL);
124 if (!tb[TCA_EM_IPT_HOOK] || !tb[TCA_EM_IPT_MATCH_NAME] ||
125 !tb[TCA_EM_IPT_MATCH_DATA] || !tb[TCA_EM_IPT_NFPROTO])
128 match = get_xt_match(tb);
130 pr_err("unable to load match\n");
131 return PTR_ERR(match);
134 mdata_len = XT_ALIGN(nla_len(tb[TCA_EM_IPT_MATCH_DATA]));
135 im = kzalloc(sizeof(*im) + mdata_len, GFP_KERNEL);
142 im->hook = nla_get_u32(tb[TCA_EM_IPT_HOOK]);
143 nla_memcpy(im->match_data, tb[TCA_EM_IPT_MATCH_DATA], mdata_len);
145 ret = check_match(net, im, mdata_len);
149 em->datalen = sizeof(*im) + mdata_len;
150 em->data = (unsigned long)im;
155 module_put(match->me);
159 static void em_ipt_destroy(struct tcf_ematch *em)
161 struct em_ipt_match *im = (void *)em->data;
166 if (im->match->destroy) {
167 struct xt_mtdtor_param par = {
170 .matchinfo = im->match_data,
171 .family = im->match->family
173 im->match->destroy(&par);
175 module_put(im->match->me);
179 static int em_ipt_match(struct sk_buff *skb, struct tcf_ematch *em,
180 struct tcf_pkt_info *info)
182 const struct em_ipt_match *im = (const void *)em->data;
183 struct xt_action_param acpar = {};
184 struct net_device *indev = NULL;
185 struct nf_hook_state state;
191 indev = dev_get_by_index_rcu(em->net, skb->skb_iif);
193 nf_hook_state_init(&state, im->hook, im->match->family,
194 indev ?: skb->dev, skb->dev, NULL, em->net, NULL);
196 acpar.match = im->match;
197 acpar.matchinfo = im->match_data;
198 acpar.state = &state;
200 ret = im->match->match(skb, &acpar);
206 static int em_ipt_dump(struct sk_buff *skb, struct tcf_ematch *em)
208 struct em_ipt_match *im = (void *)em->data;
210 if (nla_put_string(skb, TCA_EM_IPT_MATCH_NAME, im->match->name) < 0)
212 if (nla_put_u32(skb, TCA_EM_IPT_HOOK, im->hook) < 0)
214 if (nla_put_u8(skb, TCA_EM_IPT_MATCH_REVISION, im->match->revision) < 0)
216 if (nla_put_u8(skb, TCA_EM_IPT_NFPROTO, im->match->family) < 0)
218 if (nla_put(skb, TCA_EM_IPT_MATCH_DATA,
219 im->match->usersize ?: im->match->matchsize,
226 static struct tcf_ematch_ops em_ipt_ops = {
228 .change = em_ipt_change,
229 .destroy = em_ipt_destroy,
230 .match = em_ipt_match,
232 .owner = THIS_MODULE,
233 .link = LIST_HEAD_INIT(em_ipt_ops.link)
236 static int __init init_em_ipt(void)
238 return tcf_em_register(&em_ipt_ops);
241 static void __exit exit_em_ipt(void)
243 tcf_em_unregister(&em_ipt_ops);
246 MODULE_LICENSE("GPL");
247 MODULE_AUTHOR("Eyal Birger <eyal.birger@gmail.com>");
248 MODULE_DESCRIPTION("TC extended match for IPtables matches");
250 module_init(init_em_ipt);
251 module_exit(exit_em_ipt);
253 MODULE_ALIAS_TCF_EMATCH(TCF_EM_IPT);