2 * Copyright (c) 2008, Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
16 * Author: Alexander Duyck <alexander.h.duyck@intel.com>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/skbuff.h>
23 #include <linux/rtnetlink.h>
24 #include <net/netlink.h>
25 #include <net/pkt_sched.h>
28 #include <net/dsfield.h>
30 #include <linux/tc_act/tc_skbedit.h>
31 #include <net/tc_act/tc_skbedit.h>
33 static unsigned int skbedit_net_id;
34 static struct tc_action_ops act_skbedit_ops;
36 static int tcf_skbedit(struct sk_buff *skb, const struct tc_action *a,
37 struct tcf_result *res)
39 struct tcf_skbedit *d = to_skbedit(a);
41 spin_lock(&d->tcf_lock);
42 tcf_lastuse_update(&d->tcf_tm);
43 bstats_update(&d->tcf_bstats, skb);
45 if (d->flags & SKBEDIT_F_PRIORITY)
46 skb->priority = d->priority;
47 if (d->flags & SKBEDIT_F_INHERITDSFIELD) {
48 int wlen = skb_network_offset(skb);
50 switch (tc_skb_protocol(skb)) {
52 wlen += sizeof(struct iphdr);
53 if (!pskb_may_pull(skb, wlen))
55 skb->priority = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
58 case htons(ETH_P_IPV6):
59 wlen += sizeof(struct ipv6hdr);
60 if (!pskb_may_pull(skb, wlen))
62 skb->priority = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
66 if (d->flags & SKBEDIT_F_QUEUE_MAPPING &&
67 skb->dev->real_num_tx_queues > d->queue_mapping)
68 skb_set_queue_mapping(skb, d->queue_mapping);
69 if (d->flags & SKBEDIT_F_MARK) {
70 skb->mark &= ~d->mask;
71 skb->mark |= d->mark & d->mask;
73 if (d->flags & SKBEDIT_F_PTYPE)
74 skb->pkt_type = d->ptype;
76 spin_unlock(&d->tcf_lock);
80 d->tcf_qstats.drops++;
81 spin_unlock(&d->tcf_lock);
85 static const struct nla_policy skbedit_policy[TCA_SKBEDIT_MAX + 1] = {
86 [TCA_SKBEDIT_PARMS] = { .len = sizeof(struct tc_skbedit) },
87 [TCA_SKBEDIT_PRIORITY] = { .len = sizeof(u32) },
88 [TCA_SKBEDIT_QUEUE_MAPPING] = { .len = sizeof(u16) },
89 [TCA_SKBEDIT_MARK] = { .len = sizeof(u32) },
90 [TCA_SKBEDIT_PTYPE] = { .len = sizeof(u16) },
91 [TCA_SKBEDIT_MASK] = { .len = sizeof(u32) },
92 [TCA_SKBEDIT_FLAGS] = { .len = sizeof(u64) },
95 static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
96 struct nlattr *est, struct tc_action **a,
97 int ovr, int bind, bool rtnl_held,
98 struct netlink_ext_ack *extack)
100 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
101 struct nlattr *tb[TCA_SKBEDIT_MAX + 1];
102 struct tc_skbedit *parm;
103 struct tcf_skbedit *d;
104 u32 flags = 0, *priority = NULL, *mark = NULL, *mask = NULL;
105 u16 *queue_mapping = NULL, *ptype = NULL;
112 err = nla_parse_nested(tb, TCA_SKBEDIT_MAX, nla, skbedit_policy, NULL);
116 if (tb[TCA_SKBEDIT_PARMS] == NULL)
119 if (tb[TCA_SKBEDIT_PRIORITY] != NULL) {
120 flags |= SKBEDIT_F_PRIORITY;
121 priority = nla_data(tb[TCA_SKBEDIT_PRIORITY]);
124 if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) {
125 flags |= SKBEDIT_F_QUEUE_MAPPING;
126 queue_mapping = nla_data(tb[TCA_SKBEDIT_QUEUE_MAPPING]);
129 if (tb[TCA_SKBEDIT_PTYPE] != NULL) {
130 ptype = nla_data(tb[TCA_SKBEDIT_PTYPE]);
131 if (!skb_pkt_type_ok(*ptype))
133 flags |= SKBEDIT_F_PTYPE;
136 if (tb[TCA_SKBEDIT_MARK] != NULL) {
137 flags |= SKBEDIT_F_MARK;
138 mark = nla_data(tb[TCA_SKBEDIT_MARK]);
141 if (tb[TCA_SKBEDIT_MASK] != NULL) {
142 flags |= SKBEDIT_F_MASK;
143 mask = nla_data(tb[TCA_SKBEDIT_MASK]);
146 if (tb[TCA_SKBEDIT_FLAGS] != NULL) {
147 u64 *pure_flags = nla_data(tb[TCA_SKBEDIT_FLAGS]);
149 if (*pure_flags & SKBEDIT_F_INHERITDSFIELD)
150 flags |= SKBEDIT_F_INHERITDSFIELD;
153 parm = nla_data(tb[TCA_SKBEDIT_PARMS]);
155 exists = tcf_idr_check(tn, parm->index, a, bind);
161 tcf_idr_release(*a, bind);
166 ret = tcf_idr_create(tn, parm->index, est, a,
167 &act_skbedit_ops, bind, false);
176 tcf_idr_release(*a, bind);
181 spin_lock_bh(&d->tcf_lock);
184 if (flags & SKBEDIT_F_PRIORITY)
185 d->priority = *priority;
186 if (flags & SKBEDIT_F_QUEUE_MAPPING)
187 d->queue_mapping = *queue_mapping;
188 if (flags & SKBEDIT_F_MARK)
190 if (flags & SKBEDIT_F_PTYPE)
192 /* default behaviour is to use all the bits */
193 d->mask = 0xffffffff;
194 if (flags & SKBEDIT_F_MASK)
197 d->tcf_action = parm->action;
199 spin_unlock_bh(&d->tcf_lock);
201 if (ret == ACT_P_CREATED)
202 tcf_idr_insert(tn, *a);
206 static int tcf_skbedit_dump(struct sk_buff *skb, struct tc_action *a,
209 unsigned char *b = skb_tail_pointer(skb);
210 struct tcf_skbedit *d = to_skbedit(a);
211 struct tc_skbedit opt = {
212 .index = d->tcf_index,
213 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
214 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
215 .action = d->tcf_action,
220 if (nla_put(skb, TCA_SKBEDIT_PARMS, sizeof(opt), &opt))
221 goto nla_put_failure;
222 if ((d->flags & SKBEDIT_F_PRIORITY) &&
223 nla_put_u32(skb, TCA_SKBEDIT_PRIORITY, d->priority))
224 goto nla_put_failure;
225 if ((d->flags & SKBEDIT_F_QUEUE_MAPPING) &&
226 nla_put_u16(skb, TCA_SKBEDIT_QUEUE_MAPPING, d->queue_mapping))
227 goto nla_put_failure;
228 if ((d->flags & SKBEDIT_F_MARK) &&
229 nla_put_u32(skb, TCA_SKBEDIT_MARK, d->mark))
230 goto nla_put_failure;
231 if ((d->flags & SKBEDIT_F_PTYPE) &&
232 nla_put_u16(skb, TCA_SKBEDIT_PTYPE, d->ptype))
233 goto nla_put_failure;
234 if ((d->flags & SKBEDIT_F_MASK) &&
235 nla_put_u32(skb, TCA_SKBEDIT_MASK, d->mask))
236 goto nla_put_failure;
237 if (d->flags & SKBEDIT_F_INHERITDSFIELD)
238 pure_flags |= SKBEDIT_F_INHERITDSFIELD;
239 if (pure_flags != 0 &&
240 nla_put(skb, TCA_SKBEDIT_FLAGS, sizeof(pure_flags), &pure_flags))
241 goto nla_put_failure;
243 tcf_tm_dump(&t, &d->tcf_tm);
244 if (nla_put_64bit(skb, TCA_SKBEDIT_TM, sizeof(t), &t, TCA_SKBEDIT_PAD))
245 goto nla_put_failure;
253 static int tcf_skbedit_walker(struct net *net, struct sk_buff *skb,
254 struct netlink_callback *cb, int type,
255 const struct tc_action_ops *ops,
256 struct netlink_ext_ack *extack)
258 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
260 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
263 static int tcf_skbedit_search(struct net *net, struct tc_action **a, u32 index,
264 struct netlink_ext_ack *extack)
266 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
268 return tcf_idr_search(tn, a, index);
271 static int tcf_skbedit_delete(struct net *net, u32 index)
273 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
275 return tcf_idr_delete_index(tn, index);
278 static struct tc_action_ops act_skbedit_ops = {
280 .type = TCA_ACT_SKBEDIT,
281 .owner = THIS_MODULE,
283 .dump = tcf_skbedit_dump,
284 .init = tcf_skbedit_init,
285 .walk = tcf_skbedit_walker,
286 .lookup = tcf_skbedit_search,
287 .delete = tcf_skbedit_delete,
288 .size = sizeof(struct tcf_skbedit),
291 static __net_init int skbedit_init_net(struct net *net)
293 struct tc_action_net *tn = net_generic(net, skbedit_net_id);
295 return tc_action_net_init(tn, &act_skbedit_ops);
298 static void __net_exit skbedit_exit_net(struct list_head *net_list)
300 tc_action_net_exit(net_list, skbedit_net_id);
303 static struct pernet_operations skbedit_net_ops = {
304 .init = skbedit_init_net,
305 .exit_batch = skbedit_exit_net,
306 .id = &skbedit_net_id,
307 .size = sizeof(struct tc_action_net),
310 MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
311 MODULE_DESCRIPTION("SKB Editing");
312 MODULE_LICENSE("GPL");
314 static int __init skbedit_init_module(void)
316 return tcf_register_action(&act_skbedit_ops, &skbedit_net_ops);
319 static void __exit skbedit_cleanup_module(void)
321 tcf_unregister_action(&act_skbedit_ops, &skbedit_net_ops);
324 module_init(skbedit_init_module);
325 module_exit(skbedit_cleanup_module);