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/if_arp.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/skbuff.h>
17 #include <linux/rtnetlink.h>
18 #include <net/netlink.h>
19 #include <net/pkt_sched.h>
21 #include <linux/tc_act/tc_skbmod.h>
22 #include <net/tc_act/tc_skbmod.h>
24 static unsigned int skbmod_net_id;
25 static struct tc_action_ops act_skbmod_ops;
27 #define MAX_EDIT_LEN ETH_HLEN
28 static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a,
29 struct tcf_result *res)
31 struct tcf_skbmod *d = to_skbmod(a);
33 struct tcf_skbmod_params *p;
37 tcf_lastuse_update(&d->tcf_tm);
38 bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
40 action = READ_ONCE(d->tcf_action);
41 if (unlikely(action == TC_ACT_SHOT))
44 if (!skb->dev || skb->dev->type != ARPHRD_ETHER)
47 /* XXX: if you are going to edit more fields beyond ethernet header
48 * (example when you add IP header replacement or vlan swap)
49 * then MAX_EDIT_LEN needs to change appropriately
51 err = skb_ensure_writable(skb, MAX_EDIT_LEN);
52 if (unlikely(err)) /* best policy is to drop on the floor */
55 p = rcu_dereference_bh(d->skbmod_p);
57 if (flags & SKBMOD_F_DMAC)
58 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
59 if (flags & SKBMOD_F_SMAC)
60 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
61 if (flags & SKBMOD_F_ETYPE)
62 eth_hdr(skb)->h_proto = p->eth_type;
64 if (flags & SKBMOD_F_SWAPMAC) {
65 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
66 /*XXX: I am sure we can come up with more efficient swapping*/
67 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
68 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
69 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
75 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
79 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
80 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
81 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
82 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
83 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
86 static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
87 struct nlattr *est, struct tc_action **a,
88 int ovr, int bind, bool rtnl_held,
89 struct netlink_ext_ack *extack)
91 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
92 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
93 struct tcf_skbmod_params *p, *p_old;
94 struct tc_skbmod *parm;
95 u32 lflags = 0, index;
106 err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
110 if (!tb[TCA_SKBMOD_PARMS])
113 if (tb[TCA_SKBMOD_DMAC]) {
114 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
115 lflags |= SKBMOD_F_DMAC;
118 if (tb[TCA_SKBMOD_SMAC]) {
119 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
120 lflags |= SKBMOD_F_SMAC;
123 if (tb[TCA_SKBMOD_ETYPE]) {
124 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
125 lflags |= SKBMOD_F_ETYPE;
128 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
130 if (parm->flags & SKBMOD_F_SWAPMAC)
131 lflags = SKBMOD_F_SWAPMAC;
133 err = tcf_idr_check_alloc(tn, &index, a, bind);
142 tcf_idr_release(*a, bind);
144 tcf_idr_cleanup(tn, index);
149 ret = tcf_idr_create(tn, index, est, a,
150 &act_skbmod_ops, bind, true);
152 tcf_idr_cleanup(tn, index);
158 tcf_idr_release(*a, bind);
164 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
166 tcf_idr_release(*a, bind);
171 d->tcf_action = parm->action;
174 spin_lock_bh(&d->tcf_lock);
175 /* Protected by tcf_lock if overwriting existing action. */
176 p_old = rcu_dereference_protected(d->skbmod_p, 1);
178 if (lflags & SKBMOD_F_DMAC)
179 ether_addr_copy(p->eth_dst, daddr);
180 if (lflags & SKBMOD_F_SMAC)
181 ether_addr_copy(p->eth_src, saddr);
182 if (lflags & SKBMOD_F_ETYPE)
183 p->eth_type = htons(eth_type);
185 rcu_assign_pointer(d->skbmod_p, p);
187 spin_unlock_bh(&d->tcf_lock);
190 kfree_rcu(p_old, rcu);
192 if (ret == ACT_P_CREATED)
193 tcf_idr_insert(tn, *a);
197 static void tcf_skbmod_cleanup(struct tc_action *a)
199 struct tcf_skbmod *d = to_skbmod(a);
200 struct tcf_skbmod_params *p;
202 p = rcu_dereference_protected(d->skbmod_p, 1);
207 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
210 struct tcf_skbmod *d = to_skbmod(a);
211 unsigned char *b = skb_tail_pointer(skb);
212 struct tcf_skbmod_params *p;
213 struct tc_skbmod opt = {
214 .index = d->tcf_index,
215 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
216 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
220 spin_lock_bh(&d->tcf_lock);
221 opt.action = d->tcf_action;
222 p = rcu_dereference_protected(d->skbmod_p,
223 lockdep_is_held(&d->tcf_lock));
224 opt.flags = p->flags;
225 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
226 goto nla_put_failure;
227 if ((p->flags & SKBMOD_F_DMAC) &&
228 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
229 goto nla_put_failure;
230 if ((p->flags & SKBMOD_F_SMAC) &&
231 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
232 goto nla_put_failure;
233 if ((p->flags & SKBMOD_F_ETYPE) &&
234 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
235 goto nla_put_failure;
237 tcf_tm_dump(&t, &d->tcf_tm);
238 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
239 goto nla_put_failure;
241 spin_unlock_bh(&d->tcf_lock);
244 spin_unlock_bh(&d->tcf_lock);
249 static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
250 struct netlink_callback *cb, int type,
251 const struct tc_action_ops *ops,
252 struct netlink_ext_ack *extack)
254 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
256 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
259 static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
260 struct netlink_ext_ack *extack)
262 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
264 return tcf_idr_search(tn, a, index);
267 static struct tc_action_ops act_skbmod_ops = {
269 .type = TCA_ACT_SKBMOD,
270 .owner = THIS_MODULE,
271 .act = tcf_skbmod_act,
272 .dump = tcf_skbmod_dump,
273 .init = tcf_skbmod_init,
274 .cleanup = tcf_skbmod_cleanup,
275 .walk = tcf_skbmod_walker,
276 .lookup = tcf_skbmod_search,
277 .size = sizeof(struct tcf_skbmod),
280 static __net_init int skbmod_init_net(struct net *net)
282 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
284 return tc_action_net_init(net, tn, &act_skbmod_ops);
287 static void __net_exit skbmod_exit_net(struct list_head *net_list)
289 tc_action_net_exit(net_list, skbmod_net_id);
292 static struct pernet_operations skbmod_net_ops = {
293 .init = skbmod_init_net,
294 .exit_batch = skbmod_exit_net,
295 .id = &skbmod_net_id,
296 .size = sizeof(struct tc_action_net),
299 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
300 MODULE_DESCRIPTION("SKB data mod-ing");
301 MODULE_LICENSE("GPL");
303 static int __init skbmod_init_module(void)
305 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
308 static void __exit skbmod_cleanup_module(void)
310 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
313 module_init(skbmod_init_module);
314 module_exit(skbmod_cleanup_module);