blob: 091ced38a3762a8096b25b65705f9e2a2b84ba3e [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070021#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_pedit.h>
24#include <net/tc_act/tc_pedit.h>
25
David S. Millere9ce1cd2006-08-21 23:54:55 -070026#define PEDIT_TAB_MASK 15
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
WANG Cong369ba562013-12-15 20:15:08 -080028static struct tcf_hashinfo pedit_hash_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Patrick McHardy53b2bf32008-01-23 20:36:30 -080030static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
jamal53f7e352009-10-11 04:21:38 +000031 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
Patrick McHardy53b2bf32008-01-23 20:36:30 -080032};
33
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000034static int tcf_pedit_init(struct net *net, struct nlattr *nla,
35 struct nlattr *est, struct tc_action *a,
36 int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037{
Patrick McHardy7ba699c2008-01-22 22:11:50 -080038 struct nlattr *tb[TCA_PEDIT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct tc_pedit *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080040 int ret = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct tcf_pedit *p;
42 struct tc_pedit_key *keys = NULL;
43 int ksize;
44
Patrick McHardycee63722008-01-23 20:33:32 -080045 if (nla == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 return -EINVAL;
47
Patrick McHardy53b2bf32008-01-23 20:36:30 -080048 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy);
Patrick McHardycee63722008-01-23 20:33:32 -080049 if (err < 0)
50 return err;
51
Patrick McHardy53b2bf32008-01-23 20:36:30 -080052 if (tb[TCA_PEDIT_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080054 parm = nla_data(tb[TCA_PEDIT_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080056 if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return -EINVAL;
58
WANG Cong86062032014-02-11 17:07:31 -080059 if (!tcf_hash_check(parm->index, a, bind)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (!parm->nkeys)
61 return -EINVAL;
WANG Cong86062032014-02-11 17:07:31 -080062 ret = tcf_hash_create(parm->index, est, a, sizeof(*p), bind);
63 if (ret)
64 return ret;
65 p = to_pedit(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 keys = kmalloc(ksize, GFP_KERNEL);
67 if (keys == NULL) {
WANG Cong86062032014-02-11 17:07:31 -080068 tcf_hash_cleanup(a, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -ENOMEM;
70 }
71 ret = ACT_P_CREATED;
72 } else {
WANG Cong86062032014-02-11 17:07:31 -080073 p = to_pedit(a);
74 tcf_hash_release(a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050075 if (bind)
76 return 0;
77 if (!ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -EEXIST;
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050079
David S. Millere9ce1cd2006-08-21 23:54:55 -070080 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 keys = kmalloc(ksize, GFP_KERNEL);
82 if (keys == NULL)
83 return -ENOMEM;
84 }
85 }
86
David S. Millere9ce1cd2006-08-21 23:54:55 -070087 spin_lock_bh(&p->tcf_lock);
88 p->tcfp_flags = parm->flags;
89 p->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (keys) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070091 kfree(p->tcfp_keys);
92 p->tcfp_keys = keys;
93 p->tcfp_nkeys = parm->nkeys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
David S. Millere9ce1cd2006-08-21 23:54:55 -070095 memcpy(p->tcfp_keys, parm->keys, ksize);
96 spin_unlock_bh(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ret == ACT_P_CREATED)
WANG Cong86062032014-02-11 17:07:31 -080098 tcf_hash_insert(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return ret;
100}
101
David S. Millere9ce1cd2006-08-21 23:54:55 -0700102static int tcf_pedit_cleanup(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700104 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 if (p) {
107 struct tc_pedit_key *keys = p->tcfp_keys;
WANG Cong86062032014-02-11 17:07:31 -0800108 if (tcf_hash_release(a, bind)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 kfree(keys);
110 return 1;
111 }
112 }
113 return 0;
114}
115
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000116static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700117 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700119 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int i, munged = 0;
Changli Gaodb2c2412010-06-02 04:55:02 +0000121 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000123 if (skb_unclone(skb, GFP_ATOMIC))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000124 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Changli Gaodb2c2412010-06-02 04:55:02 +0000126 off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
David S. Millere9ce1cd2006-08-21 23:54:55 -0700128 spin_lock(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130 p->tcf_tm.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David S. Millere9ce1cd2006-08-21 23:54:55 -0700132 if (p->tcfp_nkeys > 0) {
133 struct tc_pedit_key *tkey = p->tcfp_keys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
David S. Millere9ce1cd2006-08-21 23:54:55 -0700135 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000136 u32 *ptr, _data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int offset = tkey->off;
138
139 if (tkey->offmask) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000140 char *d, _d;
141
142 d = skb_header_pointer(skb, off + tkey->at, 1,
143 &_d);
144 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto bad;
Changli Gaodb2c2412010-06-02 04:55:02 +0000146 offset += (*d & tkey->offmask) >> tkey->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 if (offset % 4) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000150 pr_info("tc filter pedit"
151 " offset must be on 32 bit boundaries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 goto bad;
153 }
Bill Nottingham75202e72007-05-31 21:33:35 -0700154 if (offset > 0 && offset > skb->len) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000155 pr_info("tc filter pedit"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300156 " offset %d can't exceed pkt length %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 offset, skb->len);
158 goto bad;
159 }
160
Changli Gaodb2c2412010-06-02 04:55:02 +0000161 ptr = skb_header_pointer(skb, off + offset, 4, &_data);
162 if (!ptr)
163 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* just do it, baby */
165 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
Changli Gaodb2c2412010-06-02 04:55:02 +0000166 if (ptr == &_data)
167 skb_store_bits(skb, off + offset, ptr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 munged++;
169 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (munged)
172 skb->tc_verd = SET_TC_MUNGED(skb->tc_verd);
173 goto done;
stephen hemminger6ff9c362010-05-12 06:37:05 +0000174 } else
175 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177bad:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700178 p->tcf_qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179done:
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000180 bstats_update(&p->tcf_bstats, skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700181 spin_unlock(&p->tcf_lock);
182 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
David S. Millere9ce1cd2006-08-21 23:54:55 -0700185static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
186 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700188 unsigned char *b = skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189 struct tcf_pedit *p = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct tc_pedit *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct tcf_t t;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900192 int s;
193
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 /* netlink spinlocks held above us - must use ATOMIC */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700197 opt = kzalloc(s, GFP_ATOMIC);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700198 if (unlikely(!opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201 memcpy(opt->keys, p->tcfp_keys,
202 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
203 opt->index = p->tcf_index;
204 opt->nkeys = p->tcfp_nkeys;
205 opt->flags = p->tcfp_flags;
206 opt->action = p->tcf_action;
207 opt->refcnt = p->tcf_refcnt - ref;
208 opt->bindcnt = p->tcf_bindcnt - bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David S. Miller1b34ec42012-03-29 05:11:39 -0400210 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
211 goto nla_put_failure;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700212 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
213 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
214 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
David S. Miller1b34ec42012-03-29 05:11:39 -0400215 if (nla_put(skb, TCA_PEDIT_TM, sizeof(t), &t))
216 goto nla_put_failure;
Patrick McHardy541673c82006-01-08 22:17:27 -0800217 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return skb->len;
219
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800220nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700221 nlmsg_trim(skb, b);
Patrick McHardy541673c82006-01-08 22:17:27 -0800222 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -1;
224}
225
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226static struct tc_action_ops act_pedit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .kind = "pedit",
David S. Millere9ce1cd2006-08-21 23:54:55 -0700228 .hinfo = &pedit_hash_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .type = TCA_ACT_PEDIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .owner = THIS_MODULE,
231 .act = tcf_pedit,
232 .dump = tcf_pedit_dump,
233 .cleanup = tcf_pedit_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 .init = tcf_pedit_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
238MODULE_DESCRIPTION("Generic Packet Editor actions");
239MODULE_LICENSE("GPL");
240
David S. Millere9ce1cd2006-08-21 23:54:55 -0700241static int __init pedit_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
WANG Cong568a1532013-12-20 00:08:51 -0800243 int err = tcf_hashinfo_init(&pedit_hash_info, PEDIT_TAB_MASK);
WANG Cong369ba562013-12-15 20:15:08 -0800244 if (err)
245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return tcf_register_action(&act_pedit_ops);
247}
248
David S. Millere9ce1cd2006-08-21 23:54:55 -0700249static void __exit pedit_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
WANG Cong369ba562013-12-15 20:15:08 -0800251 tcf_hashinfo_destroy(&pedit_hash_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 tcf_unregister_action(&act_pedit_ops);
253}
254
255module_init(pedit_init_module);
256module_exit(pedit_cleanup_module);
257