1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2c7e2b968SJiri Pirko /* 3c7e2b968SJiri Pirko * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> 4c7e2b968SJiri Pirko */ 5c7e2b968SJiri Pirko 6c7e2b968SJiri Pirko #include <linux/module.h> 7c7e2b968SJiri Pirko #include <linux/init.h> 8c7e2b968SJiri Pirko #include <linux/kernel.h> 9c7e2b968SJiri Pirko #include <linux/skbuff.h> 10c7e2b968SJiri Pirko #include <linux/rtnetlink.h> 11c7e2b968SJiri Pirko #include <linux/if_vlan.h> 12c7e2b968SJiri Pirko #include <net/netlink.h> 13c7e2b968SJiri Pirko #include <net/pkt_sched.h> 147e0c8892SDavide Caratti #include <net/pkt_cls.h> 15c7e2b968SJiri Pirko 16c7e2b968SJiri Pirko #include <linux/tc_act/tc_vlan.h> 17c7e2b968SJiri Pirko #include <net/tc_act/tc_vlan.h> 18c7e2b968SJiri Pirko 19c7d03a00SAlexey Dobriyan static unsigned int vlan_net_id; 20a85a970aSWANG Cong static struct tc_action_ops act_vlan_ops; 21ddf97ccdSWANG Cong 228aa7f22eSJamal Hadi Salim static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a, 23c7e2b968SJiri Pirko struct tcf_result *res) 24c7e2b968SJiri Pirko { 25a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 264c5b9d96SManish Kurup struct tcf_vlan_params *p; 27c7e2b968SJiri Pirko int action; 28c7e2b968SJiri Pirko int err; 2945a497f2SShmulik Ladkani u16 tci; 30c7e2b968SJiri Pirko 319c4a4e48SJamal Hadi Salim tcf_lastuse_update(&v->tcf_tm); 32e0496cbbSManish Kurup bstats_cpu_update(this_cpu_ptr(v->common.cpu_bstats), skb); 33e0496cbbSManish Kurup 34f39acc84SShmulik Ladkani /* Ensure 'data' points at mac_header prior calling vlan manipulating 35f39acc84SShmulik Ladkani * functions. 36f39acc84SShmulik Ladkani */ 37f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 38f39acc84SShmulik Ladkani skb_push_rcsum(skb, skb->mac_len); 39f39acc84SShmulik Ladkani 404c5b9d96SManish Kurup action = READ_ONCE(v->tcf_action); 414c5b9d96SManish Kurup 427fd4b288SPaolo Abeni p = rcu_dereference_bh(v->vlan_p); 434c5b9d96SManish Kurup 444c5b9d96SManish Kurup switch (p->tcfv_action) { 45c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 46c7e2b968SJiri Pirko err = skb_vlan_pop(skb); 47c7e2b968SJiri Pirko if (err) 48c7e2b968SJiri Pirko goto drop; 49c7e2b968SJiri Pirko break; 50c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 514c5b9d96SManish Kurup err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid | 524c5b9d96SManish Kurup (p->tcfv_push_prio << VLAN_PRIO_SHIFT)); 53c7e2b968SJiri Pirko if (err) 54c7e2b968SJiri Pirko goto drop; 55c7e2b968SJiri Pirko break; 5645a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 5745a497f2SShmulik Ladkani /* No-op if no vlan tag (either hw-accel or in-payload) */ 5845a497f2SShmulik Ladkani if (!skb_vlan_tagged(skb)) 597fd4b288SPaolo Abeni goto out; 6045a497f2SShmulik Ladkani /* extract existing tag (and guarantee no hw-accel tag) */ 6145a497f2SShmulik Ladkani if (skb_vlan_tag_present(skb)) { 6245a497f2SShmulik Ladkani tci = skb_vlan_tag_get(skb); 63b1817524SMichał Mirosław __vlan_hwaccel_clear_tag(skb); 6445a497f2SShmulik Ladkani } else { 6545a497f2SShmulik Ladkani /* in-payload vlan tag, pop it */ 6645a497f2SShmulik Ladkani err = __skb_vlan_pop(skb, &tci); 6745a497f2SShmulik Ladkani if (err) 6845a497f2SShmulik Ladkani goto drop; 6945a497f2SShmulik Ladkani } 7045a497f2SShmulik Ladkani /* replace the vid */ 714c5b9d96SManish Kurup tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid; 7245a497f2SShmulik Ladkani /* replace prio bits, if tcfv_push_prio specified */ 734c5b9d96SManish Kurup if (p->tcfv_push_prio) { 7445a497f2SShmulik Ladkani tci &= ~VLAN_PRIO_MASK; 754c5b9d96SManish Kurup tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT; 7645a497f2SShmulik Ladkani } 7745a497f2SShmulik Ladkani /* put updated tci as hwaccel tag */ 784c5b9d96SManish Kurup __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci); 7945a497f2SShmulik Ladkani break; 80c7e2b968SJiri Pirko default: 81c7e2b968SJiri Pirko BUG(); 82c7e2b968SJiri Pirko } 83c7e2b968SJiri Pirko 847fd4b288SPaolo Abeni out: 85f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 86f39acc84SShmulik Ladkani skb_pull_rcsum(skb, skb->mac_len); 87f39acc84SShmulik Ladkani 88c7e2b968SJiri Pirko return action; 897fd4b288SPaolo Abeni 907fd4b288SPaolo Abeni drop: 917fd4b288SPaolo Abeni qstats_drop_inc(this_cpu_ptr(v->common.cpu_qstats)); 927fd4b288SPaolo Abeni return TC_ACT_SHOT; 93c7e2b968SJiri Pirko } 94c7e2b968SJiri Pirko 95c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { 96c7e2b968SJiri Pirko [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, 97c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, 98c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, 99956af371SHadar Hen Zion [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, 100c7e2b968SJiri Pirko }; 101c7e2b968SJiri Pirko 102c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla, 103a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 104789871bbSVlad Buslov int ovr, int bind, bool rtnl_held, 10585d0966fSDavide Caratti struct tcf_proto *tp, struct netlink_ext_ack *extack) 106c7e2b968SJiri Pirko { 107ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 108c7e2b968SJiri Pirko struct nlattr *tb[TCA_VLAN_MAX + 1]; 1097e0c8892SDavide Caratti struct tcf_chain *goto_ch = NULL; 110764e9a24SVlad Buslov struct tcf_vlan_params *p; 111c7e2b968SJiri Pirko struct tc_vlan *parm; 112c7e2b968SJiri Pirko struct tcf_vlan *v; 113c7e2b968SJiri Pirko int action; 11494cb5492SDavide Caratti u16 push_vid = 0; 115c7e2b968SJiri Pirko __be16 push_proto = 0; 116956af371SHadar Hen Zion u8 push_prio = 0; 117b2313077SWANG Cong bool exists = false; 118b2313077SWANG Cong int ret = 0, err; 119c7e2b968SJiri Pirko 120c7e2b968SJiri Pirko if (!nla) 121c7e2b968SJiri Pirko return -EINVAL; 122c7e2b968SJiri Pirko 1238cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy, 1248cb08174SJohannes Berg NULL); 125c7e2b968SJiri Pirko if (err < 0) 126c7e2b968SJiri Pirko return err; 127c7e2b968SJiri Pirko 128c7e2b968SJiri Pirko if (!tb[TCA_VLAN_PARMS]) 129c7e2b968SJiri Pirko return -EINVAL; 130c7e2b968SJiri Pirko parm = nla_data(tb[TCA_VLAN_PARMS]); 1310190c1d4SVlad Buslov err = tcf_idr_check_alloc(tn, &parm->index, a, bind); 1320190c1d4SVlad Buslov if (err < 0) 1330190c1d4SVlad Buslov return err; 1340190c1d4SVlad Buslov exists = err; 1355026c9b1SJamal Hadi Salim if (exists && bind) 1365026c9b1SJamal Hadi Salim return 0; 1375026c9b1SJamal Hadi Salim 138c7e2b968SJiri Pirko switch (parm->v_action) { 139c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 140c7e2b968SJiri Pirko break; 141c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 14245a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 1435026c9b1SJamal Hadi Salim if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { 1445026c9b1SJamal Hadi Salim if (exists) 14565a206c0SChris Mi tcf_idr_release(*a, bind); 1460190c1d4SVlad Buslov else 1470190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 148c7e2b968SJiri Pirko return -EINVAL; 1495026c9b1SJamal Hadi Salim } 150c7e2b968SJiri Pirko push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); 1515026c9b1SJamal Hadi Salim if (push_vid >= VLAN_VID_MASK) { 1525026c9b1SJamal Hadi Salim if (exists) 15365a206c0SChris Mi tcf_idr_release(*a, bind); 1540190c1d4SVlad Buslov else 1550190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 156c7e2b968SJiri Pirko return -ERANGE; 1575026c9b1SJamal Hadi Salim } 158c7e2b968SJiri Pirko 159c7e2b968SJiri Pirko if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { 160c7e2b968SJiri Pirko push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); 161c7e2b968SJiri Pirko switch (push_proto) { 162c7e2b968SJiri Pirko case htons(ETH_P_8021Q): 163c7e2b968SJiri Pirko case htons(ETH_P_8021AD): 164c7e2b968SJiri Pirko break; 165c7e2b968SJiri Pirko default: 1665a4931aeSDavide Caratti if (exists) 1675a4931aeSDavide Caratti tcf_idr_release(*a, bind); 1680190c1d4SVlad Buslov else 1690190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 170c7e2b968SJiri Pirko return -EPROTONOSUPPORT; 171c7e2b968SJiri Pirko } 172c7e2b968SJiri Pirko } else { 173c7e2b968SJiri Pirko push_proto = htons(ETH_P_8021Q); 174c7e2b968SJiri Pirko } 175956af371SHadar Hen Zion 176956af371SHadar Hen Zion if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) 177956af371SHadar Hen Zion push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); 178c7e2b968SJiri Pirko break; 179c7e2b968SJiri Pirko default: 1805026c9b1SJamal Hadi Salim if (exists) 18165a206c0SChris Mi tcf_idr_release(*a, bind); 1820190c1d4SVlad Buslov else 1830190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 184c7e2b968SJiri Pirko return -EINVAL; 185c7e2b968SJiri Pirko } 186c7e2b968SJiri Pirko action = parm->v_action; 187c7e2b968SJiri Pirko 1885026c9b1SJamal Hadi Salim if (!exists) { 18965a206c0SChris Mi ret = tcf_idr_create(tn, parm->index, est, a, 190e0496cbbSManish Kurup &act_vlan_ops, bind, true); 1910190c1d4SVlad Buslov if (ret) { 1920190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 193c7e2b968SJiri Pirko return ret; 1940190c1d4SVlad Buslov } 195c7e2b968SJiri Pirko 196c7e2b968SJiri Pirko ret = ACT_P_CREATED; 1974e8ddd7fSVlad Buslov } else if (!ovr) { 19865a206c0SChris Mi tcf_idr_release(*a, bind); 199c7e2b968SJiri Pirko return -EEXIST; 200c7e2b968SJiri Pirko } 201c7e2b968SJiri Pirko 2027e0c8892SDavide Caratti err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 2037e0c8892SDavide Caratti if (err < 0) 2047e0c8892SDavide Caratti goto release_idr; 2057e0c8892SDavide Caratti 206a85a970aSWANG Cong v = to_vlan(*a); 207c7e2b968SJiri Pirko 2084c5b9d96SManish Kurup p = kzalloc(sizeof(*p), GFP_KERNEL); 2094c5b9d96SManish Kurup if (!p) { 2107e0c8892SDavide Caratti err = -ENOMEM; 2117e0c8892SDavide Caratti goto put_chain; 2124c5b9d96SManish Kurup } 213c7e2b968SJiri Pirko 2144c5b9d96SManish Kurup p->tcfv_action = action; 2154c5b9d96SManish Kurup p->tcfv_push_vid = push_vid; 2164c5b9d96SManish Kurup p->tcfv_push_prio = push_prio; 2174c5b9d96SManish Kurup p->tcfv_push_proto = push_proto; 2184c5b9d96SManish Kurup 219653cd284SVlad Buslov spin_lock_bh(&v->tcf_lock); 2207e0c8892SDavide Caratti goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 221764e9a24SVlad Buslov rcu_swap_protected(v->vlan_p, p, lockdep_is_held(&v->tcf_lock)); 222653cd284SVlad Buslov spin_unlock_bh(&v->tcf_lock); 2234c5b9d96SManish Kurup 2247e0c8892SDavide Caratti if (goto_ch) 2257e0c8892SDavide Caratti tcf_chain_put_by_act(goto_ch); 226764e9a24SVlad Buslov if (p) 227764e9a24SVlad Buslov kfree_rcu(p, rcu); 228c7e2b968SJiri Pirko 229c7e2b968SJiri Pirko if (ret == ACT_P_CREATED) 23065a206c0SChris Mi tcf_idr_insert(tn, *a); 231c7e2b968SJiri Pirko return ret; 2327e0c8892SDavide Caratti put_chain: 2337e0c8892SDavide Caratti if (goto_ch) 2347e0c8892SDavide Caratti tcf_chain_put_by_act(goto_ch); 2357e0c8892SDavide Caratti release_idr: 2367e0c8892SDavide Caratti tcf_idr_release(*a, bind); 2377e0c8892SDavide Caratti return err; 238c7e2b968SJiri Pirko } 239c7e2b968SJiri Pirko 2409a63b255SCong Wang static void tcf_vlan_cleanup(struct tc_action *a) 2414c5b9d96SManish Kurup { 2424c5b9d96SManish Kurup struct tcf_vlan *v = to_vlan(a); 2434c5b9d96SManish Kurup struct tcf_vlan_params *p; 2444c5b9d96SManish Kurup 2454c5b9d96SManish Kurup p = rcu_dereference_protected(v->vlan_p, 1); 2461edf8abeSDavide Caratti if (p) 2474c5b9d96SManish Kurup kfree_rcu(p, rcu); 2484c5b9d96SManish Kurup } 2494c5b9d96SManish Kurup 250c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, 251c7e2b968SJiri Pirko int bind, int ref) 252c7e2b968SJiri Pirko { 253c7e2b968SJiri Pirko unsigned char *b = skb_tail_pointer(skb); 254a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 255764e9a24SVlad Buslov struct tcf_vlan_params *p; 256c7e2b968SJiri Pirko struct tc_vlan opt = { 257c7e2b968SJiri Pirko .index = v->tcf_index, 258036bb443SVlad Buslov .refcnt = refcount_read(&v->tcf_refcnt) - ref, 259036bb443SVlad Buslov .bindcnt = atomic_read(&v->tcf_bindcnt) - bind, 260c7e2b968SJiri Pirko }; 261c7e2b968SJiri Pirko struct tcf_t t; 262c7e2b968SJiri Pirko 263653cd284SVlad Buslov spin_lock_bh(&v->tcf_lock); 264764e9a24SVlad Buslov opt.action = v->tcf_action; 265764e9a24SVlad Buslov p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock)); 266764e9a24SVlad Buslov opt.v_action = p->tcfv_action; 267c7e2b968SJiri Pirko if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) 268c7e2b968SJiri Pirko goto nla_put_failure; 269c7e2b968SJiri Pirko 2704c5b9d96SManish Kurup if ((p->tcfv_action == TCA_VLAN_ACT_PUSH || 2714c5b9d96SManish Kurup p->tcfv_action == TCA_VLAN_ACT_MODIFY) && 2724c5b9d96SManish Kurup (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || 2730b0f43feSJamal Hadi Salim nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, 2744c5b9d96SManish Kurup p->tcfv_push_proto) || 275956af371SHadar Hen Zion (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, 2764c5b9d96SManish Kurup p->tcfv_push_prio)))) 277c7e2b968SJiri Pirko goto nla_put_failure; 278c7e2b968SJiri Pirko 27948d8ee16SJamal Hadi Salim tcf_tm_dump(&t, &v->tcf_tm); 2809854518eSNicolas Dichtel if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) 281c7e2b968SJiri Pirko goto nla_put_failure; 282653cd284SVlad Buslov spin_unlock_bh(&v->tcf_lock); 283764e9a24SVlad Buslov 284c7e2b968SJiri Pirko return skb->len; 285c7e2b968SJiri Pirko 286c7e2b968SJiri Pirko nla_put_failure: 287653cd284SVlad Buslov spin_unlock_bh(&v->tcf_lock); 288c7e2b968SJiri Pirko nlmsg_trim(skb, b); 289c7e2b968SJiri Pirko return -1; 290c7e2b968SJiri Pirko } 291c7e2b968SJiri Pirko 292ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb, 293ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 29441780105SAlexander Aring const struct tc_action_ops *ops, 29541780105SAlexander Aring struct netlink_ext_ack *extack) 296ddf97ccdSWANG Cong { 297ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 298ddf97ccdSWANG Cong 299b3620145SAlexander Aring return tcf_generic_walker(tn, skb, cb, type, ops, extack); 300ddf97ccdSWANG Cong } 301ddf97ccdSWANG Cong 302f061b48cSCong Wang static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) 303ddf97ccdSWANG Cong { 304ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 305ddf97ccdSWANG Cong 30665a206c0SChris Mi return tcf_idr_search(tn, a, index); 307ddf97ccdSWANG Cong } 308ddf97ccdSWANG Cong 309c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = { 310c7e2b968SJiri Pirko .kind = "vlan", 311eddd2cf1SEli Cohen .id = TCA_ID_VLAN, 312c7e2b968SJiri Pirko .owner = THIS_MODULE, 3138aa7f22eSJamal Hadi Salim .act = tcf_vlan_act, 314c7e2b968SJiri Pirko .dump = tcf_vlan_dump, 315c7e2b968SJiri Pirko .init = tcf_vlan_init, 3164c5b9d96SManish Kurup .cleanup = tcf_vlan_cleanup, 317ddf97ccdSWANG Cong .walk = tcf_vlan_walker, 318ddf97ccdSWANG Cong .lookup = tcf_vlan_search, 319a85a970aSWANG Cong .size = sizeof(struct tcf_vlan), 320ddf97ccdSWANG Cong }; 321ddf97ccdSWANG Cong 322ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net) 323ddf97ccdSWANG Cong { 324ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 325ddf97ccdSWANG Cong 326c7e460ceSCong Wang return tc_action_net_init(tn, &act_vlan_ops); 327ddf97ccdSWANG Cong } 328ddf97ccdSWANG Cong 329039af9c6SCong Wang static void __net_exit vlan_exit_net(struct list_head *net_list) 330ddf97ccdSWANG Cong { 331039af9c6SCong Wang tc_action_net_exit(net_list, vlan_net_id); 332ddf97ccdSWANG Cong } 333ddf97ccdSWANG Cong 334ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = { 335ddf97ccdSWANG Cong .init = vlan_init_net, 336039af9c6SCong Wang .exit_batch = vlan_exit_net, 337ddf97ccdSWANG Cong .id = &vlan_net_id, 338ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 339c7e2b968SJiri Pirko }; 340c7e2b968SJiri Pirko 341c7e2b968SJiri Pirko static int __init vlan_init_module(void) 342c7e2b968SJiri Pirko { 343ddf97ccdSWANG Cong return tcf_register_action(&act_vlan_ops, &vlan_net_ops); 344c7e2b968SJiri Pirko } 345c7e2b968SJiri Pirko 346c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void) 347c7e2b968SJiri Pirko { 348ddf97ccdSWANG Cong tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); 349c7e2b968SJiri Pirko } 350c7e2b968SJiri Pirko 351c7e2b968SJiri Pirko module_init(vlan_init_module); 352c7e2b968SJiri Pirko module_exit(vlan_cleanup_module); 353c7e2b968SJiri Pirko 354c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 355c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions"); 356c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2"); 357