blob: c437c6d51a71763bf77e8350e41ccf6f5be000e3 [file] [log] [blame]
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -04001/*
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
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030023static unsigned int skbmod_net_id;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040024static 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);
Paolo Abeni7fd4b282018-07-30 14:30:43 +020044 if (unlikely(err)) /* best policy is to drop on the floor */
45 goto drop;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040046
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040047 action = READ_ONCE(d->tcf_action);
Paolo Abeni7fd4b282018-07-30 14:30:43 +020048 if (unlikely(action == TC_ACT_SHOT))
49 goto drop;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040050
Paolo Abeni7fd4b282018-07-30 14:30:43 +020051 p = rcu_dereference_bh(d->skbmod_p);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040052 flags = p->flags;
53 if (flags & SKBMOD_F_DMAC)
54 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst);
55 if (flags & SKBMOD_F_SMAC)
56 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src);
57 if (flags & SKBMOD_F_ETYPE)
58 eth_hdr(skb)->h_proto = p->eth_type;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040059
60 if (flags & SKBMOD_F_SWAPMAC) {
61 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */
62 /*XXX: I am sure we can come up with more efficient swapping*/
63 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest);
64 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source);
65 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr);
66 }
67
68 return action;
Paolo Abeni7fd4b282018-07-30 14:30:43 +020069
70drop:
71 qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats));
72 return TC_ACT_SHOT;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040073}
74
75static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = {
76 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) },
77 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN },
78 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN },
79 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 },
80};
81
82static int tcf_skbmod_init(struct net *net, struct nlattr *nla,
83 struct nlattr *est, struct tc_action **a,
Vlad Buslov789871b2018-07-05 17:24:25 +030084 int ovr, int bind, bool rtnl_held,
85 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -040086{
87 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
88 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
89 struct tcf_skbmod_params *p, *p_old;
90 struct tc_skbmod *parm;
91 struct tcf_skbmod *d;
92 bool exists = false;
93 u8 *daddr = NULL;
94 u8 *saddr = NULL;
95 u16 eth_type = 0;
96 u32 lflags = 0;
97 int ret = 0, err;
98
99 if (!nla)
100 return -EINVAL;
101
Johannes Bergfceb6432017-04-12 14:34:07 +0200102 err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, 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]);
125 if (parm->flags & SKBMOD_F_SWAPMAC)
126 lflags = SKBMOD_F_SWAPMAC;
127
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300128 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
129 if (err < 0)
130 return err;
131 exists = err;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400132 if (exists && bind)
133 return 0;
134
Roman Mashaka52956d2018-05-11 14:35:33 -0400135 if (!lflags) {
136 if (exists)
137 tcf_idr_release(*a, bind);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300138 else
139 tcf_idr_cleanup(tn, parm->index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400140 return -EINVAL;
Roman Mashaka52956d2018-05-11 14:35:33 -0400141 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400142
143 if (!exists) {
Chris Mi65a206c2017-08-30 02:31:59 -0400144 ret = tcf_idr_create(tn, parm->index, est, a,
145 &act_skbmod_ops, bind, true);
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300146 if (ret) {
147 tcf_idr_cleanup(tn, parm->index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400148 return ret;
Vlad Buslov0190c1d2018-07-05 17:24:32 +0300149 }
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400150
151 ret = ACT_P_CREATED;
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300152 } else if (!ovr) {
Chris Mi65a206c2017-08-30 02:31:59 -0400153 tcf_idr_release(*a, bind);
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300154 return -EEXIST;
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400155 }
156
157 d = to_skbmod(*a);
158
159 ASSERT_RTNL();
160 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
161 if (unlikely(!p)) {
Vlad Buslov4e8ddd72018-07-05 17:24:30 +0300162 tcf_idr_release(*a, bind);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400163 return -ENOMEM;
164 }
165
166 p->flags = lflags;
167 d->tcf_action = parm->action;
168
169 p_old = rtnl_dereference(d->skbmod_p);
170
171 if (ovr)
172 spin_lock_bh(&d->tcf_lock);
173
174 if (lflags & SKBMOD_F_DMAC)
175 ether_addr_copy(p->eth_dst, daddr);
176 if (lflags & SKBMOD_F_SMAC)
177 ether_addr_copy(p->eth_src, saddr);
178 if (lflags & SKBMOD_F_ETYPE)
179 p->eth_type = htons(eth_type);
180
181 rcu_assign_pointer(d->skbmod_p, p);
182 if (ovr)
183 spin_unlock_bh(&d->tcf_lock);
184
185 if (p_old)
186 kfree_rcu(p_old, rcu);
187
188 if (ret == ACT_P_CREATED)
Chris Mi65a206c2017-08-30 02:31:59 -0400189 tcf_idr_insert(tn, *a);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400190 return ret;
191}
192
Cong Wang9a63b252017-12-05 12:53:07 -0800193static void tcf_skbmod_cleanup(struct tc_action *a)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400194{
195 struct tcf_skbmod *d = to_skbmod(a);
196 struct tcf_skbmod_params *p;
197
198 p = rcu_dereference_protected(d->skbmod_p, 1);
Davide Caratti2d433612018-03-16 00:00:57 +0100199 if (p)
200 kfree_rcu(p, rcu);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400201}
202
203static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
204 int bind, int ref)
205{
206 struct tcf_skbmod *d = to_skbmod(a);
207 unsigned char *b = skb_tail_pointer(skb);
208 struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
209 struct tc_skbmod opt = {
210 .index = d->tcf_index,
Vlad Buslov036bb442018-07-05 17:24:24 +0300211 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
212 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400213 .action = d->tcf_action,
214 };
215 struct tcf_t t;
216
217 opt.flags = p->flags;
218 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
219 goto nla_put_failure;
220 if ((p->flags & SKBMOD_F_DMAC) &&
221 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
222 goto nla_put_failure;
223 if ((p->flags & SKBMOD_F_SMAC) &&
224 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
225 goto nla_put_failure;
226 if ((p->flags & SKBMOD_F_ETYPE) &&
227 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
228 goto nla_put_failure;
229
230 tcf_tm_dump(&t, &d->tcf_tm);
231 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
232 goto nla_put_failure;
233
234 return skb->len;
235nla_put_failure:
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400236 nlmsg_trim(skb, b);
237 return -1;
238}
239
240static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
241 struct netlink_callback *cb, int type,
Alexander Aring41780102018-02-15 10:54:58 -0500242 const struct tc_action_ops *ops,
243 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400244{
245 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
246
Alexander Aringb3620142018-02-15 10:54:59 -0500247 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400248}
249
Alexander Aring331a9292018-02-15 10:54:57 -0500250static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
251 struct netlink_ext_ack *extack)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400252{
253 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
254
Chris Mi65a206c2017-08-30 02:31:59 -0400255 return tcf_idr_search(tn, a, index);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400256}
257
Vlad Buslovb4090742018-07-05 17:24:28 +0300258static int tcf_skbmod_delete(struct net *net, u32 index)
259{
260 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
261
262 return tcf_idr_delete_index(tn, index);
263}
264
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400265static struct tc_action_ops act_skbmod_ops = {
266 .kind = "skbmod",
267 .type = TCA_ACT_SKBMOD,
268 .owner = THIS_MODULE,
269 .act = tcf_skbmod_run,
270 .dump = tcf_skbmod_dump,
271 .init = tcf_skbmod_init,
272 .cleanup = tcf_skbmod_cleanup,
273 .walk = tcf_skbmod_walker,
274 .lookup = tcf_skbmod_search,
Vlad Buslovb4090742018-07-05 17:24:28 +0300275 .delete = tcf_skbmod_delete,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400276 .size = sizeof(struct tcf_skbmod),
277};
278
279static __net_init int skbmod_init_net(struct net *net)
280{
281 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
282
Cong Wangc7e460c2017-11-06 13:47:18 -0800283 return tc_action_net_init(tn, &act_skbmod_ops);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400284}
285
Cong Wang039af9c2017-12-11 15:35:03 -0800286static void __net_exit skbmod_exit_net(struct list_head *net_list)
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400287{
Cong Wang039af9c2017-12-11 15:35:03 -0800288 tc_action_net_exit(net_list, skbmod_net_id);
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400289}
290
291static struct pernet_operations skbmod_net_ops = {
292 .init = skbmod_init_net,
Cong Wang039af9c2017-12-11 15:35:03 -0800293 .exit_batch = skbmod_exit_net,
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400294 .id = &skbmod_net_id,
295 .size = sizeof(struct tc_action_net),
Jamal Hadi Salim86da71b2016-09-12 20:13:09 -0400296};
297
298MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>");
299MODULE_DESCRIPTION("SKB data mod-ing");
300MODULE_LICENSE("GPL");
301
302static int __init skbmod_init_module(void)
303{
304 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
305}
306
307static void __exit skbmod_cleanup_module(void)
308{
309 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
310}
311
312module_init(skbmod_init_module);
313module_exit(skbmod_cleanup_module);