xref: /openbmc/linux/net/sched/em_text.c (revision 4083c9bc)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d675c989SThomas Graf /*
3d675c989SThomas Graf  * net/sched/em_text.c	Textsearch ematch
4d675c989SThomas Graf  *
5d675c989SThomas Graf  * Authors:	Thomas Graf <tgraf@suug.ch>
6d675c989SThomas Graf  */
7d675c989SThomas Graf 
85a0e3ad6STejun Heo #include <linux/slab.h>
9d675c989SThomas Graf #include <linux/module.h>
10d675c989SThomas Graf #include <linux/types.h>
11d675c989SThomas Graf #include <linux/kernel.h>
12d675c989SThomas Graf #include <linux/string.h>
13d675c989SThomas Graf #include <linux/skbuff.h>
14d675c989SThomas Graf #include <linux/textsearch.h>
15d675c989SThomas Graf #include <linux/tc_ematch/tc_em_text.h>
16d675c989SThomas Graf #include <net/pkt_cls.h>
17d675c989SThomas Graf 
18cc7ec456SEric Dumazet struct text_match {
19d675c989SThomas Graf 	u16			from_offset;
20d675c989SThomas Graf 	u16			to_offset;
21d675c989SThomas Graf 	u8			from_layer;
22d675c989SThomas Graf 	u8			to_layer;
23d675c989SThomas Graf 	struct ts_config	*config;
24d675c989SThomas Graf };
25d675c989SThomas Graf 
26d675c989SThomas Graf #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
27d675c989SThomas Graf 
em_text_match(struct sk_buff * skb,struct tcf_ematch * m,struct tcf_pkt_info * info)28d675c989SThomas Graf static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
29d675c989SThomas Graf 			 struct tcf_pkt_info *info)
30d675c989SThomas Graf {
31d675c989SThomas Graf 	struct text_match *tm = EM_TEXT_PRIV(m);
32d675c989SThomas Graf 	int from, to;
33d675c989SThomas Graf 
34d675c989SThomas Graf 	from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
35d675c989SThomas Graf 	from += tm->from_offset;
36d675c989SThomas Graf 
37d675c989SThomas Graf 	to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
38d675c989SThomas Graf 	to += tm->to_offset;
39d675c989SThomas Graf 
40059a2440SBojan Prtvar 	return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
41d675c989SThomas Graf }
42d675c989SThomas Graf 
em_text_change(struct net * net,void * data,int len,struct tcf_ematch * m)4382a470f1SJohn Fastabend static int em_text_change(struct net *net, void *data, int len,
44d675c989SThomas Graf 			  struct tcf_ematch *m)
45d675c989SThomas Graf {
46d675c989SThomas Graf 	struct text_match *tm;
47d675c989SThomas Graf 	struct tcf_em_text *conf = data;
48d675c989SThomas Graf 	struct ts_config *ts_conf;
49d675c989SThomas Graf 	int flags = 0;
50d675c989SThomas Graf 
51d675c989SThomas Graf 	if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
52d675c989SThomas Graf 		return -EINVAL;
53d675c989SThomas Graf 
54d675c989SThomas Graf 	if (conf->from_layer > conf->to_layer)
55d675c989SThomas Graf 		return -EINVAL;
56d675c989SThomas Graf 
57d675c989SThomas Graf 	if (conf->from_layer == conf->to_layer &&
58d675c989SThomas Graf 	    conf->from_offset > conf->to_offset)
59d675c989SThomas Graf 		return -EINVAL;
60d675c989SThomas Graf 
61d675c989SThomas Graf retry:
62d675c989SThomas Graf 	ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
63d675c989SThomas Graf 				     conf->pattern_len, GFP_KERNEL, flags);
64d675c989SThomas Graf 
65d675c989SThomas Graf 	if (flags & TS_AUTOLOAD)
66d675c989SThomas Graf 		rtnl_lock();
67d675c989SThomas Graf 
68d675c989SThomas Graf 	if (IS_ERR(ts_conf)) {
69d675c989SThomas Graf 		if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
70d675c989SThomas Graf 			rtnl_unlock();
71d675c989SThomas Graf 			flags |= TS_AUTOLOAD;
72d675c989SThomas Graf 			goto retry;
73d675c989SThomas Graf 		} else
74d675c989SThomas Graf 			return PTR_ERR(ts_conf);
75d675c989SThomas Graf 	} else if (flags & TS_AUTOLOAD) {
76d675c989SThomas Graf 		textsearch_destroy(ts_conf);
77d675c989SThomas Graf 		return -EAGAIN;
78d675c989SThomas Graf 	}
79d675c989SThomas Graf 
80d675c989SThomas Graf 	tm = kmalloc(sizeof(*tm), GFP_KERNEL);
81d675c989SThomas Graf 	if (tm == NULL) {
82d675c989SThomas Graf 		textsearch_destroy(ts_conf);
83d675c989SThomas Graf 		return -ENOBUFS;
84d675c989SThomas Graf 	}
85d675c989SThomas Graf 
86d675c989SThomas Graf 	tm->from_offset = conf->from_offset;
87d675c989SThomas Graf 	tm->to_offset   = conf->to_offset;
88d675c989SThomas Graf 	tm->from_layer  = conf->from_layer;
89d675c989SThomas Graf 	tm->to_layer    = conf->to_layer;
90d675c989SThomas Graf 	tm->config      = ts_conf;
91d675c989SThomas Graf 
92d675c989SThomas Graf 	m->datalen = sizeof(*tm);
93d675c989SThomas Graf 	m->data = (unsigned long) tm;
94d675c989SThomas Graf 
95d675c989SThomas Graf 	return 0;
96d675c989SThomas Graf }
97d675c989SThomas Graf 
em_text_destroy(struct tcf_ematch * m)9882a470f1SJohn Fastabend static void em_text_destroy(struct tcf_ematch *m)
99d675c989SThomas Graf {
100*4083c9bcSHangyu Hua 	if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config) {
101d675c989SThomas Graf 		textsearch_destroy(EM_TEXT_PRIV(m)->config);
102*4083c9bcSHangyu Hua 		kfree(EM_TEXT_PRIV(m));
103*4083c9bcSHangyu Hua 	}
104d675c989SThomas Graf }
105d675c989SThomas Graf 
em_text_dump(struct sk_buff * skb,struct tcf_ematch * m)106d675c989SThomas Graf static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
107d675c989SThomas Graf {
108d675c989SThomas Graf 	struct text_match *tm = EM_TEXT_PRIV(m);
109d675c989SThomas Graf 	struct tcf_em_text conf;
110d675c989SThomas Graf 
111d675c989SThomas Graf 	strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
112d675c989SThomas Graf 	conf.from_offset = tm->from_offset;
113d675c989SThomas Graf 	conf.to_offset = tm->to_offset;
114d675c989SThomas Graf 	conf.from_layer = tm->from_layer;
115d675c989SThomas Graf 	conf.to_layer = tm->to_layer;
116d675c989SThomas Graf 	conf.pattern_len = textsearch_get_pattern_len(tm->config);
117d675c989SThomas Graf 	conf.pad = 0;
118d675c989SThomas Graf 
119add93b61SPatrick McHardy 	if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
120add93b61SPatrick McHardy 		goto nla_put_failure;
121add93b61SPatrick McHardy 	if (nla_append(skb, conf.pattern_len,
122add93b61SPatrick McHardy 		       textsearch_get_pattern(tm->config)) < 0)
123add93b61SPatrick McHardy 		goto nla_put_failure;
124d675c989SThomas Graf 	return 0;
125d675c989SThomas Graf 
126add93b61SPatrick McHardy nla_put_failure:
127d675c989SThomas Graf 	return -1;
128d675c989SThomas Graf }
129d675c989SThomas Graf 
130d675c989SThomas Graf static struct tcf_ematch_ops em_text_ops = {
131d675c989SThomas Graf 	.kind	  = TCF_EM_TEXT,
132d675c989SThomas Graf 	.change	  = em_text_change,
133d675c989SThomas Graf 	.match	  = em_text_match,
134d675c989SThomas Graf 	.destroy  = em_text_destroy,
135d675c989SThomas Graf 	.dump	  = em_text_dump,
136d675c989SThomas Graf 	.owner	  = THIS_MODULE,
137d675c989SThomas Graf 	.link	  = LIST_HEAD_INIT(em_text_ops.link)
138d675c989SThomas Graf };
139d675c989SThomas Graf 
init_em_text(void)140d675c989SThomas Graf static int __init init_em_text(void)
141d675c989SThomas Graf {
142d675c989SThomas Graf 	return tcf_em_register(&em_text_ops);
143d675c989SThomas Graf }
144d675c989SThomas Graf 
exit_em_text(void)145d675c989SThomas Graf static void __exit exit_em_text(void)
146d675c989SThomas Graf {
147d675c989SThomas Graf 	tcf_em_unregister(&em_text_ops);
148d675c989SThomas Graf }
149d675c989SThomas Graf 
150d675c989SThomas Graf MODULE_LICENSE("GPL");
151d675c989SThomas Graf 
152d675c989SThomas Graf module_init(init_em_text);
153d675c989SThomas Graf module_exit(exit_em_text);
154db3d99c0SPatrick McHardy 
155db3d99c0SPatrick McHardy MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);
156