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/config.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/string.h>
18 #include <linux/skbuff.h>
19 #include <linux/textsearch.h>
20 #include <linux/tc_ematch/tc_em_text.h>
21 #include <net/pkt_cls.h>
29 struct ts_config *config;
32 #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
34 static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
35 struct tcf_pkt_info *info)
37 struct text_match *tm = EM_TEXT_PRIV(m);
39 struct ts_state state;
41 from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
42 from += tm->from_offset;
44 to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
47 return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX;
50 static int em_text_change(struct tcf_proto *tp, void *data, int len,
53 struct text_match *tm;
54 struct tcf_em_text *conf = data;
55 struct ts_config *ts_conf;
58 printk("Configuring text: %s from %d:%d to %d:%d len %d\n", conf->algo, conf->from_offset,
59 conf->from_layer, conf->to_offset, conf->to_layer, conf->pattern_len);
61 if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
64 if (conf->from_layer > conf->to_layer)
67 if (conf->from_layer == conf->to_layer &&
68 conf->from_offset > conf->to_offset)
72 ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
73 conf->pattern_len, GFP_KERNEL, flags);
75 if (flags & TS_AUTOLOAD)
78 if (IS_ERR(ts_conf)) {
79 if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
84 return PTR_ERR(ts_conf);
85 } else if (flags & TS_AUTOLOAD) {
86 textsearch_destroy(ts_conf);
90 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
92 textsearch_destroy(ts_conf);
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;
102 m->datalen = sizeof(*tm);
103 m->data = (unsigned long) tm;
108 static void em_text_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
110 textsearch_destroy(EM_TEXT_PRIV(m)->config);
113 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
115 struct text_match *tm = EM_TEXT_PRIV(m);
116 struct tcf_em_text conf;
118 strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
119 conf.from_offset = tm->from_offset;
120 conf.to_offset = tm->to_offset;
121 conf.from_layer = tm->from_layer;
122 conf.to_layer = tm->to_layer;
123 conf.pattern_len = textsearch_get_pattern_len(tm->config);
126 RTA_PUT_NOHDR(skb, sizeof(conf), &conf);
127 RTA_APPEND(skb, conf.pattern_len, textsearch_get_pattern(tm->config));
134 static struct tcf_ematch_ops em_text_ops = {
136 .change = em_text_change,
137 .match = em_text_match,
138 .destroy = em_text_destroy,
139 .dump = em_text_dump,
140 .owner = THIS_MODULE,
141 .link = LIST_HEAD_INIT(em_text_ops.link)
144 static int __init init_em_text(void)
146 return tcf_em_register(&em_text_ops);
149 static void __exit exit_em_text(void)
151 tcf_em_unregister(&em_text_ops);
154 MODULE_LICENSE("GPL");
156 module_init(init_em_text);
157 module_exit(exit_em_text);