Skip to content

Commit 86da71b

Browse files
jhsmtdavem330
authored andcommitted
net_sched: Introduce skbmod action
This action is intended to be an upgrade from a usability perspective from pedit (as well as operational debugability). Compare this: sudo tc filter add dev $ETH parent 1: protocol ip prio 10 \ u32 match ip protocol 1 0xff flowid 1:2 \ action pedit munge offset -14 u8 set 0x02 \ munge offset -13 u8 set 0x15 \ munge offset -12 u8 set 0x15 \ munge offset -11 u8 set 0x15 \ munge offset -10 u16 set 0x1515 \ pipe to: sudo tc filter add dev $ETH parent 1: protocol ip prio 10 \ u32 match ip protocol 1 0xff flowid 1:2 \ action skbmod dmac 02:15:15:15:15:15 Also try to do a MAC address swap with pedit or worse try to debug a policy with destination mac, source mac and etherype. Then make few rules out of those and you'll get my point. In the future common use cases on pedit can be migrated to this action (as an example different fields in ip v4/6, transports like tcp/udp/sctp etc). For this first cut, this allows modifying basic ethernet header. The most important ethernet use case at the moment is when redirecting or mirroring packets to a remote machine. The dst mac address needs a re-write so that it doesnt get dropped or confuse an interconnecting (learning) switch or dropped by a target machine (which looks at the dst mac). And at times when flipping back the packet a swap of the MAC addresses is needed. Signed-off-by: Jamal Hadi Salim <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c865250 commit 86da71b

File tree

5 files changed

+382
-0
lines changed

5 files changed

+382
-0
lines changed

include/net/tc_act/tc_skbmod.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2016, Jamal Hadi Salim
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*/
9+
10+
#ifndef __NET_TC_SKBMOD_H
11+
#define __NET_TC_SKBMOD_H
12+
13+
#include <net/act_api.h>
14+
#include <linux/tc_act/tc_skbmod.h>
15+
16+
struct tcf_skbmod_params {
17+
struct rcu_head rcu;
18+
u64 flags; /*up to 64 types of operations; extend if needed */
19+
u8 eth_dst[ETH_ALEN];
20+
u16 eth_type;
21+
u8 eth_src[ETH_ALEN];
22+
};
23+
24+
struct tcf_skbmod {
25+
struct tc_action common;
26+
struct tcf_skbmod_params __rcu *skbmod_p;
27+
};
28+
#define to_skbmod(a) ((struct tcf_skbmod *)a)
29+
30+
#endif /* __NET_TC_SKBMOD_H */

include/uapi/linux/tc_act/tc_skbmod.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2016, Jamal Hadi Salim
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*/
9+
10+
#ifndef __LINUX_TC_SKBMOD_H
11+
#define __LINUX_TC_SKBMOD_H
12+
13+
#include <linux/pkt_cls.h>
14+
15+
#define TCA_ACT_SKBMOD 15
16+
17+
#define SKBMOD_F_DMAC 0x1
18+
#define SKBMOD_F_SMAC 0x2
19+
#define SKBMOD_F_ETYPE 0x4
20+
#define SKBMOD_F_SWAPMAC 0x8
21+
22+
struct tc_skbmod {
23+
tc_gen;
24+
__u64 flags;
25+
};
26+
27+
enum {
28+
TCA_SKBMOD_UNSPEC,
29+
TCA_SKBMOD_TM,
30+
TCA_SKBMOD_PARMS,
31+
TCA_SKBMOD_DMAC,
32+
TCA_SKBMOD_SMAC,
33+
TCA_SKBMOD_ETYPE,
34+
TCA_SKBMOD_PAD,
35+
__TCA_SKBMOD_MAX
36+
};
37+
#define TCA_SKBMOD_MAX (__TCA_SKBMOD_MAX - 1)
38+
39+
#endif

net/sched/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,17 @@ config NET_ACT_CONNMARK
749749
To compile this code as a module, choose M here: the
750750
module will be called act_connmark.
751751

752+
config NET_ACT_SKBMOD
753+
tristate "skb data modification action"
754+
depends on NET_CLS_ACT
755+
---help---
756+
Say Y here to allow modification of skb data
757+
758+
If unsure, say N.
759+
760+
To compile this code as a module, choose M here: the
761+
module will be called act_skbmod.
762+
752763
config NET_ACT_IFE
753764
tristate "Inter-FE action based on IETF ForCES InterFE LFB"
754765
depends on NET_CLS_ACT

net/sched/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ obj-$(CONFIG_NET_ACT_CSUM) += act_csum.o
1919
obj-$(CONFIG_NET_ACT_VLAN) += act_vlan.o
2020
obj-$(CONFIG_NET_ACT_BPF) += act_bpf.o
2121
obj-$(CONFIG_NET_ACT_CONNMARK) += act_connmark.o
22+
obj-$(CONFIG_NET_ACT_SKBMOD) += act_skbmod.o
2223
obj-$(CONFIG_NET_ACT_IFE) += act_ife.o
2324
obj-$(CONFIG_NET_IFE_SKBMARK) += act_meta_mark.o
2425
obj-$(CONFIG_NET_IFE_SKBPRIO) += act_meta_skbprio.o

