2 * net/sched/act_skbmod.c skb data modifier
4 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/skbuff.h>
16 #include <linux/rtnetlink.h>
17 #include <net/netlink.h>
18 #include <net/pkt_sched.h>
20 #include <linux/tc_act/tc_skbmod.h>
21 #include <net/tc_act/tc_skbmod.h>
23 static unsigned int skbmod_net_id;
24 static struct tc_action_ops act_skbmod_ops;
26 #define MAX_EDIT_LEN ETH_HLEN
27 static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a,
28 struct tcf_result *res)
30 struct tcf_skbmod *d = to_skbmod(a);
32 struct tcf_skbmod_params *p;
36 tcf_lastuse_update(&d->tcf_tm);
37 bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
39 /* XXX: if you are going to edit more fields beyond ethernet header
40 * (example when you add IP header replacement or vlan swap)
41 * then MAX_EDIT_LEN needs to change appropriately
43 err = skb_ensure_writable(skb, MAX_EDIT_LEN);
44 if (unlikely(err)) { /* best policy is to drop on the floor */
45 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
50 action = READ_ONCE(d->tcf_action);
51 if (unlikely(action == TC_ACT_SHOT)) {
52 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
57 p = rcu_dereference(d->skbmod_p);
59 if (flags & SKBMOD_F_DMAC)
60 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
61 if (flags & SKBMOD_F_SMAC)
62 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
63 if (flags & SKBMOD_F_ETYPE)
64 eth_hdr(skb)->h_proto = p->eth_type;
67 if (flags & SKBMOD_F_SWAPMAC) {
68 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
69 /*XXX: I am sure we can come up with more efficient swapping*/
70 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
71 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
72 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
78 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
79 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
80 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
81 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
82 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
85 static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
86 struct nlattr *est, struct tc_action **a,
87 int ovr, int bind, bool rtnl_held,
88 struct netlink_ext_ack *extack)
90 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
91 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
92 struct tcf_skbmod_params *p, *p_old;
93 struct tc_skbmod *parm;
105 err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
109 if (!tb[TCA_SKBMOD_PARMS])
112 if (tb[TCA_SKBMOD_DMAC]) {
113 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
114 lflags |= SKBMOD_F_DMAC;
117 if (tb[TCA_SKBMOD_SMAC]) {
118 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
119 lflags |= SKBMOD_F_SMAC;
122 if (tb[TCA_SKBMOD_ETYPE]) {
123 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
124 lflags |= SKBMOD_F_ETYPE;
127 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
128 if (parm->flags & SKBMOD_F_SWAPMAC)
129 lflags = SKBMOD_F_SWAPMAC;
131 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
140 tcf_idr_release(*a, bind);
142 tcf_idr_cleanup(tn, parm->index);
147 ret = tcf_idr_create(tn, parm->index, est, a,
148 &act_skbmod_ops, bind, true);
150 tcf_idr_cleanup(tn, parm->index);
156 tcf_idr_release(*a, bind);
163 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
165 tcf_idr_release(*a, bind);
170 d->tcf_action = parm->action;
172 p_old = rtnl_dereference(d->skbmod_p);
175 spin_lock_bh(&d->tcf_lock);
177 if (lflags & SKBMOD_F_DMAC)
178 ether_addr_copy(p->eth_dst, daddr);
179 if (lflags & SKBMOD_F_SMAC)
180 ether_addr_copy(p->eth_src, saddr);
181 if (lflags & SKBMOD_F_ETYPE)
182 p->eth_type = htons(eth_type);
184 rcu_assign_pointer(d->skbmod_p, p);
186 spin_unlock_bh(&d->tcf_lock);
189 kfree_rcu(p_old, rcu);
191 if (ret == ACT_P_CREATED)
192 tcf_idr_insert(tn, *a);
196 static void tcf_skbmod_cleanup(struct tc_action *a)
198 struct tcf_skbmod *d = to_skbmod(a);
199 struct tcf_skbmod_params *p;
201 p = rcu_dereference_protected(d->skbmod_p, 1);
206 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
209 struct tcf_skbmod *d = to_skbmod(a);
210 unsigned char *b = skb_tail_pointer(skb);
211 struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
212 struct tc_skbmod opt = {
213 .index = d->tcf_index,
214 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
215 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
216 .action = d->tcf_action,
220 opt.flags = p->flags;
221 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
222 goto nla_put_failure;
223 if ((p->flags & SKBMOD_F_DMAC) &&
224 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
225 goto nla_put_failure;
226 if ((p->flags & SKBMOD_F_SMAC) &&
227 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
228 goto nla_put_failure;
229 if ((p->flags & SKBMOD_F_ETYPE) &&
230 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
231 goto nla_put_failure;
233 tcf_tm_dump(&t, &d->tcf_tm);
234 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
235 goto nla_put_failure;
243 static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
244 struct netlink_callback *cb, int type,
245 const struct tc_action_ops *ops,
246 struct netlink_ext_ack *extack)
248 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
250 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
253 static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
254 struct netlink_ext_ack *extack)
256 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
258 return tcf_idr_search(tn, a, index);
261 static int tcf_skbmod_delete(struct net *net, u32 index)
263 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
265 return tcf_idr_delete_index(tn, index);
268 static struct tc_action_ops act_skbmod_ops = {
270 .type = TCA_ACT_SKBMOD,
271 .owner = THIS_MODULE,
272 .act = tcf_skbmod_run,
273 .dump = tcf_skbmod_dump,
274 .init = tcf_skbmod_init,
275 .cleanup = tcf_skbmod_cleanup,
276 .walk = tcf_skbmod_walker,
277 .lookup = tcf_skbmod_search,
278 .delete = tcf_skbmod_delete,
279 .size = sizeof(struct tcf_skbmod),
282 static __net_init int skbmod_init_net(struct net *net)
284 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
286 return tc_action_net_init(tn, &act_skbmod_ops);
289 static void __net_exit skbmod_exit_net(struct list_head *net_list)
291 tc_action_net_exit(net_list, skbmod_net_id);
294 static struct pernet_operations skbmod_net_ops = {
295 .init = skbmod_init_net,
296 .exit_batch = skbmod_exit_net,
297 .id = &skbmod_net_id,
298 .size = sizeof(struct tc_action_net),
301 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
302 MODULE_DESCRIPTION("SKB data mod-ing");
303 MODULE_LICENSE("GPL");
305 static int __init skbmod_init_module(void)
307 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
310 static void __exit skbmod_cleanup_module(void)
312 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
315 module_init(skbmod_init_module);
316 module_exit(skbmod_cleanup_module);