blob: cf9b2fe8eac6032f46a570c1482de601b41ac74f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jiri Pirko0c6965d2014-11-05 20:51:51 +01002 * net/sched/act_pedit.c Generic packet editor
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Congddf97cc2016-02-22 15:57:53 -080028static int pedit_net_id;
WANG Conga85a9702016-07-25 16:09:41 -070029static struct tc_action_ops act_pedit_ops;
WANG Congddf97cc2016-02-22 15:57:53 -080030
Patrick McHardy53b2bf32008-01-23 20:36:30 -080031static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
jamal53f7e352009-10-11 04:21:38 +000032 [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) },
Patrick McHardy53b2bf32008-01-23 20:36:30 -080033};
34
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000035static int tcf_pedit_init(struct net *net, struct nlattr *nla,
WANG Conga85a9702016-07-25 16:09:41 -070036 struct nlattr *est, struct tc_action **a,
Benjamin LaHaisec1b52732013-01-14 05:15:39 +000037 int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
WANG Congddf97cc2016-02-22 15:57:53 -080039 struct tc_action_net *tn = net_generic(net, pedit_net_id);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080040 struct nlattr *tb[TCA_PEDIT_MAX + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct tc_pedit *parm;
Patrick McHardycee63722008-01-23 20:33:32 -080042 int ret = 0, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct tcf_pedit *p;
44 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 McHardy53b2bf32008-01-23 20:36:30 -080050 err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy);
Patrick McHardycee63722008-01-23 20:33:32 -080051 if (err < 0)
52 return err;
53
Patrick McHardy53b2bf32008-01-23 20:36:30 -080054 if (tb[TCA_PEDIT_PARMS] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return -EINVAL;
Patrick McHardy7ba699c2008-01-22 22:11:50 -080056 parm = nla_data(tb[TCA_PEDIT_PARMS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080058 if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EINVAL;
60
WANG Congddf97cc2016-02-22 15:57:53 -080061 if (!tcf_hash_check(tn, parm->index, a, bind)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (!parm->nkeys)
63 return -EINVAL;
WANG Congddf97cc2016-02-22 15:57:53 -080064 ret = tcf_hash_create(tn, parm->index, est, a,
WANG Conga85a9702016-07-25 16:09:41 -070065 &act_pedit_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -080066 if (ret)
67 return ret;
WANG Conga85a9702016-07-25 16:09:41 -070068 p = to_pedit(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 keys = kmalloc(ksize, GFP_KERNEL);
70 if (keys == NULL) {
WANG Conga85a9702016-07-25 16:09:41 -070071 tcf_hash_cleanup(*a, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return -ENOMEM;
73 }
74 ret = ACT_P_CREATED;
75 } else {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050076 if (bind)
77 return 0;
WANG Conga85a9702016-07-25 16:09:41 -070078 tcf_hash_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050079 if (!ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EEXIST;
WANG Conga85a9702016-07-25 16:09:41 -070081 p = to_pedit(*a);
David S. Millere9ce1cd2006-08-21 23:54:55 -070082 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 keys = kmalloc(ksize, GFP_KERNEL);
84 if (keys == NULL)
85 return -ENOMEM;
86 }
87 }
88
David S. Millere9ce1cd2006-08-21 23:54:55 -070089 spin_lock_bh(&p->tcf_lock);
90 p->tcfp_flags = parm->flags;
91 p->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 if (keys) {
David S. Millere9ce1cd2006-08-21 23:54:55 -070093 kfree(p->tcfp_keys);
94 p->tcfp_keys = keys;
95 p->tcfp_nkeys = parm->nkeys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
David S. Millere9ce1cd2006-08-21 23:54:55 -070097 memcpy(p->tcfp_keys, parm->keys, ksize);
98 spin_unlock_bh(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (ret == ACT_P_CREATED)
WANG Conga85a9702016-07-25 16:09:41 -0700100 tcf_hash_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return ret;
102}
103
WANG Conga5b5c952014-02-11 17:07:32 -0800104static void tcf_pedit_cleanup(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
WANG Conga85a9702016-07-25 16:09:41 -0700106 struct tcf_pedit *p = to_pedit(a);
WANG Conga5b5c952014-02-11 17:07:32 -0800107 struct tc_pedit_key *keys = p->tcfp_keys;
108 kfree(keys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Amir Vadai95c20272016-11-28 12:56:40 +0200111static bool offset_valid(struct sk_buff *skb, int offset)
112{
113 if (offset > 0 && offset > skb->len)
114 return false;
115
116 if (offset < 0 && -offset > skb_headroom(skb))
117 return false;
118
119 return true;
120}
121
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000122static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700123 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
WANG Conga85a9702016-07-25 16:09:41 -0700125 struct tcf_pedit *p = to_pedit(a);
Florian Westphal4749c3ef2015-04-30 12:12:00 +0200126 int i;
Changli Gaodb2c2412010-06-02 04:55:02 +0000127 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000129 if (skb_unclone(skb, GFP_ATOMIC))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000130 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Changli Gaodb2c2412010-06-02 04:55:02 +0000132 off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David S. Millere9ce1cd2006-08-21 23:54:55 -0700134 spin_lock(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400136 tcf_lastuse_update(&p->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David S. Millere9ce1cd2006-08-21 23:54:55 -0700138 if (p->tcfp_nkeys > 0) {
139 struct tc_pedit_key *tkey = p->tcfp_keys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
David S. Millere9ce1cd2006-08-21 23:54:55 -0700141 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000142 u32 *ptr, _data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int offset = tkey->off;
144
145 if (tkey->offmask) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000146 char *d, _d;
147
Amir Vadai95c20272016-11-28 12:56:40 +0200148 if (!offset_valid(skb, off + tkey->at)) {
149 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
150 off + tkey->at);
151 goto bad;
152 }
Changli Gaodb2c2412010-06-02 04:55:02 +0000153 d = skb_header_pointer(skb, off + tkey->at, 1,
154 &_d);
155 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto bad;
Changli Gaodb2c2412010-06-02 04:55:02 +0000157 offset += (*d & tkey->offmask) >> tkey->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
160 if (offset % 4) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000161 pr_info("tc filter pedit"
162 " offset must be on 32 bit boundaries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto bad;
164 }
Amir Vadai95c20272016-11-28 12:56:40 +0200165
166 if (!offset_valid(skb, off + offset)) {
167 pr_info("tc filter pedit offset %d out of bounds\n",
168 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto bad;
170 }
171
Changli Gaodb2c2412010-06-02 04:55:02 +0000172 ptr = skb_header_pointer(skb, off + offset, 4, &_data);
173 if (!ptr)
174 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 /* just do it, baby */
176 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
Changli Gaodb2c2412010-06-02 04:55:02 +0000177 if (ptr == &_data)
178 skb_store_bits(skb, off + offset, ptr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 goto done;
stephen hemminger6ff9c362010-05-12 06:37:05 +0000182 } else
183 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185bad:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186 p->tcf_qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187done:
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000188 bstats_update(&p->tcf_bstats, skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700189 spin_unlock(&p->tcf_lock);
190 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
194 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700196 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700197 struct tcf_pedit *p = to_pedit(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct tc_pedit *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct tcf_t t;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900200 int s;
201
David S. Millere9ce1cd2006-08-21 23:54:55 -0700202 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* netlink spinlocks held above us - must use ATOMIC */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700205 opt = kzalloc(s, GFP_ATOMIC);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700206 if (unlikely(!opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 memcpy(opt->keys, p->tcfp_keys,
210 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
211 opt->index = p->tcf_index;
212 opt->nkeys = p->tcfp_nkeys;
213 opt->flags = p->tcfp_flags;
214 opt->action = p->tcf_action;
215 opt->refcnt = p->tcf_refcnt - ref;
216 opt->bindcnt = p->tcf_bindcnt - bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David S. Miller1b34ec42012-03-29 05:11:39 -0400218 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
219 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400220
221 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200222 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400223 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400224
Patrick McHardy541673c82006-01-08 22:17:27 -0800225 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return skb->len;
227
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800228nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700229 nlmsg_trim(skb, b);
Patrick McHardy541673c82006-01-08 22:17:27 -0800230 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return -1;
232}
233
WANG Congddf97cc2016-02-22 15:57:53 -0800234static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
235 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700236 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800237{
238 struct tc_action_net *tn = net_generic(net, pedit_net_id);
239
WANG Conga85a9702016-07-25 16:09:41 -0700240 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800241}
242
WANG Conga85a9702016-07-25 16:09:41 -0700243static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800244{
245 struct tc_action_net *tn = net_generic(net, pedit_net_id);
246
247 return tcf_hash_search(tn, a, index);
248}
249
David S. Millere9ce1cd2006-08-21 23:54:55 -0700250static struct tc_action_ops act_pedit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .kind = "pedit",
252 .type = TCA_ACT_PEDIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .owner = THIS_MODULE,
254 .act = tcf_pedit,
255 .dump = tcf_pedit_dump,
256 .cleanup = tcf_pedit_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 .init = tcf_pedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800258 .walk = tcf_pedit_walker,
259 .lookup = tcf_pedit_search,
WANG Conga85a9702016-07-25 16:09:41 -0700260 .size = sizeof(struct tcf_pedit),
WANG Congddf97cc2016-02-22 15:57:53 -0800261};
262
263static __net_init int pedit_init_net(struct net *net)
264{
265 struct tc_action_net *tn = net_generic(net, pedit_net_id);
266
267 return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
268}
269
270static void __net_exit pedit_exit_net(struct net *net)
271{
272 struct tc_action_net *tn = net_generic(net, pedit_net_id);
273
274 tc_action_net_exit(tn);
275}
276
277static struct pernet_operations pedit_net_ops = {
278 .init = pedit_init_net,
279 .exit = pedit_exit_net,
280 .id = &pedit_net_id,
281 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283
284MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
285MODULE_DESCRIPTION("Generic Packet Editor actions");
286MODULE_LICENSE("GPL");
287
David S. Millere9ce1cd2006-08-21 23:54:55 -0700288static int __init pedit_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
WANG Congddf97cc2016-02-22 15:57:53 -0800290 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
David S. Millere9ce1cd2006-08-21 23:54:55 -0700293static void __exit pedit_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
WANG Congddf97cc2016-02-22 15:57:53 -0800295 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298module_init(pedit_init_module);
299module_exit(pedit_cleanup_module);
300