at commit c03c1a14

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