blob: b54d56d4959b6381d35799ba84f64ea634384cce [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
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000111static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700112 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
WANG Conga85a9702016-07-25 16:09:41 -0700114 struct tcf_pedit *p = to_pedit(a);
Florian Westphal4749c3ef2015-04-30 12:12:00 +0200115 int i;
Changli Gaodb2c2412010-06-02 04:55:02 +0000116 unsigned int off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Pravin B Shelar14bbd6a2013-02-14 09:44:49 +0000118 if (skb_unclone(skb, GFP_ATOMIC))
Eric Dumazetcc7ec452011-01-19 19:26:56 +0000119 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Changli Gaodb2c2412010-06-02 04:55:02 +0000121 off = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
David S. Millere9ce1cd2006-08-21 23:54:55 -0700123 spin_lock(&p->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jamal Hadi Salim9c4a4e42016-06-06 06:32:53 -0400125 tcf_lastuse_update(&p->tcf_tm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
David S. Millere9ce1cd2006-08-21 23:54:55 -0700127 if (p->tcfp_nkeys > 0) {
128 struct tc_pedit_key *tkey = p->tcfp_keys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
David S. Millere9ce1cd2006-08-21 23:54:55 -0700130 for (i = p->tcfp_nkeys; i > 0; i--, tkey++) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000131 u32 *ptr, _data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int offset = tkey->off;
133
134 if (tkey->offmask) {
Changli Gaodb2c2412010-06-02 04:55:02 +0000135 char *d, _d;
136
137 d = skb_header_pointer(skb, off + tkey->at, 1,
138 &_d);
139 if (!d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto bad;
Changli Gaodb2c2412010-06-02 04:55:02 +0000141 offset += (*d & tkey->offmask) >> tkey->shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144 if (offset % 4) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000145 pr_info("tc filter pedit"
146 " offset must be on 32 bit boundaries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto bad;
148 }
Bill Nottingham75202e72007-05-31 21:33:35 -0700149 if (offset > 0 && offset > skb->len) {
stephen hemminger6ff9c362010-05-12 06:37:05 +0000150 pr_info("tc filter pedit"
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300151 " offset %d can't exceed pkt length %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 offset, skb->len);
153 goto bad;
154 }
155
Changli Gaodb2c2412010-06-02 04:55:02 +0000156 ptr = skb_header_pointer(skb, off + offset, 4, &_data);
157 if (!ptr)
158 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* just do it, baby */
160 *ptr = ((*ptr & tkey->mask) ^ tkey->val);
Changli Gaodb2c2412010-06-02 04:55:02 +0000161 if (ptr == &_data)
162 skb_store_bits(skb, off + offset, ptr, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto done;
stephen hemminger6ff9c362010-05-12 06:37:05 +0000166 } else
167 WARN(1, "pedit BUG: index %d\n", p->tcf_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169bad:
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 p->tcf_qstats.overlimits++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171done:
Eric Dumazetbfe0d022011-01-09 08:30:54 +0000172 bstats_update(&p->tcf_bstats, skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173 spin_unlock(&p->tcf_lock);
174 return p->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
David S. Millere9ce1cd2006-08-21 23:54:55 -0700177static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
178 int bind, int ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700180 unsigned char *b = skb_tail_pointer(skb);
WANG Conga85a9702016-07-25 16:09:41 -0700181 struct tcf_pedit *p = to_pedit(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 struct tc_pedit *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct tcf_t t;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900184 int s;
185
David S. Millere9ce1cd2006-08-21 23:54:55 -0700186 s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* netlink spinlocks held above us - must use ATOMIC */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700189 opt = kzalloc(s, GFP_ATOMIC);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190 if (unlikely(!opt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
David S. Millere9ce1cd2006-08-21 23:54:55 -0700193 memcpy(opt->keys, p->tcfp_keys,
194 p->tcfp_nkeys * sizeof(struct tc_pedit_key));
195 opt->index = p->tcf_index;
196 opt->nkeys = p->tcfp_nkeys;
197 opt->flags = p->tcfp_flags;
198 opt->action = p->tcf_action;
199 opt->refcnt = p->tcf_refcnt - ref;
200 opt->bindcnt = p->tcf_bindcnt - bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
David S. Miller1b34ec42012-03-29 05:11:39 -0400202 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
203 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400204
205 tcf_tm_dump(&t, &p->tcf_tm);
Nicolas Dichtel98545182016-04-26 10:06:18 +0200206 if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
David S. Miller1b34ec42012-03-29 05:11:39 -0400207 goto nla_put_failure;
Jamal Hadi Salim48d8ee12016-06-06 06:32:55 -0400208
Patrick McHardy541673c82006-01-08 22:17:27 -0800209 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return skb->len;
211
Patrick McHardy7ba699c2008-01-22 22:11:50 -0800212nla_put_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700213 nlmsg_trim(skb, b);
Patrick McHardy541673c82006-01-08 22:17:27 -0800214 kfree(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -1;
216}
217
WANG Congddf97cc2016-02-22 15:57:53 -0800218static int tcf_pedit_walker(struct net *net, struct sk_buff *skb,
219 struct netlink_callback *cb, int type,
WANG Conga85a9702016-07-25 16:09:41 -0700220 const struct tc_action_ops *ops)
WANG Congddf97cc2016-02-22 15:57:53 -0800221{
222 struct tc_action_net *tn = net_generic(net, pedit_net_id);
223
WANG Conga85a9702016-07-25 16:09:41 -0700224 return tcf_generic_walker(tn, skb, cb, type, ops);
WANG Congddf97cc2016-02-22 15:57:53 -0800225}
226
WANG Conga85a9702016-07-25 16:09:41 -0700227static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index)
WANG Congddf97cc2016-02-22 15:57:53 -0800228{
229 struct tc_action_net *tn = net_generic(net, pedit_net_id);
230
231 return tcf_hash_search(tn, a, index);
232}
233
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234static struct tc_action_ops act_pedit_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .kind = "pedit",
236 .type = TCA_ACT_PEDIT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .owner = THIS_MODULE,
238 .act = tcf_pedit,
239 .dump = tcf_pedit_dump,
240 .cleanup = tcf_pedit_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 .init = tcf_pedit_init,
WANG Congddf97cc2016-02-22 15:57:53 -0800242 .walk = tcf_pedit_walker,
243 .lookup = tcf_pedit_search,
WANG Conga85a9702016-07-25 16:09:41 -0700244 .size = sizeof(struct tcf_pedit),
WANG Congddf97cc2016-02-22 15:57:53 -0800245};
246
247static __net_init int pedit_init_net(struct net *net)
248{
249 struct tc_action_net *tn = net_generic(net, pedit_net_id);
250
251 return tc_action_net_init(tn, &act_pedit_ops, PEDIT_TAB_MASK);
252}
253
254static void __net_exit pedit_exit_net(struct net *net)
255{
256 struct tc_action_net *tn = net_generic(net, pedit_net_id);
257
258 tc_action_net_exit(tn);
259}
260
261static struct pernet_operations pedit_net_ops = {
262 .init = pedit_init_net,
263 .exit = pedit_exit_net,
264 .id = &pedit_net_id,
265 .size = sizeof(struct tc_action_net),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
268MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
269MODULE_DESCRIPTION("Generic Packet Editor actions");
270MODULE_LICENSE("GPL");
271
David S. Millere9ce1cd2006-08-21 23:54:55 -0700272static int __init pedit_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
WANG Congddf97cc2016-02-22 15:57:53 -0800274 return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
David S. Millere9ce1cd2006-08-21 23:54:55 -0700277static void __exit pedit_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
WANG Congddf97cc2016-02-22 15:57:53 -0800279 tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282module_init(pedit_init_module);
283module_exit(pedit_cleanup_module);
284