1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
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>
14 #include <net/pkt_cls.h>
16 #include <linux/tc_act/tc_vlan.h>
17 #include <net/tc_act/tc_vlan.h>
19 static struct tc_action_ops act_vlan_ops;
21 static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a,
22 struct tcf_result *res)
24 struct tcf_vlan *v = to_vlan(a);
25 struct tcf_vlan_params *p;
30 tcf_lastuse_update(&v->tcf_tm);
31 tcf_action_update_bstats(&v->common, skb);
33 /* Ensure 'data' points at mac_header prior calling vlan manipulating
36 if (skb_at_tc_ingress(skb))
37 skb_push_rcsum(skb, skb->mac_len);
39 action = READ_ONCE(v->tcf_action);
41 p = rcu_dereference_bh(v->vlan_p);
43 switch (p->tcfv_action) {
44 case TCA_VLAN_ACT_POP:
45 err = skb_vlan_pop(skb);
49 case TCA_VLAN_ACT_PUSH:
50 err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid |
51 (p->tcfv_push_prio << VLAN_PRIO_SHIFT));
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))
59 /* extract existing tag (and guarantee no hw-accel tag) */
60 if (skb_vlan_tag_present(skb)) {
61 tci = skb_vlan_tag_get(skb);
62 __vlan_hwaccel_clear_tag(skb);
64 /* in-payload vlan tag, pop it */
65 err = __skb_vlan_pop(skb, &tci);
70 tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid;
71 /* replace prio bits, if tcfv_push_prio specified */
72 if (p->tcfv_push_prio_exists) {
73 tci &= ~VLAN_PRIO_MASK;
74 tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT;
76 /* put updated tci as hwaccel tag */
77 __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci);
79 case TCA_VLAN_ACT_POP_ETH:
80 err = skb_eth_pop(skb);
84 case TCA_VLAN_ACT_PUSH_ETH:
85 err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src);
94 if (skb_at_tc_ingress(skb))
95 skb_pull_rcsum(skb, skb->mac_len);
100 tcf_action_inc_drop_qstats(&v->common);
104 static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
105 [TCA_VLAN_UNSPEC] = { .strict_start_type = TCA_VLAN_PUSH_ETH_DST },
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 },
109 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
110 [TCA_VLAN_PUSH_ETH_DST] = NLA_POLICY_ETH_ADDR,
111 [TCA_VLAN_PUSH_ETH_SRC] = NLA_POLICY_ETH_ADDR,
114 static int tcf_vlan_init(struct net *net, struct nlattr *nla,
115 struct nlattr *est, struct tc_action **a,
116 struct tcf_proto *tp, u32 flags,
117 struct netlink_ext_ack *extack)
119 struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
120 bool bind = flags & TCA_ACT_FLAGS_BIND;
121 struct nlattr *tb[TCA_VLAN_MAX + 1];
122 struct tcf_chain *goto_ch = NULL;
123 bool push_prio_exists = false;
124 struct tcf_vlan_params *p;
125 struct tc_vlan *parm;
129 __be16 push_proto = 0;
138 err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy,
143 if (!tb[TCA_VLAN_PARMS])
145 parm = nla_data(tb[TCA_VLAN_PARMS]);
147 err = tcf_idr_check_alloc(tn, &index, a, bind);
154 switch (parm->v_action) {
155 case TCA_VLAN_ACT_POP:
157 case TCA_VLAN_ACT_PUSH:
158 case TCA_VLAN_ACT_MODIFY:
159 if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
161 tcf_idr_release(*a, bind);
163 tcf_idr_cleanup(tn, index);
166 push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
167 if (push_vid >= VLAN_VID_MASK) {
169 tcf_idr_release(*a, bind);
171 tcf_idr_cleanup(tn, index);
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):
183 tcf_idr_release(*a, bind);
185 tcf_idr_cleanup(tn, index);
186 return -EPROTONOSUPPORT;
189 push_proto = htons(ETH_P_8021Q);
192 push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY];
193 if (push_prio_exists)
194 push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
196 case TCA_VLAN_ACT_POP_ETH:
198 case TCA_VLAN_ACT_PUSH_ETH:
199 if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) {
201 tcf_idr_release(*a, bind);
203 tcf_idr_cleanup(tn, index);
209 tcf_idr_release(*a, bind);
211 tcf_idr_cleanup(tn, index);
214 action = parm->v_action;
217 ret = tcf_idr_create_from_flags(tn, index, est, a,
218 &act_vlan_ops, bind, flags);
220 tcf_idr_cleanup(tn, index);
225 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
226 tcf_idr_release(*a, bind);
230 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
236 p = kzalloc(sizeof(*p), GFP_KERNEL);
242 p->tcfv_action = action;
243 p->tcfv_push_vid = push_vid;
244 p->tcfv_push_prio = push_prio;
245 p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH;
246 p->tcfv_push_proto = push_proto;
248 if (action == TCA_VLAN_ACT_PUSH_ETH) {
249 nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST],
251 nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC],
255 spin_lock_bh(&v->tcf_lock);
256 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
257 p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock));
258 spin_unlock_bh(&v->tcf_lock);
261 tcf_chain_put_by_act(goto_ch);
268 tcf_chain_put_by_act(goto_ch);
270 tcf_idr_release(*a, bind);
274 static void tcf_vlan_cleanup(struct tc_action *a)
276 struct tcf_vlan *v = to_vlan(a);
277 struct tcf_vlan_params *p;
279 p = rcu_dereference_protected(v->vlan_p, 1);
284 static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
287 unsigned char *b = skb_tail_pointer(skb);
288 struct tcf_vlan *v = to_vlan(a);
289 struct tcf_vlan_params *p;
290 struct tc_vlan opt = {
291 .index = v->tcf_index,
292 .refcnt = refcount_read(&v->tcf_refcnt) - ref,
293 .bindcnt = atomic_read(&v->tcf_bindcnt) - bind,
297 spin_lock_bh(&v->tcf_lock);
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;
301 if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
302 goto nla_put_failure;
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) ||
307 nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
308 p->tcfv_push_proto) ||
309 (p->tcfv_push_prio_exists &&
310 nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
311 goto nla_put_failure;
313 if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) {
314 if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN,
316 goto nla_put_failure;
317 if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN,
319 goto nla_put_failure;
322 tcf_tm_dump(&t, &v->tcf_tm);
323 if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
324 goto nla_put_failure;
325 spin_unlock_bh(&v->tcf_lock);
330 spin_unlock_bh(&v->tcf_lock);
335 static void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets,
336 u64 drops, u64 lastuse, bool hw)
338 struct tcf_vlan *v = to_vlan(a);
339 struct tcf_t *tm = &v->tcf_tm;
341 tcf_action_update_stats(a, bytes, packets, drops, hw);
342 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
345 static size_t tcf_vlan_get_fill_size(const struct tc_action *act)
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 */
353 static int tcf_vlan_offload_act_setup(struct tc_action *act, void *entry_data,
354 u32 *index_inc, bool bind,
355 struct netlink_ext_ack *extack)
358 struct flow_action_entry *entry = entry_data;
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);
367 case TCA_VLAN_ACT_POP:
368 entry->id = FLOW_ACTION_VLAN_POP;
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);
376 case TCA_VLAN_ACT_POP_ETH:
377 entry->id = FLOW_ACTION_VLAN_POP_ETH;
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);
384 NL_SET_ERR_MSG_MOD(extack, "Unsupported vlan action mode offload");
389 struct flow_offload_action *fl_action = entry_data;
391 switch (tcf_vlan_action(act)) {
392 case TCA_VLAN_ACT_PUSH:
393 fl_action->id = FLOW_ACTION_VLAN_PUSH;
395 case TCA_VLAN_ACT_POP:
396 fl_action->id = FLOW_ACTION_VLAN_POP;
398 case TCA_VLAN_ACT_MODIFY:
399 fl_action->id = FLOW_ACTION_VLAN_MANGLE;
401 case TCA_VLAN_ACT_POP_ETH:
402 fl_action->id = FLOW_ACTION_VLAN_POP_ETH;
404 case TCA_VLAN_ACT_PUSH_ETH:
405 fl_action->id = FLOW_ACTION_VLAN_PUSH_ETH;
415 static struct tc_action_ops act_vlan_ops = {
418 .owner = THIS_MODULE,
420 .dump = tcf_vlan_dump,
421 .init = tcf_vlan_init,
422 .cleanup = tcf_vlan_cleanup,
423 .stats_update = tcf_vlan_stats_update,
424 .get_fill_size = tcf_vlan_get_fill_size,
425 .offload_act_setup = tcf_vlan_offload_act_setup,
426 .size = sizeof(struct tcf_vlan),
429 static __net_init int vlan_init_net(struct net *net)
431 struct tc_action_net *tn = net_generic(net, act_vlan_ops.net_id);
433 return tc_action_net_init(net, tn, &act_vlan_ops);
436 static void __net_exit vlan_exit_net(struct list_head *net_list)
438 tc_action_net_exit(net_list, act_vlan_ops.net_id);
441 static struct pernet_operations vlan_net_ops = {
442 .init = vlan_init_net,
443 .exit_batch = vlan_exit_net,
444 .id = &act_vlan_ops.net_id,
445 .size = sizeof(struct tc_action_net),
448 static int __init vlan_init_module(void)
450 return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
453 static void __exit vlan_cleanup_module(void)
455 tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
458 module_init(vlan_init_module);
459 module_exit(vlan_cleanup_module);
461 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
462 MODULE_DESCRIPTION("vlan manipulation actions");
463 MODULE_LICENSE("GPL v2");