Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * net/sched/act_skbmod.c skb data modifier
4 *
5 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
6*/
7
8#include <linux/module.h>
9#include <linux/if_arp.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/skbuff.h>
13#include <linux/rtnetlink.h>
14#include <net/inet_ecn.h>
15#include <net/netlink.h>
16#include <net/pkt_sched.h>
17#include <net/pkt_cls.h>
18#include <net/tc_wrapper.h>
19
20#include <linux/tc_act/tc_skbmod.h>
21#include <net/tc_act/tc_skbmod.h>
22
23static struct tc_action_ops act_skbmod_ops;
24
25TC_INDIRECT_SCOPE int tcf_skbmod_act(struct sk_buff *skb,
26 const struct tc_action *a,
27 struct tcf_result *res)
28{
29 struct tcf_skbmod *d = to_skbmod(a);
30 int action, max_edit_len, err;
31 struct tcf_skbmod_params *p;
32 u64 flags;
33
34 tcf_lastuse_update(&d->tcf_tm);
35 bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb);
36
37 action = READ_ONCE(d->tcf_action);
38 if (unlikely(action == TC_ACT_SHOT))
39 goto drop;
40
41 max_edit_len = skb_mac_header_len(skb);
42 p = rcu_dereference_bh(d->skbmod_p);
43 flags = p->flags;
44
45 /* tcf_skbmod_init() guarantees "flags" to be one of the following:
46 * 1. a combination of SKBMOD_F_{DMAC,SMAC,ETYPE}
47 * 2. SKBMOD_F_SWAPMAC
48 * 3. SKBMOD_F_ECN
49 * SKBMOD_F_ECN only works with IP packets; all other flags only work with Ethernet
50 * packets.
51 */
52 if (flags == SKBMOD_F_ECN) {
53 switch (skb_protocol(skb, true)) {
54 case cpu_to_be16(ETH_P_IP):
55 case cpu_to_be16(ETH_P_IPV6):
56 max_edit_len += skb_network_header_len(skb);
57 break;
58 default:
59 goto out;
60 }
61 } else if (!skb->dev || skb->dev->type != ARPHRD_ETHER) {
62 goto out;
63 }
64
65 err = skb_ensure_writable(skb, max_edit_len);
66 if (unlikely(err)) /* best policy is to drop on the floor */
67 goto drop;
68
69 if (flags & SKBMOD_F_DMAC)
70 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
71 if (flags & SKBMOD_F_SMAC)
72 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
73 if (flags & SKBMOD_F_ETYPE)
74 eth_hdr(skb)->h_proto = p->eth_type;
75
76 if (flags & SKBMOD_F_SWAPMAC) {
77 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
78 /*XXX: I am sure we can come up with more efficient swapping*/
79 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
80 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
81 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
82 }
83
84 if (flags & SKBMOD_F_ECN)
85 INET_ECN_set_ce(skb);
86
87out:
88 return action;
89
90drop:
91 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
92 return TC_ACT_SHOT;
93}
94
95static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
96 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
97 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
98 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
99 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
100};
101
102static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
103 struct nlattr *est, struct tc_action **a,
104 struct tcf_proto *tp, u32 flags,
105 struct netlink_ext_ack *extack)
106{
107 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
108 bool ovr = flags & TCA_ACT_FLAGS_REPLACE;
109 bool bind = flags & TCA_ACT_FLAGS_BIND;
110 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
111 struct tcf_skbmod_params *p, *p_old;
112 struct tcf_chain *goto_ch = NULL;
113 struct tc_skbmod *parm;
114 u32 lflags = 0, index;
115 struct tcf_skbmod *d;
116 bool exists = false;
117 u8 *daddr = NULL;
118 u8 *saddr = NULL;
119 u16 eth_type = 0;
120 int ret = 0, err;
121
122 if (!nla)
123 return -EINVAL;
124
125 err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
126 skbmod_policy, NULL);
127 if (err < 0)
128 return err;
129
130 if (!tb[TCA_SKBMOD_PARMS])
131 return -EINVAL;
132
133 if (tb[TCA_SKBMOD_DMAC]) {
134 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
135 lflags |= SKBMOD_F_DMAC;
136 }
137
138 if (tb[TCA_SKBMOD_SMAC]) {
139 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
140 lflags |= SKBMOD_F_SMAC;
141 }
142
143 if (tb[TCA_SKBMOD_ETYPE]) {
144 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
145 lflags |= SKBMOD_F_ETYPE;
146 }
147
148 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
149 index = parm->index;
150 if (parm->flags & SKBMOD_F_SWAPMAC)
151 lflags = SKBMOD_F_SWAPMAC;
152 if (parm->flags & SKBMOD_F_ECN)
153 lflags = SKBMOD_F_ECN;
154
155 err = tcf_idr_check_alloc(tn, &index, a, bind);
156 if (err < 0)
157 return err;
158 exists = err;
159 if (exists && bind)
160 return ACT_P_BOUND;
161
162 if (!lflags) {
163 if (exists)
164 tcf_idr_release(*a, bind);
165 else
166 tcf_idr_cleanup(tn, index);
167 return -EINVAL;
168 }
169
170 if (!exists) {
171 ret = tcf_idr_create(tn, index, est, a,
172 &act_skbmod_ops, bind, true, flags);
173 if (ret) {
174 tcf_idr_cleanup(tn, index);
175 return ret;
176 }
177
178 ret = ACT_P_CREATED;
179 } else if (!ovr) {
180 tcf_idr_release(*a, bind);
181 return -EEXIST;
182 }
183 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
184 if (err < 0)
185 goto release_idr;
186
187 d = to_skbmod(*a);
188
189 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
190 if (unlikely(!p)) {
191 err = -ENOMEM;
192 goto put_chain;
193 }
194
195 p->flags = lflags;
196
197 if (ovr)
198 spin_lock_bh(&d->tcf_lock);
199 /* Protected by tcf_lock if overwriting existing action. */
200 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
201 p_old = rcu_dereference_protected(d->skbmod_p, 1);
202
203 if (lflags & SKBMOD_F_DMAC)
204 ether_addr_copy(p->eth_dst, daddr);
205 if (lflags & SKBMOD_F_SMAC)
206 ether_addr_copy(p->eth_src, saddr);
207 if (lflags & SKBMOD_F_ETYPE)
208 p->eth_type = htons(eth_type);
209
210 rcu_assign_pointer(d->skbmod_p, p);
211 if (ovr)
212 spin_unlock_bh(&d->tcf_lock);
213
214 if (p_old)
215 kfree_rcu(p_old, rcu);
216 if (goto_ch)
217 tcf_chain_put_by_act(goto_ch);
218
219 return ret;
220put_chain:
221 if (goto_ch)
222 tcf_chain_put_by_act(goto_ch);
223release_idr:
224 tcf_idr_release(*a, bind);
225 return err;
226}
227
228static void tcf_skbmod_cleanup(struct tc_action *a)
229{
230 struct tcf_skbmod *d = to_skbmod(a);
231 struct tcf_skbmod_params *p;
232
233 p = rcu_dereference_protected(d->skbmod_p, 1);
234 if (p)
235 kfree_rcu(p, rcu);
236}
237
238static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
239 int bind, int ref)
240{
241 struct tcf_skbmod *d = to_skbmod(a);
242 unsigned char *b = skb_tail_pointer(skb);
243 struct tcf_skbmod_params *p;
244 struct tc_skbmod opt;
245 struct tcf_t t;
246
247 memset(&opt, 0, sizeof(opt));
248 opt.index = d->tcf_index;
249 opt.refcnt = refcount_read(&d->tcf_refcnt) - ref;
250 opt.bindcnt = atomic_read(&d->tcf_bindcnt) - bind;
251 spin_lock_bh(&d->tcf_lock);
252 opt.action = d->tcf_action;
253 p = rcu_dereference_protected(d->skbmod_p,
254 lockdep_is_held(&d->tcf_lock));
255 opt.flags = p->flags;
256 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
257 goto nla_put_failure;
258 if ((p->flags & SKBMOD_F_DMAC) &&
259 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
260 goto nla_put_failure;
261 if ((p->flags & SKBMOD_F_SMAC) &&
262 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
263 goto nla_put_failure;
264 if ((p->flags & SKBMOD_F_ETYPE) &&
265 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
266 goto nla_put_failure;
267
268 tcf_tm_dump(&t, &d->tcf_tm);
269 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
270 goto nla_put_failure;
271
272 spin_unlock_bh(&d->tcf_lock);
273 return skb->len;
274nla_put_failure:
275 spin_unlock_bh(&d->tcf_lock);
276 nlmsg_trim(skb, b);
277 return -1;
278}
279
280static struct tc_action_ops act_skbmod_ops = {
281 .kind = "skbmod",
282 .id = TCA_ACT_SKBMOD,
283 .owner = THIS_MODULE,
284 .act = tcf_skbmod_act,
285 .dump = tcf_skbmod_dump,
286 .init = tcf_skbmod_init,
287 .cleanup = tcf_skbmod_cleanup,
288 .size = sizeof(struct tcf_skbmod),
289};
290MODULE_ALIAS_NET_ACT("skbmod");
291
292static __net_init int skbmod_init_net(struct net *net)
293{
294 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id);
295
296 return tc_action_net_init(net, tn, &act_skbmod_ops);
297}
298
299static void __net_exit skbmod_exit_net(struct list_head *net_list)
300{
301 tc_action_net_exit(net_list, act_skbmod_ops.net_id);
302}
303
304static struct pernet_operations skbmod_net_ops = {
305 .init = skbmod_init_net,
306 .exit_batch = skbmod_exit_net,
307 .id = &act_skbmod_ops.net_id,
308 .size = sizeof(struct tc_action_net),
309};
310
311MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
312MODULE_DESCRIPTION("SKB data mod-ing");
313MODULE_LICENSE("GPL");
314
315static int __init skbmod_init_module(void)
316{
317 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
318}
319
320static void __exit skbmod_cleanup_module(void)
321{
322 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
323}
324
325module_init(skbmod_init_module);
326module_exit(skbmod_cleanup_module);
1/*
2 * net/sched/act_skbmod.c skb data modifier
3 *
4 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
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
23static unsigned int skbmod_net_id;
24static struct tc_action_ops act_skbmod_ops;
25
26#define MAX_EDIT_LEN ETH_HLEN
27static int tcf_skbmod_run(struct sk_buff *skb, const struct tc_action *a,
28 struct tcf_result *res)
29{
30 struct tcf_skbmod *d = to_skbmod(a);
31 int action;
32 struct tcf_skbmod_params *p;
33 u64 flags;
34 int err;
35
36 tcf_lastuse_update(&d->tcf_tm);
37 bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
38
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
42 */
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));
46 return TC_ACT_SHOT;
47 }
48
49 rcu_read_lock();
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));
53 rcu_read_unlock();
54 return action;
55 }
56
57 p = rcu_dereference(d->skbmod_p);
58 flags = p->flags;
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;
65 rcu_read_unlock();
66
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);
73 }
74
75 return action;
76}
77
78static 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 },
83};
84
85static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
86 struct nlattr *est, struct tc_action **a,
87 int ovr, int bind, struct netlink_ext_ack *extack)
88{
89 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
90 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
91 struct tcf_skbmod_params *p, *p_old;
92 struct tc_skbmod *parm;
93 struct tcf_skbmod *d;
94 bool exists = false;
95 u8 *daddr = NULL;
96 u8 *saddr = NULL;
97 u16 eth_type = 0;
98 u32 lflags = 0;
99 int ret = 0, err;
100
101 if (!nla)
102 return -EINVAL;
103
104 err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
105 if (err < 0)
106 return err;
107
108 if (!tb[TCA_SKBMOD_PARMS])
109 return -EINVAL;
110
111 if (tb[TCA_SKBMOD_DMAC]) {
112 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
113 lflags |= SKBMOD_F_DMAC;
114 }
115
116 if (tb[TCA_SKBMOD_SMAC]) {
117 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
118 lflags |= SKBMOD_F_SMAC;
119 }
120
121 if (tb[TCA_SKBMOD_ETYPE]) {
122 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
123 lflags |= SKBMOD_F_ETYPE;
124 }
125
126 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
127 if (parm->flags & SKBMOD_F_SWAPMAC)
128 lflags = SKBMOD_F_SWAPMAC;
129
130 exists = tcf_idr_check(tn, parm->index, a, bind);
131 if (exists && bind)
132 return 0;
133
134 if (!lflags) {
135 if (exists)
136 tcf_idr_release(*a, bind);
137 return -EINVAL;
138 }
139
140 if (!exists) {
141 ret = tcf_idr_create(tn, parm->index, est, a,
142 &act_skbmod_ops, bind, true);
143 if (ret)
144 return ret;
145
146 ret = ACT_P_CREATED;
147 } else {
148 tcf_idr_release(*a, bind);
149 if (!ovr)
150 return -EEXIST;
151 }
152
153 d = to_skbmod(*a);
154
155 ASSERT_RTNL();
156 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
157 if (unlikely(!p)) {
158 if (ret == ACT_P_CREATED)
159 tcf_idr_release(*a, bind);
160 return -ENOMEM;
161 }
162
163 p->flags = lflags;
164 d->tcf_action = parm->action;
165
166 p_old = rtnl_dereference(d->skbmod_p);
167
168 if (ovr)
169 spin_lock_bh(&d->tcf_lock);
170
171 if (lflags & SKBMOD_F_DMAC)
172 ether_addr_copy(p->eth_dst, daddr);
173 if (lflags & SKBMOD_F_SMAC)
174 ether_addr_copy(p->eth_src, saddr);
175 if (lflags & SKBMOD_F_ETYPE)
176 p->eth_type = htons(eth_type);
177
178 rcu_assign_pointer(d->skbmod_p, p);
179 if (ovr)
180 spin_unlock_bh(&d->tcf_lock);
181
182 if (p_old)
183 kfree_rcu(p_old, rcu);
184
185 if (ret == ACT_P_CREATED)
186 tcf_idr_insert(tn, *a);
187 return ret;
188}
189
190static void tcf_skbmod_cleanup(struct tc_action *a)
191{
192 struct tcf_skbmod *d = to_skbmod(a);
193 struct tcf_skbmod_params *p;
194
195 p = rcu_dereference_protected(d->skbmod_p, 1);
196 if (p)
197 kfree_rcu(p, rcu);
198}
199
200static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
201 int bind, int ref)
202{
203 struct tcf_skbmod *d = to_skbmod(a);
204 unsigned char *b = skb_tail_pointer(skb);
205 struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
206 struct tc_skbmod opt = {
207 .index = d->tcf_index,
208 .refcnt = d->tcf_refcnt - ref,
209 .bindcnt = d->tcf_bindcnt - bind,
210 .action = d->tcf_action,
211 };
212 struct tcf_t t;
213
214 opt.flags = p->flags;
215 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
216 goto nla_put_failure;
217 if ((p->flags & SKBMOD_F_DMAC) &&
218 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
219 goto nla_put_failure;
220 if ((p->flags & SKBMOD_F_SMAC) &&
221 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
222 goto nla_put_failure;
223 if ((p->flags & SKBMOD_F_ETYPE) &&
224 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
225 goto nla_put_failure;
226
227 tcf_tm_dump(&t, &d->tcf_tm);
228 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
229 goto nla_put_failure;
230
231 return skb->len;
232nla_put_failure:
233 nlmsg_trim(skb, b);
234 return -1;
235}
236
237static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
238 struct netlink_callback *cb, int type,
239 const struct tc_action_ops *ops,
240 struct netlink_ext_ack *extack)
241{
242 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
243
244 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
245}
246
247static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
248 struct netlink_ext_ack *extack)
249{
250 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
251
252 return tcf_idr_search(tn, a, index);
253}
254
255static struct tc_action_ops act_skbmod_ops = {
256 .kind = "skbmod",
257 .type = TCA_ACT_SKBMOD,
258 .owner = THIS_MODULE,
259 .act = tcf_skbmod_run,
260 .dump = tcf_skbmod_dump,
261 .init = tcf_skbmod_init,
262 .cleanup = tcf_skbmod_cleanup,
263 .walk = tcf_skbmod_walker,
264 .lookup = tcf_skbmod_search,
265 .size = sizeof(struct tcf_skbmod),
266};
267
268static __net_init int skbmod_init_net(struct net *net)
269{
270 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
271
272 return tc_action_net_init(tn, &act_skbmod_ops);
273}
274
275static void __net_exit skbmod_exit_net(struct list_head *net_list)
276{
277 tc_action_net_exit(net_list, skbmod_net_id);
278}
279
280static struct pernet_operations skbmod_net_ops = {
281 .init = skbmod_init_net,
282 .exit_batch = skbmod_exit_net,
283 .id = &skbmod_net_id,
284 .size = sizeof(struct tc_action_net),
285};
286
287MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
288MODULE_DESCRIPTION("SKB data mod-ing");
289MODULE_LICENSE("GPL");
290
291static int __init skbmod_init_module(void)
292{
293 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
294}
295
296static void __exit skbmod_cleanup_module(void)
297{
298 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
299}
300
301module_init(skbmod_init_module);
302module_exit(skbmod_cleanup_module);