blob: 9bebf4dc1c8e75dad65ef006794774f48c551bbf [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]);
Davide Caratti079e02e2019-11-19 23:47:33 +010057 if (!parm->nkeys)
58 return -EINVAL;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 ksize = parm->nkeys * sizeof(struct tc_pedit_key);
Patrick McHardy7ba699c2008-01-22 22:11:50 -080061 if (nla_len(tb[TCA_PEDIT_PARMS]) < sizeof(*parm) + ksize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return -EINVAL;
63
WANG Congddf97cc2016-02-22 15:57:53 -080064 if (!tcf_hash_check(tn, parm->index, a, bind)) {
WANG Congddf97cc2016-02-22 15:57:53 -080065 ret = tcf_hash_create(tn, parm->index, est, a,
WANG Conga85a9702016-07-25 16:09:41 -070066 &act_pedit_ops, bind, false);
WANG Cong86062032014-02-11 17:07:31 -080067 if (ret)
68 return ret;
WANG Conga85a9702016-07-25 16:09:41 -070069 p = to_pedit(*a);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 keys = kmalloc(ksize, GFP_KERNEL);
71 if (keys == NULL) {
WANG Conga85a9702016-07-25 16:09:41 -070072 tcf_hash_cleanup(*a, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -ENOMEM;
74 }
75 ret = ACT_P_CREATED;
76 } else {
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050077 if (bind)
78 return 0;
WANG Conga85a9702016-07-25 16:09:41 -070079 tcf_hash_release(*a, bind);
Jamal Hadi Salim1a293212013-12-23 08:02:11 -050080 if (!ovr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EEXIST;
WANG Conga85a9702016-07-25 16:09:41 -070082 p = to_pedit(*a);
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)
WANG Conga85a9702016-07-25 16:09:41 -0700101 tcf_hash_insert(tn, *a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return ret;
103}
104
WANG Conga5b5c952014-02-11 17:07:32 -0800105static void tcf_pedit_cleanup(struct tc_action *a, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
WANG Conga85a9702016-07-25 16:09:41 -0700107 struct tcf_pedit *p = to_pedit(a);
WANG Conga5b5c952014-02-11 17:07:32 -0800108 struct tc_pedit_key *keys = p->tcfp_keys;
109 kfree(keys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Amir Vadai95c20272016-11-28 12:56:40 +0200112static bool offset_valid(struct sk_buff *skb, int offset)
113{
114 if (offset > 0 && offset > skb->len)
115 return false;
116
117 if (offset < 0 && -offset > skb_headroom(skb))
118 return false;
119
120 return true;
121}
122
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000123static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700124 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
WANG Conga85a9702016-07-25 16:09:41 -0700126 struct tcf_pedit *p = to_pedit(a);
Florian Westphal4749c3ef2015-04-30 12:12:00 +0200127 int i;
Changli Gaodb2c2412010-06-02 04:55:02 +0000128 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000130 if (skb_unclone(skb, GFP_ATOMIC))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000131 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Changli Gaodb2c2412010-06-02 04:55:02 +0000133 off = skb_network_offset(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
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400137 tcf_lastuse_update(&p->tcf_tm);
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++) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000143 u32 *ptr, _data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int offset = tkey->off;
145
146 if (tkey->offmask) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000147 char *d, _d;
148
Amir Vadai95c20272016-11-28 12:56:40 +0200149 if (!offset_valid(skb, off + tkey->at)) {
150 pr_info("tc filter pedit 'at' offset %d out of bounds\n",
151 off + tkey->at);
152 goto bad;
153 }
Changli Gaodb2c2412010-06-02 04:55:02 +0000154 d = skb_header_pointer(skb, off + tkey->at, 1,
155 &_d);
156 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 goto bad;
Changli Gaodb2c2412010-06-02 04:55:02 +0000158 offset += (*d & tkey->offmask) >> tkey->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
161 if (offset % 4) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000162 pr_info("tc filter pedit"
163 " offset must be on 32 bit boundaries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto bad;
165 }
Amir Vadai95c20272016-11-28 12:56:40 +0200166
167 if (!offset_valid(skb, off + offset)) {
168 pr_info("tc filter pedit offset %d out of bounds\n",
169 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto bad;
171 }
172
Changli Gaodb2c2412010-06-02 04:55:02 +0000173 ptr = skb_header_pointer(skb, off + offset, 4, &_data);
174 if (!ptr)
175 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* just do it, baby */
177 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
Changli Gaodb2c2412010-06-02 04:55:02 +0000178 if (ptr == &_data)
179 skb_store_bits(skb, off + offset, ptr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 goto done;
stephen hemminger6ff9c362010-05-12 06:37:05 +0000183 } else
184 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186bad:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700187 p->tcf_qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188done:
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000189 bstats_update(&p->tcf_bstats, skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190 spin_unlock(&p->tcf_lock);
191 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
David S. Millere9ce1cd2006-08-21 23:54:55 -0700194static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
195 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700197 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700198 struct tcf_pedit *p = to_pedit(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct tc_pedit *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct tcf_t t;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900201 int s;
202
David S. Millere9ce1cd2006-08-21 23:54:55 -0700203 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* netlink spinlocks held above us - must use ATOMIC */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700206 opt = kzalloc(s, GFP_ATOMIC);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700207 if (unlikely(!opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
David S. Millere9ce1cd2006-08-21 23:54:55 -0700210 memcpy(opt->keys, p->tcfp_keys,
211 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
212 opt->index = p->tcf_index;
213 opt->nkeys = p->tcfp_nkeys;
214 opt->flags = p->tcfp_flags;
215 opt->action = p->tcf_action;
216 opt->refcnt = p->tcf_refcnt - ref;
217 opt->bindcnt = p->tcf_bindcnt - bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
David S. Miller1b34ec42012-03-29 05:11:39 -0400219 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
220 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400221
222 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200223 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400224 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400225
Patrick McHardy541673c82006-01-08 22:17:27 -0800226 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return skb->len;
228
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800229nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700230 nlmsg_trim(skb, b);
Patrick McHardy541673c82006-01-08 22:17:27 -0800231 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return -1;
233}
234
WANG Congddf97cc2016-02-22 15:57:53 -0800235static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
236 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700237 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800238{
239 struct tc_action_net *tn = net_generic(net, pedit_net_id);
240
WANG Conga85a9702016-07-25 16:09:41 -0700241 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800242}
243
WANG Conga85a9702016-07-25 16:09:41 -0700244static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800245{
246 struct tc_action_net *tn = net_generic(net, pedit_net_id);
247
248 return tcf_hash_search(tn, a, index);
249}
250
David S. Millere9ce1cd2006-08-21 23:54:55 -0700251static struct tc_action_ops act_pedit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .kind = "pedit",
253 .type = TCA_ACT_PEDIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .owner = THIS_MODULE,
255 .act = tcf_pedit,
256 .dump = tcf_pedit_dump,
257 .cleanup = tcf_pedit_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .init = tcf_pedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800259 .walk = tcf_pedit_walker,
260 .lookup = tcf_pedit_search,
WANG Conga85a9702016-07-25 16:09:41 -0700261 .size = sizeof(struct tcf_pedit),
WANG Congddf97cc2016-02-22 15:57:53 -0800262};
263
264static __net_init int pedit_init_net(struct net *net)
265{
266 struct tc_action_net *tn = net_generic(net, pedit_net_id);
267
268 return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
269}
270
271static void __net_exit pedit_exit_net(struct net *net)
272{
273 struct tc_action_net *tn = net_generic(net, pedit_net_id);
274
275 tc_action_net_exit(tn);
276}
277
278static struct pernet_operations pedit_net_ops = {
279 .init = pedit_init_net,
280 .exit = pedit_exit_net,
281 .id = &pedit_net_id,
282 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283};
284
285MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
286MODULE_DESCRIPTION("Generic Packet Editor actions");
287MODULE_LICENSE("GPL");
288
David S. Millere9ce1cd2006-08-21 23:54:55 -0700289static int __init pedit_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
WANG Congddf97cc2016-02-22 15:57:53 -0800291 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
David S. Millere9ce1cd2006-08-21 23:54:55 -0700294static void __exit pedit_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
WANG Congddf97cc2016-02-22 15:57:53 -0800296 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299module_init(pedit_init_module);
300module_exit(pedit_cleanup_module);
301