]> Git Repo - linux.git/blame - net/sched/act_skbmod.c
net: sched: implement unlocked action init API
[linux.git] / net / sched / act_skbmod.c
CommitLineData
86da71b5
JHS
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
c7d03a00 23static unsigned int skbmod_net_id;
86da71b5
JHS
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,
789871bb
VB
87 int ovr, int bind, bool rtnl_held,
88 struct netlink_ext_ack *extack)
86da71b5
JHS
89{
90 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
91 struct nlattr *tb[TCA_SKBMOD_MAX + 1];
92 struct tcf_skbmod_params *p, *p_old;
93 struct tc_skbmod *parm;
94 struct tcf_skbmod *d;
95 bool exists = false;
96 u8 *daddr = NULL;
97 u8 *saddr = NULL;
98 u16 eth_type = 0;
99 u32 lflags = 0;
100 int ret = 0, err;
101
102 if (!nla)
103 return -EINVAL;
104
fceb6435 105 err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL);
86da71b5
JHS
106 if (err < 0)
107 return err;
108
109 if (!tb[TCA_SKBMOD_PARMS])
110 return -EINVAL;
111
112 if (tb[TCA_SKBMOD_DMAC]) {
113 daddr = nla_data(tb[TCA_SKBMOD_DMAC]);
114 lflags |= SKBMOD_F_DMAC;
115 }
116
117 if (tb[TCA_SKBMOD_SMAC]) {
118 saddr = nla_data(tb[TCA_SKBMOD_SMAC]);
119 lflags |= SKBMOD_F_SMAC;
120 }
121
122 if (tb[TCA_SKBMOD_ETYPE]) {
123 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]);
124 lflags |= SKBMOD_F_ETYPE;
125 }
126
127 parm = nla_data(tb[TCA_SKBMOD_PARMS]);
128 if (parm->flags & SKBMOD_F_SWAPMAC)
129 lflags = SKBMOD_F_SWAPMAC;
130
65a206c0 131 exists = tcf_idr_check(tn, parm->index, a, bind);
86da71b5
JHS
132 if (exists && bind)
133 return 0;
134
a52956df
RM
135 if (!lflags) {
136 if (exists)
137 tcf_idr_release(*a, bind);
86da71b5 138 return -EINVAL;
a52956df 139 }
86da71b5
JHS
140
141 if (!exists) {
65a206c0
CM
142 ret = tcf_idr_create(tn, parm->index, est, a,
143 &act_skbmod_ops, bind, true);
86da71b5
JHS
144 if (ret)
145 return ret;
146
147 ret = ACT_P_CREATED;
148 } else {
65a206c0 149 tcf_idr_release(*a, bind);
86da71b5
JHS
150 if (!ovr)
151 return -EEXIST;
152 }
153
154 d = to_skbmod(*a);
155
156 ASSERT_RTNL();
157 p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL);
158 if (unlikely(!p)) {
f29cdfbe 159 if (ret == ACT_P_CREATED)
65a206c0 160 tcf_idr_release(*a, bind);
86da71b5
JHS
161 return -ENOMEM;
162 }
163
164 p->flags = lflags;
165 d->tcf_action = parm->action;
166
167 p_old = rtnl_dereference(d->skbmod_p);
168
169 if (ovr)
170 spin_lock_bh(&d->tcf_lock);
171
172 if (lflags & SKBMOD_F_DMAC)
173 ether_addr_copy(p->eth_dst, daddr);
174 if (lflags & SKBMOD_F_SMAC)
175 ether_addr_copy(p->eth_src, saddr);
176 if (lflags & SKBMOD_F_ETYPE)
177 p->eth_type = htons(eth_type);
178
179 rcu_assign_pointer(d->skbmod_p, p);
180 if (ovr)
181 spin_unlock_bh(&d->tcf_lock);
182
183 if (p_old)
184 kfree_rcu(p_old, rcu);
185
186 if (ret == ACT_P_CREATED)
65a206c0 187 tcf_idr_insert(tn, *a);
86da71b5
JHS
188 return ret;
189}
190
9a63b255 191static void tcf_skbmod_cleanup(struct tc_action *a)
86da71b5
JHS
192{
193 struct tcf_skbmod *d = to_skbmod(a);
194 struct tcf_skbmod_params *p;
195
196 p = rcu_dereference_protected(d->skbmod_p, 1);
2d433610
DC
197 if (p)
198 kfree_rcu(p, rcu);
86da71b5
JHS
199}
200
201static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a,
202 int bind, int ref)
203{
204 struct tcf_skbmod *d = to_skbmod(a);
205 unsigned char *b = skb_tail_pointer(skb);
206 struct tcf_skbmod_params *p = rtnl_dereference(d->skbmod_p);
207 struct tc_skbmod opt = {
208 .index = d->tcf_index,
036bb443
VB
209 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
210 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
86da71b5
JHS
211 .action = d->tcf_action,
212 };
213 struct tcf_t t;
214
215 opt.flags = p->flags;
216 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt))
217 goto nla_put_failure;
218 if ((p->flags & SKBMOD_F_DMAC) &&
219 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst))
220 goto nla_put_failure;
221 if ((p->flags & SKBMOD_F_SMAC) &&
222 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src))
223 goto nla_put_failure;
224 if ((p->flags & SKBMOD_F_ETYPE) &&
225 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type)))
226 goto nla_put_failure;
227
228 tcf_tm_dump(&t, &d->tcf_tm);
229 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD))
230 goto nla_put_failure;
231
232 return skb->len;
233nla_put_failure:
86da71b5
JHS
234 nlmsg_trim(skb, b);
235 return -1;
236}
237
238static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb,
239 struct netlink_callback *cb, int type,
41780105
AA
240 const struct tc_action_ops *ops,
241 struct netlink_ext_ack *extack)
86da71b5
JHS
242{
243 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
244
b3620145 245 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
86da71b5
JHS
246}
247
331a9295
AA
248static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index,
249 struct netlink_ext_ack *extack)
86da71b5
JHS
250{
251 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
252
65a206c0 253 return tcf_idr_search(tn, a, index);
86da71b5
JHS
254}
255
256static struct tc_action_ops act_skbmod_ops = {
257 .kind = "skbmod",
258 .type = TCA_ACT_SKBMOD,
259 .owner = THIS_MODULE,
260 .act = tcf_skbmod_run,
261 .dump = tcf_skbmod_dump,
262 .init = tcf_skbmod_init,
263 .cleanup = tcf_skbmod_cleanup,
264 .walk = tcf_skbmod_walker,
265 .lookup = tcf_skbmod_search,
266 .size = sizeof(struct tcf_skbmod),
267};
268
269static __net_init int skbmod_init_net(struct net *net)
270{
271 struct tc_action_net *tn = net_generic(net, skbmod_net_id);
272
c7e460ce 273 return tc_action_net_init(tn, &act_skbmod_ops);
86da71b5
JHS
274}
275
039af9c6 276static void __net_exit skbmod_exit_net(struct list_head *net_list)
86da71b5 277{
039af9c6 278 tc_action_net_exit(net_list, skbmod_net_id);
86da71b5
JHS
279}
280
281static struct pernet_operations skbmod_net_ops = {
282 .init = skbmod_init_net,
039af9c6 283 .exit_batch = skbmod_exit_net,
86da71b5
JHS
284 .id = &skbmod_net_id,
285 .size = sizeof(struct tc_action_net),
286};
287
288MODULE_AUTHOR("Jamal Hadi Salim, <[email protected]>");
289MODULE_DESCRIPTION("SKB data mod-ing");
290MODULE_LICENSE("GPL");
291
292static int __init skbmod_init_module(void)
293{
294 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops);
295}
296
297static void __exit skbmod_cleanup_module(void)
298{
299 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops);
300}
301
302module_init(skbmod_init_module);
303module_exit(skbmod_cleanup_module);
This page took 0.232234 seconds and 4 git commands to generate.