Merge branch 'stable/xen-pcifront-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / net / sched / em_text.c
1 /*
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
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>
21
22 struct text_match
23 {
24         u16                     from_offset;
25         u16                     to_offset;
26         u8                      from_layer;
27         u8                      to_layer;
28         struct ts_config        *config;
29 };
30
31 #define EM_TEXT_PRIV(m) ((struct text_match *) (m)->data)
32
33 static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
34                          struct tcf_pkt_info *info)
35 {
36         struct text_match *tm = EM_TEXT_PRIV(m);
37         int from, to;
38         struct ts_state state;
39
40         from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
41         from += tm->from_offset;
42
43         to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
44         to += tm->to_offset;
45
46         return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX;
47 }
48
49 static int em_text_change(struct tcf_proto *tp, void *data, int len,
50                           struct tcf_ematch *m)
51 {
52         struct text_match *tm;
53         struct tcf_em_text *conf = data;
54         struct ts_config *ts_conf;
55         int flags = 0;
56
57         if (len < sizeof(*conf) || len < (sizeof(*conf) + conf->pattern_len))
58                 return -EINVAL;
59
60         if (conf->from_layer > conf->to_layer)
61                 return -EINVAL;
62
63         if (conf->from_layer == conf->to_layer &&
64             conf->from_offset > conf->to_offset)
65                 return -EINVAL;
66
67 retry:
68         ts_conf = textsearch_prepare(conf->algo, (u8 *) conf + sizeof(*conf),
69                                      conf->pattern_len, GFP_KERNEL, flags);
70
71         if (flags & TS_AUTOLOAD)
72                 rtnl_lock();
73
74         if (IS_ERR(ts_conf)) {
75                 if (PTR_ERR(ts_conf) == -ENOENT && !(flags & TS_AUTOLOAD)) {
76                         rtnl_unlock();
77                         flags |= TS_AUTOLOAD;
78                         goto retry;
79                 } else
80                         return PTR_ERR(ts_conf);
81         } else if (flags & TS_AUTOLOAD) {
82                 textsearch_destroy(ts_conf);
83                 return -EAGAIN;
84         }
85
86         tm = kmalloc(sizeof(*tm), GFP_KERNEL);
87         if (tm == NULL) {
88                 textsearch_destroy(ts_conf);
89                 return -ENOBUFS;
90         }
91
92         tm->from_offset = conf->from_offset;
93         tm->to_offset   = conf->to_offset;
94         tm->from_layer  = conf->from_layer;
95         tm->to_layer    = conf->to_layer;
96         tm->config      = ts_conf;
97
98         m->datalen = sizeof(*tm);
99         m->data = (unsigned long) tm;
100
101         return 0;
102 }
103
104 static void em_text_destroy(struct tcf_proto *tp, struct tcf_ematch *m)
105 {
106         if (EM_TEXT_PRIV(m) && EM_TEXT_PRIV(m)->config)
107                 textsearch_destroy(EM_TEXT_PRIV(m)->config);
108 }
109
110 static int em_text_dump(struct sk_buff *skb, struct tcf_ematch *m)
111 {
112         struct text_match *tm = EM_TEXT_PRIV(m);
113         struct tcf_em_text conf;
114
115         strncpy(conf.algo, tm->config->ops->name, sizeof(conf.algo) - 1);
116         conf.from_offset = tm->from_offset;
117         conf.to_offset = tm->to_offset;
118         conf.from_layer = tm->from_layer;
119         conf.to_layer = tm->to_layer;
120         conf.pattern_len = textsearch_get_pattern_len(tm->config);
121         conf.pad = 0;
122
123         if (nla_put_nohdr(skb, sizeof(conf), &conf) < 0)
124                 goto nla_put_failure;
125         if (nla_append(skb, conf.pattern_len,
126                        textsearch_get_pattern(tm->config)) < 0)
127                 goto nla_put_failure;
128         return 0;
129
130 nla_put_failure:
131         return -1;
132 }
133
134 static struct tcf_ematch_ops em_text_ops = {
135         .kind     = TCF_EM_TEXT,
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)
142 };
143
144 static int __init init_em_text(void)
145 {
146         return tcf_em_register(&em_text_ops);
147 }
148
149 static void __exit exit_em_text(void)
150 {
151         tcf_em_unregister(&em_text_ops);
152 }
153
154 MODULE_LICENSE("GPL");
155
156 module_init(init_em_text);
157 module_exit(exit_em_text);
158
159 MODULE_ALIAS_TCF_EMATCH(TCF_EM_TEXT);