]> Git Repo - linux.git/blame - net/sched/act_vlan.c
netfilter: conntrack: Fix data-races around ct mark
[linux.git] / net / sched / act_vlan.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
c7e2b968
JP
2/*
3 * Copyright (c) 2014 Jiri Pirko <[email protected]>
c7e2b968
JP
4 */
5
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/kernel.h>
9#include <linux/skbuff.h>
10#include <linux/rtnetlink.h>
11#include <linux/if_vlan.h>
12#include <net/netlink.h>
13#include <net/pkt_sched.h>
7e0c8892 14#include <net/pkt_cls.h>
c7e2b968
JP
15
16#include <linux/tc_act/tc_vlan.h>
17#include <net/tc_act/tc_vlan.h>
18
a85a970a 19static struct tc_action_ops act_vlan_ops;
ddf97ccd 20
8aa7f22e
JHS
21static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a,
22 struct tcf_result *res)
c7e2b968 23{
a85a970a 24 struct tcf_vlan *v = to_vlan(a);
4c5b9d96 25 struct tcf_vlan_params *p;
c7e2b968
JP
26 int action;
27 int err;
45a497f2 28 u16 tci;
c7e2b968 29
9c4a4e48 30 tcf_lastuse_update(&v->tcf_tm);
5e1ad95b 31 tcf_action_update_bstats(&v->common, skb);
e0496cbb 32
f39acc84
SL
33 /* Ensure 'data' points at mac_header prior calling vlan manipulating
34 * functions.
35 */
36 if (skb_at_tc_ingress(skb))
37 skb_push_rcsum(skb, skb->mac_len);
38
4c5b9d96
MK
39 action = READ_ONCE(v->tcf_action);
40
7fd4b288 41 p = rcu_dereference_bh(v->vlan_p);
4c5b9d96
MK
42
43 switch (p->tcfv_action) {
c7e2b968
JP
44 case TCA_VLAN_ACT_POP:
45 err = skb_vlan_pop(skb);
46 if (err)
47 goto drop;
48 break;
49 case TCA_VLAN_ACT_PUSH:
4c5b9d96
MK
50 err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid |
51 (p->tcfv_push_prio << VLAN_PRIO_SHIFT));
c7e2b968
JP
52 if (err)
53 goto drop;
54 break;
45a497f2
SL
55 case TCA_VLAN_ACT_MODIFY:
56 /* No-op if no vlan tag (either hw-accel or in-payload) */
57 if (!skb_vlan_tagged(skb))
7fd4b288 58 goto out;
45a497f2
SL
59 /* extract existing tag (and guarantee no hw-accel tag) */
60 if (skb_vlan_tag_present(skb)) {
61 tci = skb_vlan_tag_get(skb);
b1817524 62 __vlan_hwaccel_clear_tag(skb);
45a497f2
SL
63 } else {
64 /* in-payload vlan tag, pop it */
65 err = __skb_vlan_pop(skb, &tci);
66 if (err)
67 goto drop;
68 }
69 /* replace the vid */
4c5b9d96 70 tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid;
45a497f2 71 /* replace prio bits, if tcfv_push_prio specified */
9c5eee0a 72 if (p->tcfv_push_prio_exists) {
45a497f2 73 tci &= ~VLAN_PRIO_MASK;
4c5b9d96 74 tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
45a497f2
SL
75 }
76 /* put updated tci as hwaccel tag */
4c5b9d96 77 __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
45a497f2 78 break;
19fbcb36
GN
79 case TCA_VLAN_ACT_POP_ETH:
80 err = skb_eth_pop(skb);
81 if (err)
82 goto drop;
83 break;
84 case TCA_VLAN_ACT_PUSH_ETH:
85 err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src);
86 if (err)
87 goto drop;
88 break;
c7e2b968
JP
89 default:
90 BUG();
91 }
92
7fd4b288 93out:
f39acc84
SL
94 if (skb_at_tc_ingress(skb))
95 skb_pull_rcsum(skb, skb->mac_len);
96
c7e2b968 97 return action;
7fd4b288
PA
98
99drop:
26b537a8 100 tcf_action_inc_drop_qstats(&v->common);
7fd4b288 101 return TC_ACT_SHOT;
c7e2b968
JP
102}
103
104static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
19fbcb36 105 [TCA_VLAN_UNSPEC] = { .strict_start_type = TCA_VLAN_PUSH_ETH_DST },
c7e2b968
JP
106 [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
107 [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
108 [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
956af371 109 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
19fbcb36
GN
110 [TCA_VLAN_PUSH_ETH_DST] = NLA_POLICY_ETH_ADDR,
111 [TCA_VLAN_PUSH_ETH_SRC] = NLA_POLICY_ETH_ADDR,
c7e2b968
JP
112};
113
114static int tcf_vlan_init(struct net *net, struct nlattr *nla,
a85a970a 115 struct nlattr *est, struct tc_action **a,
abbb0d33
VB
116 struct tcf_proto *tp, u32 flags,
117 struct netlink_ext_ack *extack)
c7e2b968 118{
acd0a7ab 119 struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
695176bf 120 bool bind = flags & TCA_ACT_FLAGS_BIND;
c7e2b968 121 struct nlattr *tb[TCA_VLAN_MAX + 1];
7e0c8892 122 struct tcf_chain *goto_ch = NULL;
9c5eee0a 123 bool push_prio_exists = false;
764e9a24 124 struct tcf_vlan_params *p;
c7e2b968
JP
125 struct tc_vlan *parm;
126 struct tcf_vlan *v;
127 int action;
94cb5492 128 u16 push_vid = 0;
c7e2b968 129 __be16 push_proto = 0;
956af371 130 u8 push_prio = 0;
b2313077
WC
131 bool exists = false;
132 int ret = 0, err;
7be8ef2c 133 u32 index;
c7e2b968
JP
134
135 if (!nla)
136 return -EINVAL;
137
8cb08174
JB
138 err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy,
139 NULL);
c7e2b968
JP
140 if (err < 0)
141 return err;
142
143 if (!tb[TCA_VLAN_PARMS])
144 return -EINVAL;
145 parm = nla_data(tb[TCA_VLAN_PARMS]);
7be8ef2c
DL
146 index = parm->index;
147 err = tcf_idr_check_alloc(tn, &index, a, bind);
0190c1d4
VB
148 if (err < 0)
149 return err;
150 exists = err;
5026c9b1
JHS
151 if (exists && bind)
152 return 0;
153
c7e2b968
JP
154 switch (parm->v_action) {
155 case TCA_VLAN_ACT_POP:
156 break;
157 case TCA_VLAN_ACT_PUSH:
45a497f2 158 case TCA_VLAN_ACT_MODIFY:
5026c9b1
JHS
159 if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
160 if (exists)
65a206c0 161 tcf_idr_release(*a, bind);
0190c1d4 162 else
7be8ef2c 163 tcf_idr_cleanup(tn, index);
c7e2b968 164 return -EINVAL;
5026c9b1 165 }
c7e2b968 166 push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
5026c9b1
JHS
167 if (push_vid >= VLAN_VID_MASK) {
168 if (exists)
65a206c0 169 tcf_idr_release(*a, bind);
0190c1d4 170 else
7be8ef2c 171 tcf_idr_cleanup(tn, index);
c7e2b968 172 return -ERANGE;
5026c9b1 173 }
c7e2b968
JP
174
175 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
176 push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
177 switch (push_proto) {
178 case htons(ETH_P_8021Q):
179 case htons(ETH_P_8021AD):
180 break;
181 default:
5a4931ae
DC
182 if (exists)
183 tcf_idr_release(*a, bind);
0190c1d4 184 else
7be8ef2c 185 tcf_idr_cleanup(tn, index);
c7e2b968
JP
186 return -EPROTONOSUPPORT;
187 }
188 } else {
189 push_proto = htons(ETH_P_8021Q);
190 }
956af371 191
9c5eee0a
BS
192 push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY];
193 if (push_prio_exists)
956af371 194 push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
c7e2b968 195 break;
19fbcb36
GN
196 case TCA_VLAN_ACT_POP_ETH:
197 break;
198 case TCA_VLAN_ACT_PUSH_ETH:
199 if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) {
200 if (exists)
201 tcf_idr_release(*a, bind);
202 else
203 tcf_idr_cleanup(tn, index);
204 return -EINVAL;
205 }
206 break;
c7e2b968 207 default:
5026c9b1 208 if (exists)
65a206c0 209 tcf_idr_release(*a, bind);
0190c1d4 210 else
7be8ef2c 211 tcf_idr_cleanup(tn, index);
c7e2b968
JP
212 return -EINVAL;
213 }
214 action = parm->v_action;
215
5026c9b1 216 if (!exists) {
e3822678
VB
217 ret = tcf_idr_create_from_flags(tn, index, est, a,
218 &act_vlan_ops, bind, flags);
0190c1d4 219 if (ret) {
7be8ef2c 220 tcf_idr_cleanup(tn, index);
c7e2b968 221 return ret;
0190c1d4 222 }
c7e2b968
JP
223
224 ret = ACT_P_CREATED;
695176bf 225 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
65a206c0 226 tcf_idr_release(*a, bind);
4e8ddd7f 227 return -EEXIST;
c7e2b968
JP
228 }
229
7e0c8892
DC
230 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
231 if (err < 0)
232 goto release_idr;
233
a85a970a 234 v = to_vlan(*a);
c7e2b968 235
4c5b9d96
MK
236 p = kzalloc(sizeof(*p), GFP_KERNEL);
237 if (!p) {
7e0c8892
DC
238 err = -ENOMEM;
239 goto put_chain;
4c5b9d96 240 }
c7e2b968 241
4c5b9d96
MK
242 p->tcfv_action = action;
243 p->tcfv_push_vid = push_vid;
244 p->tcfv_push_prio = push_prio;
9c5eee0a 245 p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH;
4c5b9d96
MK
246 p->tcfv_push_proto = push_proto;
247
19fbcb36
GN
248 if (action == TCA_VLAN_ACT_PUSH_ETH) {
249 nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST],
250 ETH_ALEN);
251 nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC],
252 ETH_ALEN);
253 }
254
653cd284 255 spin_lock_bh(&v->tcf_lock);
7e0c8892 256 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
445d3749 257 p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
653cd284 258 spin_unlock_bh(&v->tcf_lock);
4c5b9d96 259
7e0c8892
DC
260 if (goto_ch)
261 tcf_chain_put_by_act(goto_ch);
764e9a24
VB
262 if (p)
263 kfree_rcu(p, rcu);
c7e2b968 264
c7e2b968 265 return ret;
7e0c8892
DC
266put_chain:
267 if (goto_ch)
268 tcf_chain_put_by_act(goto_ch);
269release_idr:
270 tcf_idr_release(*a, bind);
271 return err;
c7e2b968
JP
272}
273
9a63b255 274static void tcf_vlan_cleanup(struct tc_action *a)
4c5b9d96
MK
275{
276 struct tcf_vlan *v = to_vlan(a);
277 struct tcf_vlan_params *p;
278
279 p = rcu_dereference_protected(v->vlan_p, 1);
1edf8abe
DC
280 if (p)
281 kfree_rcu(p, rcu);
4c5b9d96
MK
282}
283
c7e2b968
JP
284static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
285 int bind, int ref)
286{
287 unsigned char *b = skb_tail_pointer(skb);
a85a970a 288 struct tcf_vlan *v = to_vlan(a);
764e9a24 289 struct tcf_vlan_params *p;
c7e2b968
JP
290 struct tc_vlan opt = {
291 .index = v->tcf_index,
036bb443
VB
292 .refcnt = refcount_read(&v->tcf_refcnt) - ref,
293 .bindcnt = atomic_read(&v->tcf_bindcnt) - bind,
c7e2b968
JP
294 };
295 struct tcf_t t;
296
653cd284 297 spin_lock_bh(&v->tcf_lock);
764e9a24
VB
298 opt.action = v->tcf_action;
299 p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
300 opt.v_action = p->tcfv_action;
c7e2b968
JP
301 if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
302 goto nla_put_failure;
303
4c5b9d96
MK
304 if ((p->tcfv_action == TCA_VLAN_ACT_PUSH ||
305 p->tcfv_action == TCA_VLAN_ACT_MODIFY) &&
306 (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) ||
0b0f43fe 307 nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
4c5b9d96 308 p->tcfv_push_proto) ||
8323b20f
BS
309 (p->tcfv_push_prio_exists &&
310 nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
c7e2b968
JP
311 goto nla_put_failure;
312
19fbcb36
GN
313 if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) {
314 if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN,
315 p->tcfv_push_dst))
316 goto nla_put_failure;
317 if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN,
318 p->tcfv_push_src))
319 goto nla_put_failure;
320 }
321
48d8ee16 322 tcf_tm_dump(&t, &v->tcf_tm);
9854518e 323 if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
c7e2b968 324 goto nla_put_failure;
653cd284 325 spin_unlock_bh(&v->tcf_lock);
764e9a24 326
c7e2b968
JP
327 return skb->len;
328
329nla_put_failure:
653cd284 330 spin_unlock_bh(&v->tcf_lock);
c7e2b968
JP
331 nlmsg_trim(skb, b);
332 return -1;
333}
334
4b61d3e8
PL
335static void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets,
336 u64 drops, u64 lastuse, bool hw)
fa730a3b
JP
337{
338 struct tcf_vlan *v = to_vlan(a);
339 struct tcf_t *tm = &v->tcf_tm;
340
4b61d3e8 341 tcf_action_update_stats(a, bytes, packets, drops, hw);
fa730a3b
JP
342 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
343}
344
b35475c5
RM
345static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
346{
347 return nla_total_size(sizeof(struct tc_vlan))
348 + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
349 + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
350 + nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
351}
352
c54e1d92 353static int tcf_vlan_offload_act_setup(struct tc_action *act, void *entry_data,
c2ccf84e
IS
354 u32 *index_inc, bool bind,
355 struct netlink_ext_ack *extack)
c54e1d92
BZ
356{
357 if (bind) {
358 struct flow_action_entry *entry = entry_data;
359
360 switch (tcf_vlan_action(act)) {
361 case TCA_VLAN_ACT_PUSH:
362 entry->id = FLOW_ACTION_VLAN_PUSH;
363 entry->vlan.vid = tcf_vlan_push_vid(act);
364 entry->vlan.proto = tcf_vlan_push_proto(act);
365 entry->vlan.prio = tcf_vlan_push_prio(act);
366 break;
367 case TCA_VLAN_ACT_POP:
368 entry->id = FLOW_ACTION_VLAN_POP;
369 break;
370 case TCA_VLAN_ACT_MODIFY:
371 entry->id = FLOW_ACTION_VLAN_MANGLE;
372 entry->vlan.vid = tcf_vlan_push_vid(act);
373 entry->vlan.proto = tcf_vlan_push_proto(act);
374 entry->vlan.prio = tcf_vlan_push_prio(act);
375 break;
ab95465c
MD
376 case TCA_VLAN_ACT_POP_ETH:
377 entry->id = FLOW_ACTION_VLAN_POP_ETH;
378 break;
379 case TCA_VLAN_ACT_PUSH_ETH:
380 entry->id = FLOW_ACTION_VLAN_PUSH_ETH;
381 tcf_vlan_push_eth(entry->vlan_push_eth.src, entry->vlan_push_eth.dst, act);
382 break;
c54e1d92 383 default:
f8fab316 384 NL_SET_ERR_MSG_MOD(extack, "Unsupported vlan action mode offload");
c54e1d92
BZ
385 return -EOPNOTSUPP;
386 }
387 *index_inc = 1;
388 } else {
8cbfe939
BZ
389 struct flow_offload_action *fl_action = entry_data;
390
391 switch (tcf_vlan_action(act)) {
392 case TCA_VLAN_ACT_PUSH:
393 fl_action->id = FLOW_ACTION_VLAN_PUSH;
394 break;
395 case TCA_VLAN_ACT_POP:
396 fl_action->id = FLOW_ACTION_VLAN_POP;
397 break;
398 case TCA_VLAN_ACT_MODIFY:
399 fl_action->id = FLOW_ACTION_VLAN_MANGLE;
400 break;
ab95465c
MD
401 case TCA_VLAN_ACT_POP_ETH:
402 fl_action->id = FLOW_ACTION_VLAN_POP_ETH;
403 break;
404 case TCA_VLAN_ACT_PUSH_ETH:
405 fl_action->id = FLOW_ACTION_VLAN_PUSH_ETH;
406 break;
8cbfe939
BZ
407 default:
408 return -EOPNOTSUPP;
409 }
c54e1d92
BZ
410 }
411
412 return 0;
413}
414
c7e2b968
JP
415static struct tc_action_ops act_vlan_ops = {
416 .kind = "vlan",
eddd2cf1 417 .id = TCA_ID_VLAN,
c7e2b968 418 .owner = THIS_MODULE,
8aa7f22e 419 .act = tcf_vlan_act,
c7e2b968
JP
420 .dump = tcf_vlan_dump,
421 .init = tcf_vlan_init,
4c5b9d96 422 .cleanup = tcf_vlan_cleanup,
fa730a3b 423 .stats_update = tcf_vlan_stats_update,
b35475c5 424 .get_fill_size = tcf_vlan_get_fill_size,
c54e1d92 425 .offload_act_setup = tcf_vlan_offload_act_setup,
a85a970a 426 .size = sizeof(struct tcf_vlan),
ddf97ccd
WC
427};
428
429static __net_init int vlan_init_net(struct net *net)
430{
acd0a7ab 431 struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
ddf97ccd 432
981471bd 433 return tc_action_net_init(net, tn, &act_vlan_ops);
ddf97ccd
WC
434}
435
039af9c6 436static void __net_exit vlan_exit_net(struct list_head *net_list)
ddf97ccd 437{
acd0a7ab 438 tc_action_net_exit(net_list, act_vlan_ops.net_id);
ddf97ccd
WC
439}
440
441static struct pernet_operations vlan_net_ops = {
442 .init = vlan_init_net,
039af9c6 443 .exit_batch = vlan_exit_net,
acd0a7ab 444 .id = &act_vlan_ops.net_id,
ddf97ccd 445 .size = sizeof(struct tc_action_net),
c7e2b968
JP
446};
447
448static int __init vlan_init_module(void)
449{
ddf97ccd 450 return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
c7e2b968
JP
451}
452
453static void __exit vlan_cleanup_module(void)
454{
ddf97ccd 455 tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
c7e2b968
JP
456}
457
458module_init(vlan_init_module);
459module_exit(vlan_cleanup_module);
460
461MODULE_AUTHOR("Jiri Pirko <[email protected]>");
462MODULE_DESCRIPTION("vlan manipulation actions");
463MODULE_LICENSE("GPL v2");
This page took 0.616083 seconds and 4 git commands to generate.