2 * net/sched/em_text.c Textsearch ematch
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Thomas Graf <tgraf@suug.ch>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/skbuff.h>
18 #include <linux/textsearch.h>
19 #include <linux/tc_ematch/tc_em_text.h>
20 #include <net/pkt_cls.h>
27 struct ts_config *config;
30 #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
32 static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
33 struct tcf_pkt_info *info)
35 struct text_match *tm = EM_TEXT_PRIV(m);
38 from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
39 from += tm->from_offset;
41 to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
44 return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
47 static int em_text_change(struct net *net, void *data, int len,
50 struct text_match *tm;
51 struct tcf_em_text *conf = data;
52 struct ts_config *ts_conf;
55 if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
58 if (conf->from_layer > conf->to_layer)
61 if (conf->from_layer == conf->to_layer &&
62 conf->from_offset > conf->to_offset)
66 ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
67 conf->pattern_len, GFP_KERNEL, flags);
69 if (flags & TS_AUTOLOAD)
72 if (IS_ERR(ts_conf)) {
73 if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
78 return PTR_ERR(ts_conf);
79 } else if (flags & TS_AUTOLOAD) {
80 textsearch_destroy(ts_conf);
84 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
86 textsearch_destroy(ts_conf);
90 tm->from_offset = conf->from_offset;
91 tm->to_offset = conf->to_offset;
92 tm->from_layer = conf->from_layer;
93 tm->to_layer = conf->to_layer;
96 m->datalen = sizeof(*tm);
97 m->data = (unsigned long) tm;
102 static void em_text_destroy(struct tcf_ematch *m)
104 if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config)
105 textsearch_destroy(EM_TEXT_PRIV(m)->config);
108 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
110 struct text_match *tm = EM_TEXT_PRIV(m);
111 struct tcf_em_text conf;
113 strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
114 conf.from_offset = tm->from_offset;
115 conf.to_offset = tm->to_offset;
116 conf.from_layer = tm->from_layer;
117 conf.to_layer = tm->to_layer;
118 conf.pattern_len = textsearch_get_pattern_len(tm->config);
121 if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
122 goto nla_put_failure;
123 if (nla_append(skb, conf.pattern_len,
124 textsearch_get_pattern(tm->config)) < 0)
125 goto nla_put_failure;
132 static struct tcf_ematch_ops em_text_ops = {
134 .change = em_text_change,
135 .match = em_text_match,
136 .destroy = em_text_destroy,
137 .dump = em_text_dump,
138 .owner = THIS_MODULE,
139 .link = LIST_HEAD_INIT(em_text_ops.link)
142 static int __init init_em_text(void)
144 return tcf_em_register(&em_text_ops);
147 static void __exit exit_em_text(void)
149 tcf_em_unregister(&em_text_ops);
152 MODULE_LICENSE("GPL");
154 module_init(init_em_text);
155 module_exit(exit_em_text);
157 MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);