blob: d5cd86efb7d08fa8f3564f5edb89aee9557e5840 [file] [log] [blame]
Thomas Grafd675c982005-06-23 21:00:58 -07001/*
2 * net/sched/em_text.c Textsearch ematch
3 *
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.
8 *
9 * Authors: Thomas Graf <tgraf@suug.ch>
10 */
11
Thomas Grafd675c982005-06-23 21:00:58 -070012#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
Thomas Grafd675c982005-06-23 21:00:58 -070015#include <linux/string.h>
16#include <linux/skbuff.h>
17#include <linux/textsearch.h>
18#include <linux/tc_ematch/tc_em_text.h>
19#include <net/pkt_cls.h>
20
21struct text_match
22{
23 u16 from_offset;
24 u16 to_offset;
25 u8 from_layer;
26 u8 to_layer;
27 struct ts_config *config;
28};
29
30#define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
31
32static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
33 struct tcf_pkt_info *info)
34{
35 struct text_match *tm = EM_TEXT_PRIV(m);
36 int from, to;
37 struct ts_state state;
38
39 from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
40 from += tm->from_offset;
41
42 to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
43 to += tm->to_offset;
44
45 return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX;
46}
47
48static int em_text_change(struct tcf_proto *tp, void *data, int len,
49 struct tcf_ematch *m)
50{
51 struct text_match *tm;
52 struct tcf_em_text *conf = data;
53 struct ts_config *ts_conf;
54 int flags = 0;
55
Thomas Grafd675c982005-06-23 21:00:58 -070056 if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
57 return -EINVAL;
58
59 if (conf->from_layer > conf->to_layer)
60 return -EINVAL;
61
62 if (conf->from_layer == conf->to_layer &&
63 conf->from_offset > conf->to_offset)
64 return -EINVAL;
65
66retry:
67 ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
68 conf->pattern_len, GFP_KERNEL, flags);
69
70 if (flags & TS_AUTOLOAD)
71 rtnl_lock();
72
73 if (IS_ERR(ts_conf)) {
74 if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
75 rtnl_unlock();
76 flags |= TS_AUTOLOAD;
77 goto retry;
78 } else
79 return PTR_ERR(ts_conf);
80 } else if (flags & TS_AUTOLOAD) {
81 textsearch_destroy(ts_conf);
82 return -EAGAIN;
83 }
84
85 tm = kmalloc(sizeof(*tm), GFP_KERNEL);
86 if (tm == NULL) {
87 textsearch_destroy(ts_conf);
88 return -ENOBUFS;
89 }
90
91 tm->from_offset = conf->from_offset;
92 tm->to_offset = conf->to_offset;
93 tm->from_layer = conf->from_layer;
94 tm->to_layer = conf->to_layer;
95 tm->config = ts_conf;
96
97 m->datalen = sizeof(*tm);
98 m->data = (unsigned long) tm;
99
100 return 0;
101}
102
103static void em_text_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
104{
105 textsearch_destroy(EM_TEXT_PRIV(m)->config);
106}
107
108static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
109{
110 struct text_match *tm = EM_TEXT_PRIV(m);
111 struct tcf_em_text conf;
112
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);
119 conf.pad = 0;
120
121 RTA_PUT_NOHDR(skb, sizeof(conf), &conf);
122 RTA_APPEND(skb, conf.pattern_len, textsearch_get_pattern(tm->config));
123 return 0;
124
125rtattr_failure:
126 return -1;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900127}
Thomas Grafd675c982005-06-23 21:00:58 -0700128
129static struct tcf_ematch_ops em_text_ops = {
130 .kind = TCF_EM_TEXT,
131 .change = em_text_change,
132 .match = em_text_match,
133 .destroy = em_text_destroy,
134 .dump = em_text_dump,
135 .owner = THIS_MODULE,
136 .link = LIST_HEAD_INIT(em_text_ops.link)
137};
138
139static int __init init_em_text(void)
140{
141 return tcf_em_register(&em_text_ops);
142}
143
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900144static void __exit exit_em_text(void)
Thomas Grafd675c982005-06-23 21:00:58 -0700145{
146 tcf_em_unregister(&em_text_ops);
147}
148
149MODULE_LICENSE("GPL");
150
151module_init(init_em_text);
152module_exit(exit_em_text);
Patrick McHardydb3d99c2007-07-11 19:46:26 -0700153
154MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);