blob: 81a1c67335be62d04a468711c353efc90df9a26f [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -04002/*
3 * net/sched/act_skbmod.c skb data modifier
4 *
5 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -04006*/
7
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/kernel.h>
11#include <linux/skbuff.h>
12#include <linux/rtnetlink.h>
13#include <net/netlink.h>
14#include <net/pkt_sched.h>
Davide Caratti7c3d8252019-03-20 15:00:12 +010015#include <net/pkt_cls.h>
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040016
17#include <linux/tc_act/tc_skbmod.h>
18#include <net/tc_act/tc_skbmod.h>
19
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030020static unsigned int skbmod_net_id;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040021static struct tc_action_ops act_skbmod_ops;
22
23#define MAX_EDIT_LEN ETH_HLEN
Jamal Hadi Salim353d2c22018-08-12 09:34:59 -040024static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040025 struct tcf_result *res)
26{
27 struct tcf_skbmod *d = to_skbmod(a);
28 int action;
29 struct tcf_skbmod_params *p;
30 u64 flags;
31 int err;
32
33 tcf_lastuse_update(&d->tcf_tm);
34 bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb);
35
36 /* XXX: if you are going to edit more fields beyond ethernet header
37 * (example when you add IP header replacement or vlan swap)
38 * then MAX_EDIT_LEN needs to change appropriately
39 */
40 err = skb_ensure_writable(skb, MAX_EDIT_LEN);
Paolo Abeni7fd4b282018-07-30 14:30:43 +020041 if (unlikely(err)) /* best policy is to drop on the floor */
42 goto drop;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040043
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040044 action = READ_ONCE(d->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +020045 if (unlikely(action == TC_ACT_SHOT))
46 goto drop;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040047
Paolo Abeni7fd4b282018-07-30 14:30:43 +020048 p = rcu_dereference_bh(d->skbmod_p);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040049 flags = p->flags;
50 if (flags & SKBMOD_F_DMAC)
51 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
52 if (flags & SKBMOD_F_SMAC)
53 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
54 if (flags & SKBMOD_F_ETYPE)
55 eth_hdr(skb)->h_proto = p->eth_type;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040056
57 if (flags & SKBMOD_F_SWAPMAC) {
58 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
59 /*XXX: I am sure we can come up with more efficient swapping*/
60 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
61 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
62 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
63 }
64
65 return action;
Paolo Abeni7fd4b282018-07-30 14:30:43 +020066
67drop:
68 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
69 return TC_ACT_SHOT;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040070}
71
72static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
73 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
74 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
75 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
76 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
77};
78
79static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
80 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030081 int ovr, int bind, bool rtnl_held,
Vlad Buslovabbb0d32019-10-30 16:09:05 +020082 struct tcf_proto *tp, u32 flags,
Vlad Buslov789871b2018-07-05 17:24:25 +030083 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040084{
85 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
86 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
87 struct tcf_skbmod_params *p, *p_old;
Davide Caratti7c3d8252019-03-20 15:00:12 +010088 struct tcf_chain *goto_ch = NULL;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040089 struct tc_skbmod *parm;
Dmytro Linkin7be8ef22019-08-01 13:02:51 +000090 u32 lflags = 0, index;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040091 struct tcf_skbmod *d;
92 bool exists = false;
93 u8 *daddr = NULL;
94 u8 *saddr = NULL;
95 u16 eth_type = 0;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040096 int ret = 0, err;
97
98 if (!nla)
99 return -EINVAL;
100
Johannes Berg8cb08172019-04-26 14:07:28 +0200101 err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla,
102 skbmod_policy, NULL);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400103 if (err < 0)
104 return err;
105
106 if (!tb[TCA_SKBMOD_PARMS])
107 return -EINVAL;
108
109 if (tb[TCA_SKBMOD_DMAC]) {
110 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
111 lflags |= SKBMOD_F_DMAC;
112 }
113
114 if (tb[TCA_SKBMOD_SMAC]) {
115 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
116 lflags |= SKBMOD_F_SMAC;
117 }
118
119 if (tb[TCA_SKBMOD_ETYPE]) {
120 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
121 lflags |= SKBMOD_F_ETYPE;
122 }
123
124 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000125 index = parm->index;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400126 if (parm->flags & SKBMOD_F_SWAPMAC)
127 lflags = SKBMOD_F_SWAPMAC;
128
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000129 err = tcf_idr_check_alloc(tn, &index, a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300130 if (err < 0)
131 return err;
132 exists = err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400133 if (exists && bind)
134 return 0;
135
Roman Mashaka52956d2018-05-11 14:35:33 -0400136 if (!lflags) {
137 if (exists)
138 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300139 else
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000140 tcf_idr_cleanup(tn, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400141 return -EINVAL;
Roman Mashaka52956d2018-05-11 14:35:33 -0400142 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400143
144 if (!exists) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000145 ret = tcf_idr_create(tn, index, est, a,
Vlad Buslove3822672019-10-30 16:09:06 +0200146 &act_skbmod_ops, bind, true, 0);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300147 if (ret) {
Dmytro Linkin7be8ef22019-08-01 13:02:51 +0000148 tcf_idr_cleanup(tn, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400149 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300150 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400151
152 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300153 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400154 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300155 return -EEXIST;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400156 }
Davide Caratti7c3d8252019-03-20 15:00:12 +0100157 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
158 if (err < 0)
159 goto release_idr;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400160
161 d = to_skbmod(*a);
162
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400163 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
164 if (unlikely(!p)) {
Davide Caratti7c3d8252019-03-20 15:00:12 +0100165 err = -ENOMEM;
166 goto put_chain;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400167 }
168
169 p->flags = lflags;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400170
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400171 if (ovr)
172 spin_lock_bh(&d->tcf_lock);
Vlad Buslovc8814552018-08-10 20:51:49 +0300173 /* Protected by tcf_lock if overwriting existing action. */
Davide Caratti7c3d8252019-03-20 15:00:12 +0100174 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
Vlad Buslovc8814552018-08-10 20:51:49 +0300175 p_old = rcu_dereference_protected(d->skbmod_p, 1);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400176
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);
183
184 rcu_assign_pointer(d->skbmod_p, p);
185 if (ovr)
186 spin_unlock_bh(&d->tcf_lock);
187
188 if (p_old)
189 kfree_rcu(p_old, rcu);
Davide Caratti7c3d8252019-03-20 15:00:12 +0100190 if (goto_ch)
191 tcf_chain_put_by_act(goto_ch);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400192
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400193 return ret;
Davide Caratti7c3d8252019-03-20 15:00:12 +0100194put_chain:
195 if (goto_ch)
196 tcf_chain_put_by_act(goto_ch);
197release_idr:
198 tcf_idr_release(*a, bind);
199 return err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400200}
201
Cong Wang9a63b252017-12-05 12:53:07 -0800202static void tcf_skbmod_cleanup(struct tc_action *a)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400203{
204 struct tcf_skbmod *d = to_skbmod(a);
205 struct tcf_skbmod_params *p;
206
207 p = rcu_dereference_protected(d->skbmod_p, 1);
Davide Caratti2d433612018-03-16 00:00:57 +0100208 if (p)
209 kfree_rcu(p, rcu);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400210}
211
212static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
213 int bind, int ref)
214{
215 struct tcf_skbmod *d = to_skbmod(a);
216 unsigned char *b = skb_tail_pointer(skb);
Vlad Buslovc8814552018-08-10 20:51:49 +0300217 struct tcf_skbmod_params *p;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400218 struct tc_skbmod opt = {
219 .index = d->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300220 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
221 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400222 };
223 struct tcf_t t;
224
Vlad Buslovc8814552018-08-10 20:51:49 +0300225 spin_lock_bh(&d->tcf_lock);
226 opt.action = d->tcf_action;
227 p = rcu_dereference_protected(d->skbmod_p,
228 lockdep_is_held(&d->tcf_lock));
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400229 opt.flags = p->flags;
230 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
231 goto nla_put_failure;
232 if ((p->flags & SKBMOD_F_DMAC) &&
233 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
234 goto nla_put_failure;
235 if ((p->flags & SKBMOD_F_SMAC) &&
236 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
237 goto nla_put_failure;
238 if ((p->flags & SKBMOD_F_ETYPE) &&
239 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
240 goto nla_put_failure;
241
242 tcf_tm_dump(&t, &d->tcf_tm);
243 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
244 goto nla_put_failure;
245
Vlad Buslovc8814552018-08-10 20:51:49 +0300246 spin_unlock_bh(&d->tcf_lock);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400247 return skb->len;
248nla_put_failure:
Vlad Buslovc8814552018-08-10 20:51:49 +0300249 spin_unlock_bh(&d->tcf_lock);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400250 nlmsg_trim(skb, b);
251 return -1;
252}
253
254static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
255 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500256 const struct tc_action_ops *ops,
257 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400258{
259 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
260
Alexander Aringb3620142018-02-15 10:54:59 -0500261 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400262}
263
Cong Wangf061b482018-08-29 10:15:35 -0700264static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400265{
266 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
267
Chris Mi65a206c2017-08-30 02:31:59 -0400268 return tcf_idr_search(tn, a, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400269}
270
271static struct tc_action_ops act_skbmod_ops = {
272 .kind = "skbmod",
Eli Coheneddd2cf2019-02-10 14:25:00 +0200273 .id = TCA_ACT_SKBMOD,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400274 .owner = THIS_MODULE,
Jamal Hadi Salim353d2c22018-08-12 09:34:59 -0400275 .act = tcf_skbmod_act,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400276 .dump = tcf_skbmod_dump,
277 .init = tcf_skbmod_init,
278 .cleanup = tcf_skbmod_cleanup,
279 .walk = tcf_skbmod_walker,
280 .lookup = tcf_skbmod_search,
281 .size = sizeof(struct tcf_skbmod),
282};
283
284static __net_init int skbmod_init_net(struct net *net)
285{
286 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
287
Cong Wang981471b2019-08-25 10:01:32 -0700288 return tc_action_net_init(net, tn, &act_skbmod_ops);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400289}
290
Cong Wang039af9c2017-12-11 15:35:03 -0800291static void __net_exit skbmod_exit_net(struct list_head *net_list)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400292{
Cong Wang039af9c2017-12-11 15:35:03 -0800293 tc_action_net_exit(net_list, skbmod_net_id);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400294}
295
296static struct pernet_operations skbmod_net_ops = {
297 .init = skbmod_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800298 .exit_batch = skbmod_exit_net,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400299 .id = &skbmod_net_id,
300 .size = sizeof(struct tc_action_net),
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400301};
302
303MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
304MODULE_DESCRIPTION("SKB data mod-ing");
305MODULE_LICENSE("GPL");
306
307static int __init skbmod_init_module(void)
308{
309 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
310}
311
312static void __exit skbmod_cleanup_module(void)
313{
314 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
315}
316
317module_init(skbmod_init_module);
318module_exit(skbmod_cleanup_module);