net/sched/act_skbmod.c

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
/*
2+
* net/sched/act_skbmod.c skb data modifier
3+
*
4+
* Copyright (c) 2016 Jamal Hadi Salim <[email protected]>
5+
*
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.
10+
*/
11+
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>
19+
20+
#include <linux/tc_act/tc_skbmod.h>
21+
#include <net/tc_act/tc_skbmod.h>
22+
23+
#define SKBMOD_TAB_MASK 15
24+
25+
static int skbmod_net_id;
26+
static struct tc_action_ops act_skbmod_ops;
27+
28+
#define MAX_EDIT_LEN ETH_HLEN
29+
static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a,
30+
struct tcf_result *res)
31+
{
32+
struct tcf_skbmod *d = to_skbmod(a);
33+
int action;
34+
struct tcf_skbmod_params *p;
35+
u64 flags;
36+
int err;
37+
38+
tcf_lastuse_update(&d->tcf_tm);
39+
bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
40+
41+
/* XXX: if you are going to edit more fields beyond ethernet header
42+
* (example when you add IP header replacement or vlan swap)
43+
* then MAX_EDIT_LEN needs to change appropriately
44+
*/
45+
err = skb_ensure_writable(skb, MAX_EDIT_LEN);
46+
if (unlikely(err)) { /* best policy is to drop on the floor */
47+
qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
48+
return TC_ACT_SHOT;
49+
}
50+
51+
rcu_read_lock();
52+
action = READ_ONCE(d->tcf_action);
53+
if (unlikely(action == TC_ACT_SHOT)) {
54+
qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
55+
rcu_read_unlock();
56+
return action;
57+
}
58+
59+
p = rcu_dereference(d->skbmod_p);
60+
flags = p->flags;
61+
if (flags & SKBMOD_F_DMAC)
62+
ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
63+
if (flags & SKBMOD_F_SMAC)
64+
ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
65+
if (flags & SKBMOD_F_ETYPE)
66+
eth_hdr(skb)->h_proto = p->eth_type;
67+
rcu_read_unlock();
68+
69+
if (flags & SKBMOD_F_SWAPMAC) {
70+
u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
71+
/*XXX: I am sure we can come up with more efficient swapping*/
72+
ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
73+
ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
74+
ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
75+
}
76+
77+
return action;
78+
}
79+
80+
static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
81+
[TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
82+
[TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
83+
[TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
84+
[TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
85+
};
86+
87+
static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
88+
struct nlattr *est, struct tc_action **a,
89+
int ovr, int bind)
90+
{
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+
struct tcf_skbmod *d;
96+
bool exists = false;
97+
u8 *daddr = NULL;
98+
u8 *saddr = NULL;
99+
u16 eth_type = 0;
100+
u32 lflags = 0;
101+
int ret = 0, err;
102+
103+
if (!nla)
104+
return -EINVAL;
105+
106+
err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy);
107+
if (err < 0)
108+
return err;
109+
110+
if (!tb[TCA_SKBMOD_PARMS])
111+
return -EINVAL;
112+
113+
if (tb[TCA_SKBMOD_DMAC]) {
114+
daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
115+
lflags |= SKBMOD_F_DMAC;
116+
}
117+
118+
if (tb[TCA_SKBMOD_SMAC]) {
119+
saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
120+
lflags |= SKBMOD_F_SMAC;
121+
}
122+
123+
if (tb[TCA_SKBMOD_ETYPE]) {
124+
eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
125+
lflags |= SKBMOD_F_ETYPE;
126+
}
127+
128+
parm = nla_data(tb[TCA_SKBMOD_PARMS]);
129+
if (parm->flags & SKBMOD_F_SWAPMAC)
130+
lflags = SKBMOD_F_SWAPMAC;
131+
132+
exists = tcf_hash_check(tn, parm->index, a, bind);
133+
if (exists && bind)
134+
return 0;
135+
136+
if (!lflags)
137+
return -EINVAL;
138+
139+
if (!exists) {
140+
ret = tcf_hash_create(tn, parm->index, est, a,
141+
&act_skbmod_ops, bind, true);
142+
if (ret)
143+
return ret;
144+
145+
ret = ACT_P_CREATED;
146+
} else {
147+
tcf_hash_release(*a, bind);
148+
if (!ovr)
149+
return -EEXIST;
150+
}
151+
152+
d = to_skbmod(*a);
153+
154+
ASSERT_RTNL();
155+
p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
156+
if (unlikely(!p)) {
157+
if (ovr)
158+
tcf_hash_release(*a, bind);
159+
return -ENOMEM;
160+
}
161+
162+
p->flags = lflags;
163+
d->tcf_action = parm->action;
164+
165+
p_old = rtnl_dereference(d->skbmod_p);
166+
167+
if (ovr)
168+
spin_lock_bh(&d->tcf_lock);
169+
170+
if (lflags & SKBMOD_F_DMAC)
171+
ether_addr_copy(p->eth_dst, daddr);
172+
if (lflags & SKBMOD_F_SMAC)
173+
ether_addr_copy(p->eth_src, saddr);
174+
if (lflags & SKBMOD_F_ETYPE)
175+
p->eth_type = htons(eth_type);
176+
177+
rcu_assign_pointer(d->skbmod_p, p);
178+
if (ovr)
179+
spin_unlock_bh(&d->tcf_lock);
180+
181+
if (p_old)
182+
kfree_rcu(p_old, rcu);
183+
184+
if (ret == ACT_P_CREATED)
185+
tcf_hash_insert(tn, *a);
186+
return ret;
187+
}
188+
189+
static void tcf_skbmod_cleanup(struct tc_action *a, int bind)
190+
{
191+
struct tcf_skbmod *d = to_skbmod(a);
192+
struct tcf_skbmod_params *p;
193+
194+
p = rcu_dereference_protected(d->skbmod_p, 1);
195+
kfree_rcu(p, rcu);
196+
}
197+
198+
static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
199+
int bind, int ref)
200+
{
201+
struct tcf_skbmod *d = to_skbmod(a);
202+
unsigned char *b = skb_tail_pointer(skb);
203+
struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
204+
struct tc_skbmod opt = {
205+
.index = d->tcf_index,
206+
.refcnt = d->tcf_refcnt - ref,
207+
.bindcnt = d->tcf_bindcnt - bind,
208+
.action = d->tcf_action,
209+
};
210+
struct tcf_t t;
211+
212+
opt.flags = p->flags;
213+
if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
214+
goto nla_put_failure;
215+
if ((p->flags & SKBMOD_F_DMAC) &&
216+
nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
217+
goto nla_put_failure;
218+
if ((p->flags & SKBMOD_F_SMAC) &&
219+
nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
220+
goto nla_put_failure;
221+
if ((p->flags & SKBMOD_F_ETYPE) &&
222+
nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
223+
goto nla_put_failure;
224+
225+
tcf_tm_dump(&t, &d->tcf_tm);
226+
if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
227+
goto nla_put_failure;
228+
229+
return skb->len;
230+
nla_put_failure:
231+
rcu_read_unlock();
232+
nlmsg_trim(skb, b);
233+
return -1;
234+
}
235+
236+
static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
237+
struct netlink_callback *cb, int type,
238+
const struct tc_action_ops *ops)
239+
{
240+
struct tc_action_net *tn = net_generic(net, skbmod_net_id);
241+
242+
return tcf_generic_walker(tn, skb, cb, type, ops);
243+
}
244+
245+
static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index)
246+
{
247+
struct tc_action_net *tn = net_generic(net, skbmod_net_id);
248+
249+
return tcf_hash_search(tn, a, index);
250+
}
251+
252+
static struct tc_action_ops act_skbmod_ops = {
253+
.kind = "skbmod",
254+
.type = TCA_ACT_SKBMOD,
255+
.owner = THIS_MODULE,
256+
.act = tcf_skbmod_run,
257+
.dump = tcf_skbmod_dump,
258+
.init = tcf_skbmod_init,
259+
.cleanup = tcf_skbmod_cleanup,
260+
.walk = tcf_skbmod_walker,
261+
.lookup = tcf_skbmod_search,
262+
.size = sizeof(struct tcf_skbmod),
263+
};
264+
265+
static __net_init int skbmod_init_net(struct net *net)
266+
{
267+
struct tc_action_net *tn = net_generic(net, skbmod_net_id);
268+
269+
return tc_action_net_init(tn, &act_skbmod_ops, SKBMOD_TAB_MASK);
270+
}
271+
272+
static void __net_exit skbmod_exit_net(struct net *net)
273+
{
274+
struct tc_action_net *tn = net_generic(net, skbmod_net_id);
275+
276+
tc_action_net_exit(tn);
277+
}
278+
279+
static struct pernet_operations skbmod_net_ops = {
280+
.init = skbmod_init_net,
281+
.exit = skbmod_exit_net,
282+
.id = &skbmod_net_id,
283+
.size = sizeof(struct tc_action_net),
284+
};
285+
286+
MODULE_AUTHOR("Jamal Hadi Salim, <[email protected]>");
287+
MODULE_DESCRIPTION("SKB data mod-ing");
288+
MODULE_LICENSE("GPL");
289+
290+
static int __init skbmod_init_module(void)
291+
{
292+
return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
293+
}
294+
295+
static void __exit skbmod_cleanup_module(void)
296+
{
297+
tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
298+
}
299+
300+
module_init(skbmod_init_module);
301+
module_exit(skbmod_cleanup_module);

0 commit comments

Comments
 (0)