18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * net/sched/em_text.c	Textsearch ematch
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Authors:	Thomas Graf <tgraf@suug.ch>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/slab.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/string.h>
138c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
148c2ecf20Sopenharmony_ci#include <linux/textsearch.h>
158c2ecf20Sopenharmony_ci#include <linux/tc_ematch/tc_em_text.h>
168c2ecf20Sopenharmony_ci#include <net/pkt_cls.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct text_match {
198c2ecf20Sopenharmony_ci	u16			from_offset;
208c2ecf20Sopenharmony_ci	u16			to_offset;
218c2ecf20Sopenharmony_ci	u8			from_layer;
228c2ecf20Sopenharmony_ci	u8			to_layer;
238c2ecf20Sopenharmony_ci	struct ts_config	*config;
248c2ecf20Sopenharmony_ci};
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistatic int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
298c2ecf20Sopenharmony_ci			 struct tcf_pkt_info *info)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct text_match *tm = EM_TEXT_PRIV(m);
328c2ecf20Sopenharmony_ci	int from, to;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
358c2ecf20Sopenharmony_ci	from += tm->from_offset;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
388c2ecf20Sopenharmony_ci	to += tm->to_offset;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
418c2ecf20Sopenharmony_ci}
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic int em_text_change(struct net *net, void *data, int len,
448c2ecf20Sopenharmony_ci			  struct tcf_ematch *m)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct text_match *tm;
478c2ecf20Sopenharmony_ci	struct tcf_em_text *conf = data;
488c2ecf20Sopenharmony_ci	struct ts_config *ts_conf;
498c2ecf20Sopenharmony_ci	int flags = 0;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
528c2ecf20Sopenharmony_ci		return -EINVAL;
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	if (conf->from_layer > conf->to_layer)
558c2ecf20Sopenharmony_ci		return -EINVAL;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (conf->from_layer == conf->to_layer &&
588c2ecf20Sopenharmony_ci	    conf->from_offset > conf->to_offset)
598c2ecf20Sopenharmony_ci		return -EINVAL;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciretry:
628c2ecf20Sopenharmony_ci	ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
638c2ecf20Sopenharmony_ci				     conf->pattern_len, GFP_KERNEL, flags);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (flags & TS_AUTOLOAD)
668c2ecf20Sopenharmony_ci		rtnl_lock();
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (IS_ERR(ts_conf)) {
698c2ecf20Sopenharmony_ci		if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
708c2ecf20Sopenharmony_ci			rtnl_unlock();
718c2ecf20Sopenharmony_ci			flags |= TS_AUTOLOAD;
728c2ecf20Sopenharmony_ci			goto retry;
738c2ecf20Sopenharmony_ci		} else
748c2ecf20Sopenharmony_ci			return PTR_ERR(ts_conf);
758c2ecf20Sopenharmony_ci	} else if (flags & TS_AUTOLOAD) {
768c2ecf20Sopenharmony_ci		textsearch_destroy(ts_conf);
778c2ecf20Sopenharmony_ci		return -EAGAIN;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	tm = kmalloc(sizeof(*tm), GFP_KERNEL);
818c2ecf20Sopenharmony_ci	if (tm == NULL) {
828c2ecf20Sopenharmony_ci		textsearch_destroy(ts_conf);
838c2ecf20Sopenharmony_ci		return -ENOBUFS;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	tm->from_offset = conf->from_offset;
878c2ecf20Sopenharmony_ci	tm->to_offset   = conf->to_offset;
888c2ecf20Sopenharmony_ci	tm->from_layer  = conf->from_layer;
898c2ecf20Sopenharmony_ci	tm->to_layer    = conf->to_layer;
908c2ecf20Sopenharmony_ci	tm->config      = ts_conf;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	m->datalen = sizeof(*tm);
938c2ecf20Sopenharmony_ci	m->data = (unsigned long) tm;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void em_text_destroy(struct tcf_ematch *m)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config) {
1018c2ecf20Sopenharmony_ci		textsearch_destroy(EM_TEXT_PRIV(m)->config);
1028c2ecf20Sopenharmony_ci		kfree(EM_TEXT_PRIV(m));
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct text_match *tm = EM_TEXT_PRIV(m);
1098c2ecf20Sopenharmony_ci	struct tcf_em_text conf;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
1128c2ecf20Sopenharmony_ci	conf.from_offset = tm->from_offset;
1138c2ecf20Sopenharmony_ci	conf.to_offset = tm->to_offset;
1148c2ecf20Sopenharmony_ci	conf.from_layer = tm->from_layer;
1158c2ecf20Sopenharmony_ci	conf.to_layer = tm->to_layer;
1168c2ecf20Sopenharmony_ci	conf.pattern_len = textsearch_get_pattern_len(tm->config);
1178c2ecf20Sopenharmony_ci	conf.pad = 0;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
1208c2ecf20Sopenharmony_ci		goto nla_put_failure;
1218c2ecf20Sopenharmony_ci	if (nla_append(skb, conf.pattern_len,
1228c2ecf20Sopenharmony_ci		       textsearch_get_pattern(tm->config)) < 0)
1238c2ecf20Sopenharmony_ci		goto nla_put_failure;
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cinla_put_failure:
1278c2ecf20Sopenharmony_ci	return -1;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic struct tcf_ematch_ops em_text_ops = {
1318c2ecf20Sopenharmony_ci	.kind	  = TCF_EM_TEXT,
1328c2ecf20Sopenharmony_ci	.change	  = em_text_change,
1338c2ecf20Sopenharmony_ci	.match	  = em_text_match,
1348c2ecf20Sopenharmony_ci	.destroy  = em_text_destroy,
1358c2ecf20Sopenharmony_ci	.dump	  = em_text_dump,
1368c2ecf20Sopenharmony_ci	.owner	  = THIS_MODULE,
1378c2ecf20Sopenharmony_ci	.link	  = LIST_HEAD_INIT(em_text_ops.link)
1388c2ecf20Sopenharmony_ci};
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic int __init init_em_text(void)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return tcf_em_register(&em_text_ops);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic void __exit exit_em_text(void)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	tcf_em_unregister(&em_text_ops);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cimodule_init(init_em_text);
1538c2ecf20Sopenharmony_cimodule_exit(exit_em_text);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciMODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);
156