Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
86da71b5 JHS |
2 | /* |
3 | * net/sched/act_skbmod.c skb data modifier | |
4 | * | |
5 | * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com> | |
86da71b5 JHS |
6 | */ |
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> | |
7c3d825d | 15 | #include <net/pkt_cls.h> |
86da71b5 JHS |
16 | |
17 | #include <linux/tc_act/tc_skbmod.h> | |
18 | #include <net/tc_act/tc_skbmod.h> | |
19 | ||
c7d03a00 | 20 | static unsigned int skbmod_net_id; |
86da71b5 JHS |
21 | static struct tc_action_ops act_skbmod_ops; |
22 | ||
23 | #define MAX_EDIT_LEN ETH_HLEN | |
353d2c25 | 24 | static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a, |
86da71b5 JHS |
25 | 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); | |
7fd4b288 PA |
41 | if (unlikely(err)) /* best policy is to drop on the floor */ |
42 | goto drop; | |
86da71b5 | 43 | |
86da71b5 | 44 | action = READ_ONCE(d->tcf_action); |
7fd4b288 PA |
45 | if (unlikely(action == TC_ACT_SHOT)) |
46 | goto drop; | |
86da71b5 | 47 | |
7fd4b288 | 48 | p = rcu_dereference_bh(d->skbmod_p); |
86da71b5 JHS |
49 | 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; | |
86da71b5 JHS |
56 | |
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; | |
7fd4b288 PA |
66 | |
67 | drop: | |
68 | qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats)); | |
69 | return TC_ACT_SHOT; | |
86da71b5 JHS |
70 | } |
71 | ||
72 | static 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 | ||
79 | static int tcf_skbmod_init(struct net *net, struct nlattr *nla, | |
80 | struct nlattr *est, struct tc_action **a, | |
789871bb | 81 | int ovr, int bind, bool rtnl_held, |
abbb0d33 | 82 | struct tcf_proto *tp, u32 flags, |
789871bb | 83 | struct netlink_ext_ack *extack) |
86da71b5 JHS |
84 | { |
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; | |
7c3d825d | 88 | struct tcf_chain *goto_ch = NULL; |
86da71b5 | 89 | struct tc_skbmod *parm; |
7be8ef2c | 90 | u32 lflags = 0, index; |
86da71b5 JHS |
91 | struct tcf_skbmod *d; |
92 | bool exists = false; | |
93 | u8 *daddr = NULL; | |
94 | u8 *saddr = NULL; | |
95 | u16 eth_type = 0; | |
86da71b5 JHS |
96 | int ret = 0, err; |
97 | ||
98 | if (!nla) | |
99 | return -EINVAL; | |
100 | ||
8cb08174 JB |
101 | err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla, |
102 | skbmod_policy, NULL); | |
86da71b5 JHS |
103 | 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]); | |
7be8ef2c | 125 | index = parm->index; |
86da71b5 JHS |
126 | if (parm->flags & SKBMOD_F_SWAPMAC) |
127 | lflags = SKBMOD_F_SWAPMAC; | |
128 | ||
7be8ef2c | 129 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
0190c1d4 VB |
130 | if (err < 0) |
131 | return err; | |
132 | exists = err; | |
86da71b5 JHS |
133 | if (exists && bind) |
134 | return 0; | |
135 | ||
a52956df RM |
136 | if (!lflags) { |
137 | if (exists) | |
138 | tcf_idr_release(*a, bind); | |
0190c1d4 | 139 | else |
7be8ef2c | 140 | tcf_idr_cleanup(tn, index); |
86da71b5 | 141 | return -EINVAL; |
a52956df | 142 | } |
86da71b5 JHS |
143 | |
144 | if (!exists) { | |
7be8ef2c | 145 | ret = tcf_idr_create(tn, index, est, a, |
e3822678 | 146 | &act_skbmod_ops, bind, true, 0); |
0190c1d4 | 147 | if (ret) { |
7be8ef2c | 148 | tcf_idr_cleanup(tn, index); |
86da71b5 | 149 | return ret; |
0190c1d4 | 150 | } |
86da71b5 JHS |
151 | |
152 | ret = ACT_P_CREATED; | |
4e8ddd7f | 153 | } else if (!ovr) { |
65a206c0 | 154 | tcf_idr_release(*a, bind); |
4e8ddd7f | 155 | return -EEXIST; |
86da71b5 | 156 | } |
7c3d825d DC |
157 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
158 | if (err < 0) | |
159 | goto release_idr; | |
86da71b5 JHS |
160 | |
161 | d = to_skbmod(*a); | |
162 | ||
86da71b5 JHS |
163 | p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL); |
164 | if (unlikely(!p)) { | |
7c3d825d DC |
165 | err = -ENOMEM; |
166 | goto put_chain; | |
86da71b5 JHS |
167 | } |
168 | ||
169 | p->flags = lflags; | |
86da71b5 | 170 | |
86da71b5 JHS |
171 | if (ovr) |
172 | spin_lock_bh(&d->tcf_lock); | |
c8814552 | 173 | /* Protected by tcf_lock if overwriting existing action. */ |
7c3d825d | 174 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
c8814552 | 175 | p_old = rcu_dereference_protected(d->skbmod_p, 1); |
86da71b5 JHS |
176 | |
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); | |
7c3d825d DC |
190 | if (goto_ch) |
191 | tcf_chain_put_by_act(goto_ch); | |
86da71b5 | 192 | |
86da71b5 | 193 | return ret; |
7c3d825d DC |
194 | put_chain: |
195 | if (goto_ch) | |
196 | tcf_chain_put_by_act(goto_ch); | |
197 | release_idr: | |
198 | tcf_idr_release(*a, bind); | |
199 | return err; | |
86da71b5 JHS |
200 | } |
201 | ||
9a63b255 | 202 | static void tcf_skbmod_cleanup(struct tc_action *a) |
86da71b5 JHS |
203 | { |
204 | struct tcf_skbmod *d = to_skbmod(a); | |
205 | struct tcf_skbmod_params *p; | |
206 | ||
207 | p = rcu_dereference_protected(d->skbmod_p, 1); | |
2d433610 DC |
208 | if (p) |
209 | kfree_rcu(p, rcu); | |
86da71b5 JHS |
210 | } |
211 | ||
212 | static 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); | |
c8814552 | 217 | struct tcf_skbmod_params *p; |
86da71b5 JHS |
218 | struct tc_skbmod opt = { |
219 | .index = d->tcf_index, | |
036bb443 VB |
220 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
221 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, | |
86da71b5 JHS |
222 | }; |
223 | struct tcf_t t; | |
224 | ||
c8814552 VB |
225 | 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)); | |
86da71b5 JHS |
229 | 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 | ||
c8814552 | 246 | spin_unlock_bh(&d->tcf_lock); |
86da71b5 JHS |
247 | return skb->len; |
248 | nla_put_failure: | |
c8814552 | 249 | spin_unlock_bh(&d->tcf_lock); |
86da71b5 JHS |
250 | nlmsg_trim(skb, b); |
251 | return -1; | |
252 | } | |
253 | ||
254 | static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb, | |
255 | struct netlink_callback *cb, int type, | |
41780105 AA |
256 | const struct tc_action_ops *ops, |
257 | struct netlink_ext_ack *extack) | |
86da71b5 JHS |
258 | { |
259 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
260 | ||
b3620145 | 261 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
86da71b5 JHS |
262 | } |
263 | ||
f061b48c | 264 | static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index) |
86da71b5 JHS |
265 | { |
266 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
267 | ||
65a206c0 | 268 | return tcf_idr_search(tn, a, index); |
86da71b5 JHS |
269 | } |
270 | ||
271 | static struct tc_action_ops act_skbmod_ops = { | |
272 | .kind = "skbmod", | |
eddd2cf1 | 273 | .id = TCA_ACT_SKBMOD, |
86da71b5 | 274 | .owner = THIS_MODULE, |
353d2c25 | 275 | .act = tcf_skbmod_act, |
86da71b5 JHS |
276 | .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 | ||
284 | static __net_init int skbmod_init_net(struct net *net) | |
285 | { | |
286 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); | |
287 | ||
981471bd | 288 | return tc_action_net_init(net, tn, &act_skbmod_ops); |
86da71b5 JHS |
289 | } |
290 | ||
039af9c6 | 291 | static void __net_exit skbmod_exit_net(struct list_head *net_list) |
86da71b5 | 292 | { |
039af9c6 | 293 | tc_action_net_exit(net_list, skbmod_net_id); |
86da71b5 JHS |
294 | } |
295 | ||
296 | static struct pernet_operations skbmod_net_ops = { | |
297 | .init = skbmod_init_net, | |
039af9c6 | 298 | .exit_batch = skbmod_exit_net, |
86da71b5 JHS |
299 | .id = &skbmod_net_id, |
300 | .size = sizeof(struct tc_action_net), | |
301 | }; | |
302 | ||
303 | MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>"); | |
304 | MODULE_DESCRIPTION("SKB data mod-ing"); | |
305 | MODULE_LICENSE("GPL"); | |
306 | ||
307 | static int __init skbmod_init_module(void) | |
308 | { | |
309 | return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops); | |
310 | } | |
311 | ||
312 | static void __exit skbmod_cleanup_module(void) | |
313 | { | |
314 | tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops); | |
315 | } | |
316 | ||
317 | module_init(skbmod_init_module); | |
318 | module_exit(skbmod_cleanup_module); |