]> Pileus Git - ~andy/linux/blob - net/sched/act_pedit.c
Merge tag 'batman-adv-for-davem' of git://git.open-mesh.org/linux-merge
[~andy/linux] / net / sched / act_pedit.c
1 /*
2  * net/sched/pedit.c    Generic packet editor
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:     Jamal Hadi Salim (2002-4)
10  */
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/skbuff.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <net/netlink.h>
22 #include <net/pkt_sched.h>
23 #include <linux/tc_act/tc_pedit.h>
24 #include <net/tc_act/tc_pedit.h>
25
26 #define PEDIT_TAB_MASK  15
27
28 static struct tcf_hashinfo pedit_hash_info;
29
30 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
31         [TCA_PEDIT_PARMS]       = { .len = sizeof(struct tc_pedit) },
32 };
33
34 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
35                           struct nlattr *est, struct tc_action *a,
36                           int ovr, int bind)
37 {
38         struct nlattr *tb[TCA_PEDIT_MAX + 1];
39         struct tc_pedit *parm;
40         int ret = 0, err;
41         struct tcf_pedit *p;
42         struct tcf_common *pc;
43         struct tc_pedit_key *keys = NULL;
44         int ksize;
45
46         if (nla == NULL)
47                 return -EINVAL;
48
49         err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy);
50         if (err < 0)
51                 return err;
52
53         if (tb[TCA_PEDIT_PARMS] == NULL)
54                 return -EINVAL;
55         parm = nla_data(tb[TCA_PEDIT_PARMS]);
56         ksize = parm->nkeys * sizeof(struct tc_pedit_key);
57         if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
58                 return -EINVAL;
59
60         pc = tcf_hash_check(parm->index, a, bind, &pedit_hash_info);
61         if (!pc) {
62                 if (!parm->nkeys)
63                         return -EINVAL;
64                 pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
65                                      &pedit_hash_info);
66                 if (IS_ERR(pc))
67                         return PTR_ERR(pc);
68                 p = to_pedit(pc);
69                 keys = kmalloc(ksize, GFP_KERNEL);
70                 if (keys == NULL) {
71                         if (est)
72                                 gen_kill_estimator(&pc->tcfc_bstats,
73                                                    &pc->tcfc_rate_est);
74                         kfree_rcu(pc, tcfc_rcu);
75                         return -ENOMEM;
76                 }
77                 ret = ACT_P_CREATED;
78         } else {
79                 p = to_pedit(pc);
80                 tcf_hash_release(pc, bind, &pedit_hash_info);
81                 if (bind)
82                         return 0;
83                 if (!ovr)
84                         return -EEXIST;
85
86                 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
87                         keys = kmalloc(ksize, GFP_KERNEL);
88                         if (keys == NULL)
89                                 return -ENOMEM;
90                 }
91         }
92
93         spin_lock_bh(&p->tcf_lock);
94         p->tcfp_flags = parm->flags;
95         p->tcf_action = parm->action;
96         if (keys) {
97                 kfree(p->tcfp_keys);
98                 p->tcfp_keys = keys;
99                 p->tcfp_nkeys = parm->nkeys;
100         }
101         memcpy(p->tcfp_keys, parm->keys, ksize);
102         spin_unlock_bh(&p->tcf_lock);
103         if (ret == ACT_P_CREATED)
104                 tcf_hash_insert(pc, &pedit_hash_info);
105         return ret;
106 }
107
108 static int tcf_pedit_cleanup(struct tc_action *a, int bind)
109 {
110         struct tcf_pedit *p = a->priv;
111
112         if (p) {
113                 struct tc_pedit_key *keys = p->tcfp_keys;
114                 if (tcf_hash_release(&p->common, bind, &pedit_hash_info)) {
115                         kfree(keys);
116                         return 1;
117                 }
118         }
119         return 0;
120 }
121
122 static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
123                      struct tcf_result *res)
124 {
125         struct tcf_pedit *p = a->priv;
126         int i, munged = 0;
127         unsigned int off;
128
129         if (skb_unclone(skb, GFP_ATOMIC))
130                 return p->tcf_action;
131
132         off = skb_network_offset(skb);
133
134         spin_lock(&p->tcf_lock);
135
136         p->tcf_tm.lastuse = jiffies;
137
138         if (p->tcfp_nkeys > 0) {
139                 struct tc_pedit_key *tkey = p->tcfp_keys;
140
141                 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
142                         u32 *ptr, _data;
143                         int offset = tkey->off;
144
145                         if (tkey->offmask) {
146                                 char *d, _d;
147
148                                 d = skb_header_pointer(skb, off + tkey->at, 1,
149                                                        &_d);
150                                 if (!d)
151                                         goto bad;
152                                 offset += (*d & tkey->offmask) >> tkey->shift;
153                         }
154
155                         if (offset % 4) {
156                                 pr_info("tc filter pedit"
157                                         " offset must be on 32 bit boundaries\n");
158                                 goto bad;
159                         }
160                         if (offset > 0 && offset > skb->len) {
161                                 pr_info("tc filter pedit"
162                                         " offset %d can't exceed pkt length %d\n",
163                                        offset, skb->len);
164                                 goto bad;
165                         }
166
167                         ptr = skb_header_pointer(skb, off + offset, 4, &_data);
168                         if (!ptr)
169                                 goto bad;
170                         /* just do it, baby */
171                         *ptr = ((*ptr & tkey->mask) ^ tkey->val);
172                         if (ptr == &_data)
173                                 skb_store_bits(skb, off + offset, ptr, 4);
174                         munged++;
175                 }
176
177                 if (munged)
178                         skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
179                 goto done;
180         } else
181                 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
182
183 bad:
184         p->tcf_qstats.overlimits++;
185 done:
186         bstats_update(&p->tcf_bstats, skb);
187         spin_unlock(&p->tcf_lock);
188         return p->tcf_action;
189 }
190
191 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
192                           int bind, int ref)
193 {
194         unsigned char *b = skb_tail_pointer(skb);
195         struct tcf_pedit *p = a->priv;
196         struct tc_pedit *opt;
197         struct tcf_t t;
198         int s;
199
200         s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
201
202         /* netlink spinlocks held above us - must use ATOMIC */
203         opt = kzalloc(s, GFP_ATOMIC);
204         if (unlikely(!opt))
205                 return -ENOBUFS;
206
207         memcpy(opt->keys, p->tcfp_keys,
208                p->tcfp_nkeys * sizeof(struct tc_pedit_key));
209         opt->index = p->tcf_index;
210         opt->nkeys = p->tcfp_nkeys;
211         opt->flags = p->tcfp_flags;
212         opt->action = p->tcf_action;
213         opt->refcnt = p->tcf_refcnt - ref;
214         opt->bindcnt = p->tcf_bindcnt - bind;
215
216         if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
217                 goto nla_put_failure;
218         t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
219         t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
220         t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
221         if (nla_put(skb, TCA_PEDIT_TM, sizeof(t), &t))
222                 goto nla_put_failure;
223         kfree(opt);
224         return skb->len;
225
226 nla_put_failure:
227         nlmsg_trim(skb, b);
228         kfree(opt);
229         return -1;
230 }
231
232 static struct tc_action_ops act_pedit_ops = {
233         .kind           =       "pedit",
234         .hinfo          =       &pedit_hash_info,
235         .type           =       TCA_ACT_PEDIT,
236         .capab          =       TCA_CAP_NONE,
237         .owner          =       THIS_MODULE,
238         .act            =       tcf_pedit,
239         .dump           =       tcf_pedit_dump,
240         .cleanup        =       tcf_pedit_cleanup,
241         .init           =       tcf_pedit_init,
242 };
243
244 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
245 MODULE_DESCRIPTION("Generic Packet Editor actions");
246 MODULE_LICENSE("GPL");
247
248 static int __init pedit_init_module(void)
249 {
250         int err = tcf_hashinfo_init(&pedit_hash_info, PEDIT_TAB_MASK);
251         if (err)
252                 return err;
253         return tcf_register_action(&act_pedit_ops);
254 }
255
256 static void __exit pedit_cleanup_module(void)
257 {
258         tcf_hashinfo_destroy(&pedit_hash_info);
259         tcf_unregister_action(&act_pedit_ops);
260 }
261
262 module_init(pedit_init_module);
263 module_exit(pedit_cleanup_module);
264