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