blob: 1b9ca45a78e54a5d16ed0c91505d258766c7c1ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/skbuff.h>
17#include <linux/rtnetlink.h>
18#include <linux/module.h>
19#include <linux/init.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070020#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/pkt_sched.h>
22#include <linux/tc_act/tc_pedit.h>
23#include <net/tc_act/tc_pedit.h>
24
David S. Millere9ce1cd2006-08-21 23:54:55 -070025#define PEDIT_TAB_MASK 15
26static struct tcf_common *tcf_pedit_ht[PEDIT_TAB_MASK + 1];
27static u32 pedit_idx_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static DEFINE_RWLOCK(pedit_lock);
29
David S. Millere9ce1cd2006-08-21 23:54:55 -070030static struct tcf_hashinfo pedit_hash_info = {
31 .htab = tcf_pedit_ht,
32 .hmask = PEDIT_TAB_MASK,
33 .lock = &pedit_lock,
34};
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Patrick McHardy7ba699c2008-01-22 22:11:50 -080036static int tcf_pedit_init(struct nlattr *nla, struct nlattr *est,
David S. Millere9ce1cd2006-08-21 23:54:55 -070037 struct tc_action *a, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Patrick McHardy7ba699c2008-01-22 22:11:50 -080039 struct nlattr *tb[TCA_PEDIT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct tc_pedit *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080041 int ret = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct tcf_pedit *p;
David S. Millere9ce1cd2006-08-21 23:54:55 -070043 struct tcf_common *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct tc_pedit_key *keys = NULL;
45 int ksize;
46
Patrick McHardycee63722008-01-23 20:33:32 -080047 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 return -EINVAL;
49
Patrick McHardycee63722008-01-23 20:33:32 -080050 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, NULL);
51 if (err < 0)
52 return err;
53
Patrick McHardy7ba699c2008-01-22 22:11:50 -080054 if (tb[TCA_PEDIT_PARMS] == NULL ||
55 nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080057 parm = nla_data(tb[TCA_PEDIT_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080059 if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EINVAL;
61
David S. Millere9ce1cd2006-08-21 23:54:55 -070062 pc = tcf_hash_check(parm->index, a, bind, &pedit_hash_info);
63 if (!pc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (!parm->nkeys)
65 return -EINVAL;
David S. Millere9ce1cd2006-08-21 23:54:55 -070066 pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind,
67 &pedit_idx_gen, &pedit_hash_info);
68 if (unlikely(!pc))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -ENOMEM;
David S. Millere9ce1cd2006-08-21 23:54:55 -070070 p = to_pedit(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 keys = kmalloc(ksize, GFP_KERNEL);
72 if (keys == NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070073 kfree(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return -ENOMEM;
75 }
76 ret = ACT_P_CREATED;
77 } else {
David S. Millere9ce1cd2006-08-21 23:54:55 -070078 p = to_pedit(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (!ovr) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070080 tcf_hash_release(pc, bind, &pedit_hash_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EEXIST;
82 }
David S. Millere9ce1cd2006-08-21 23:54:55 -070083 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 keys = kmalloc(ksize, GFP_KERNEL);
85 if (keys == NULL)
86 return -ENOMEM;
87 }
88 }
89
David S. Millere9ce1cd2006-08-21 23:54:55 -070090 spin_lock_bh(&p->tcf_lock);
91 p->tcfp_flags = parm->flags;
92 p->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (keys) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070094 kfree(p->tcfp_keys);
95 p->tcfp_keys = keys;
96 p->tcfp_nkeys = parm->nkeys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
David S. Millere9ce1cd2006-08-21 23:54:55 -070098 memcpy(p->tcfp_keys, parm->keys, ksize);
99 spin_unlock_bh(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (ret == ACT_P_CREATED)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700101 tcf_hash_insert(pc, &pedit_hash_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return ret;
103}
104
David S. Millere9ce1cd2006-08-21 23:54:55 -0700105static int tcf_pedit_cleanup(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700107 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
David S. Millere9ce1cd2006-08-21 23:54:55 -0700109 if (p) {
110 struct tc_pedit_key *keys = p->tcfp_keys;
111 if (tcf_hash_release(&p->common, bind, &pedit_hash_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 kfree(keys);
113 return 1;
114 }
115 }
116 return 0;
117}
118
David S. Millere9ce1cd2006-08-21 23:54:55 -0700119static int tcf_pedit(struct sk_buff *skb, struct tc_action *a,
120 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700122 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int i, munged = 0;
124 u8 *pptr;
125
126 if (!(skb->tc_verd & TC_OK2MUNGE)) {
127 /* should we set skb->cloned? */
128 if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700129 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 }
132
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700133 pptr = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 spin_lock(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David S. Millere9ce1cd2006-08-21 23:54:55 -0700137 p->tcf_tm.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
David S. Millere9ce1cd2006-08-21 23:54:55 -0700139 if (p->tcfp_nkeys > 0) {
140 struct tc_pedit_key *tkey = p->tcfp_keys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
David S. Millere9ce1cd2006-08-21 23:54:55 -0700142 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 u32 *ptr;
144 int offset = tkey->off;
145
146 if (tkey->offmask) {
147 if (skb->len > tkey->at) {
148 char *j = pptr + tkey->at;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900149 offset += ((*j & tkey->offmask) >>
150 tkey->shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else {
152 goto bad;
153 }
154 }
155
156 if (offset % 4) {
157 printk("offset must be on 32 bit boundaries\n");
158 goto bad;
159 }
Bill Nottingham75202e72007-05-31 21:33:35 -0700160 if (offset > 0 && offset > skb->len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 printk("offset %d cant exceed pkt length %d\n",
162 offset, skb->len);
163 goto bad;
164 }
165
166 ptr = (u32 *)(pptr+offset);
167 /* just do it, baby */
168 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
169 munged++;
170 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (munged)
173 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
174 goto done;
175 } else {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700176 printk("pedit BUG: index %d\n", p->tcf_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
179bad:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700180 p->tcf_qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181done:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700182 p->tcf_bstats.bytes += skb->len;
183 p->tcf_bstats.packets++;
184 spin_unlock(&p->tcf_lock);
185 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
David S. Millere9ce1cd2006-08-21 23:54:55 -0700188static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
189 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700191 unsigned char *b = skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct tc_pedit *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct tcf_t t;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900195 int s;
196
David S. Millere9ce1cd2006-08-21 23:54:55 -0700197 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 /* netlink spinlocks held above us - must use ATOMIC */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700200 opt = kzalloc(s, GFP_ATOMIC);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201 if (unlikely(!opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David S. Millere9ce1cd2006-08-21 23:54:55 -0700204 memcpy(opt->keys, p->tcfp_keys,
205 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
206 opt->index = p->tcf_index;
207 opt->nkeys = p->tcfp_nkeys;
208 opt->flags = p->tcfp_flags;
209 opt->action = p->tcf_action;
210 opt->refcnt = p->tcf_refcnt - ref;
211 opt->bindcnt = p->tcf_bindcnt - bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800213 NLA_PUT(skb, TCA_PEDIT_PARMS, s, opt);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
215 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
216 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800217 NLA_PUT(skb, TCA_PEDIT_TM, sizeof(t), &t);
Patrick McHardy541673c82006-01-08 22:17:27 -0800218 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return skb->len;
220
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800221nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700222 nlmsg_trim(skb, b);
Patrick McHardy541673c82006-01-08 22:17:27 -0800223 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -1;
225}
226
David S. Millere9ce1cd2006-08-21 23:54:55 -0700227static struct tc_action_ops act_pedit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 .kind = "pedit",
David S. Millere9ce1cd2006-08-21 23:54:55 -0700229 .hinfo = &pedit_hash_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .type = TCA_ACT_PEDIT,
231 .capab = TCA_CAP_NONE,
232 .owner = THIS_MODULE,
233 .act = tcf_pedit,
234 .dump = tcf_pedit_dump,
235 .cleanup = tcf_pedit_cleanup,
236 .lookup = tcf_hash_search,
237 .init = tcf_pedit_init,
238 .walk = tcf_generic_walker
239};
240
241MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
242MODULE_DESCRIPTION("Generic Packet Editor actions");
243MODULE_LICENSE("GPL");
244
David S. Millere9ce1cd2006-08-21 23:54:55 -0700245static int __init pedit_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 return tcf_register_action(&act_pedit_ops);
248}
249
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250static void __exit pedit_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 tcf_unregister_action(&act_pedit_ops);
253}
254
255module_init(pedit_init_module);
256module_exit(pedit_cleanup_module);
257