1c7e2b968SJiri Pirko /* 2c7e2b968SJiri Pirko * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> 3c7e2b968SJiri Pirko * 4c7e2b968SJiri Pirko * This program is free software; you can redistribute it and/or modify 5c7e2b968SJiri Pirko * it under the terms of the GNU General Public License as published by 6c7e2b968SJiri Pirko * the Free Software Foundation; either version 2 of the License, or 7c7e2b968SJiri Pirko * (at your option) any later version. 8c7e2b968SJiri Pirko */ 9c7e2b968SJiri Pirko 10c7e2b968SJiri Pirko #include <linux/module.h> 11c7e2b968SJiri Pirko #include <linux/init.h> 12c7e2b968SJiri Pirko #include <linux/kernel.h> 13c7e2b968SJiri Pirko #include <linux/skbuff.h> 14c7e2b968SJiri Pirko #include <linux/rtnetlink.h> 15c7e2b968SJiri Pirko #include <linux/if_vlan.h> 16c7e2b968SJiri Pirko #include <net/netlink.h> 17c7e2b968SJiri Pirko #include <net/pkt_sched.h> 18c7e2b968SJiri Pirko 19c7e2b968SJiri Pirko #include <linux/tc_act/tc_vlan.h> 20c7e2b968SJiri Pirko #include <net/tc_act/tc_vlan.h> 21c7e2b968SJiri Pirko 22c7d03a00SAlexey Dobriyan static unsigned int vlan_net_id; 23a85a970aSWANG Cong static struct tc_action_ops act_vlan_ops; 24ddf97ccdSWANG Cong 25c7e2b968SJiri Pirko static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a, 26c7e2b968SJiri Pirko struct tcf_result *res) 27c7e2b968SJiri Pirko { 28a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 294c5b9d96SManish Kurup struct tcf_vlan_params *p; 30c7e2b968SJiri Pirko int action; 31c7e2b968SJiri Pirko int err; 3245a497f2SShmulik Ladkani u16 tci; 33c7e2b968SJiri Pirko 349c4a4e48SJamal Hadi Salim tcf_lastuse_update(&v->tcf_tm); 35e0496cbbSManish Kurup bstats_cpu_update(this_cpu_ptr(v->common.cpu_bstats), skb); 36e0496cbbSManish Kurup 37f39acc84SShmulik Ladkani /* Ensure 'data' points at mac_header prior calling vlan manipulating 38f39acc84SShmulik Ladkani * functions. 39f39acc84SShmulik Ladkani */ 40f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 41f39acc84SShmulik Ladkani skb_push_rcsum(skb, skb->mac_len); 42f39acc84SShmulik Ladkani 434c5b9d96SManish Kurup action = READ_ONCE(v->tcf_action); 444c5b9d96SManish Kurup 457fd4b288SPaolo Abeni p = rcu_dereference_bh(v->vlan_p); 464c5b9d96SManish Kurup 474c5b9d96SManish Kurup switch (p->tcfv_action) { 48c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 49c7e2b968SJiri Pirko err = skb_vlan_pop(skb); 50c7e2b968SJiri Pirko if (err) 51c7e2b968SJiri Pirko goto drop; 52c7e2b968SJiri Pirko break; 53c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 544c5b9d96SManish Kurup err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid | 554c5b9d96SManish Kurup (p->tcfv_push_prio << VLAN_PRIO_SHIFT)); 56c7e2b968SJiri Pirko if (err) 57c7e2b968SJiri Pirko goto drop; 58c7e2b968SJiri Pirko break; 5945a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 6045a497f2SShmulik Ladkani /* No-op if no vlan tag (either hw-accel or in-payload) */ 6145a497f2SShmulik Ladkani if (!skb_vlan_tagged(skb)) 627fd4b288SPaolo Abeni goto out; 6345a497f2SShmulik Ladkani /* extract existing tag (and guarantee no hw-accel tag) */ 6445a497f2SShmulik Ladkani if (skb_vlan_tag_present(skb)) { 6545a497f2SShmulik Ladkani tci = skb_vlan_tag_get(skb); 6645a497f2SShmulik Ladkani skb->vlan_tci = 0; 6745a497f2SShmulik Ladkani } else { 6845a497f2SShmulik Ladkani /* in-payload vlan tag, pop it */ 6945a497f2SShmulik Ladkani err = __skb_vlan_pop(skb, &tci); 7045a497f2SShmulik Ladkani if (err) 7145a497f2SShmulik Ladkani goto drop; 7245a497f2SShmulik Ladkani } 7345a497f2SShmulik Ladkani /* replace the vid */ 744c5b9d96SManish Kurup tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid; 7545a497f2SShmulik Ladkani /* replace prio bits, if tcfv_push_prio specified */ 764c5b9d96SManish Kurup if (p->tcfv_push_prio) { 7745a497f2SShmulik Ladkani tci &= ~VLAN_PRIO_MASK; 784c5b9d96SManish Kurup tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT; 7945a497f2SShmulik Ladkani } 8045a497f2SShmulik Ladkani /* put updated tci as hwaccel tag */ 814c5b9d96SManish Kurup __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci); 8245a497f2SShmulik Ladkani break; 83c7e2b968SJiri Pirko default: 84c7e2b968SJiri Pirko BUG(); 85c7e2b968SJiri Pirko } 86c7e2b968SJiri Pirko 877fd4b288SPaolo Abeni out: 88f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 89f39acc84SShmulik Ladkani skb_pull_rcsum(skb, skb->mac_len); 90f39acc84SShmulik Ladkani 91c7e2b968SJiri Pirko return action; 927fd4b288SPaolo Abeni 937fd4b288SPaolo Abeni drop: 947fd4b288SPaolo Abeni qstats_drop_inc(this_cpu_ptr(v->common.cpu_qstats)); 957fd4b288SPaolo Abeni return TC_ACT_SHOT; 96c7e2b968SJiri Pirko } 97c7e2b968SJiri Pirko 98c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { 99c7e2b968SJiri Pirko [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, 100c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, 101c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, 102956af371SHadar Hen Zion [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, 103c7e2b968SJiri Pirko }; 104c7e2b968SJiri Pirko 105c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla, 106a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 107789871bbSVlad Buslov int ovr, int bind, bool rtnl_held, 108789871bbSVlad Buslov struct netlink_ext_ack *extack) 109c7e2b968SJiri Pirko { 110ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 111c7e2b968SJiri Pirko struct nlattr *tb[TCA_VLAN_MAX + 1]; 112*764e9a24SVlad Buslov struct tcf_vlan_params *p; 113c7e2b968SJiri Pirko struct tc_vlan *parm; 114c7e2b968SJiri Pirko struct tcf_vlan *v; 115c7e2b968SJiri Pirko int action; 11694cb5492SDavide Caratti u16 push_vid = 0; 117c7e2b968SJiri Pirko __be16 push_proto = 0; 118956af371SHadar Hen Zion u8 push_prio = 0; 119b2313077SWANG Cong bool exists = false; 120b2313077SWANG Cong int ret = 0, err; 121c7e2b968SJiri Pirko 122c7e2b968SJiri Pirko if (!nla) 123c7e2b968SJiri Pirko return -EINVAL; 124c7e2b968SJiri Pirko 125fceb6435SJohannes Berg err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy, NULL); 126c7e2b968SJiri Pirko if (err < 0) 127c7e2b968SJiri Pirko return err; 128c7e2b968SJiri Pirko 129c7e2b968SJiri Pirko if (!tb[TCA_VLAN_PARMS]) 130c7e2b968SJiri Pirko return -EINVAL; 131c7e2b968SJiri Pirko parm = nla_data(tb[TCA_VLAN_PARMS]); 1320190c1d4SVlad Buslov err = tcf_idr_check_alloc(tn, &parm->index, a, bind); 1330190c1d4SVlad Buslov if (err < 0) 1340190c1d4SVlad Buslov return err; 1350190c1d4SVlad Buslov exists = err; 1365026c9b1SJamal Hadi Salim if (exists && bind) 1375026c9b1SJamal Hadi Salim return 0; 1385026c9b1SJamal Hadi Salim 139c7e2b968SJiri Pirko switch (parm->v_action) { 140c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 141c7e2b968SJiri Pirko break; 142c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 14345a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 1445026c9b1SJamal Hadi Salim if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { 1455026c9b1SJamal Hadi Salim if (exists) 14665a206c0SChris Mi tcf_idr_release(*a, bind); 1470190c1d4SVlad Buslov else 1480190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 149c7e2b968SJiri Pirko return -EINVAL; 1505026c9b1SJamal Hadi Salim } 151c7e2b968SJiri Pirko push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); 1525026c9b1SJamal Hadi Salim if (push_vid >= VLAN_VID_MASK) { 1535026c9b1SJamal Hadi Salim if (exists) 15465a206c0SChris Mi tcf_idr_release(*a, bind); 1550190c1d4SVlad Buslov else 1560190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 157c7e2b968SJiri Pirko return -ERANGE; 1585026c9b1SJamal Hadi Salim } 159c7e2b968SJiri Pirko 160c7e2b968SJiri Pirko if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { 161c7e2b968SJiri Pirko push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); 162c7e2b968SJiri Pirko switch (push_proto) { 163c7e2b968SJiri Pirko case htons(ETH_P_8021Q): 164c7e2b968SJiri Pirko case htons(ETH_P_8021AD): 165c7e2b968SJiri Pirko break; 166c7e2b968SJiri Pirko default: 1675a4931aeSDavide Caratti if (exists) 1685a4931aeSDavide Caratti tcf_idr_release(*a, bind); 1690190c1d4SVlad Buslov else 1700190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 171c7e2b968SJiri Pirko return -EPROTONOSUPPORT; 172c7e2b968SJiri Pirko } 173c7e2b968SJiri Pirko } else { 174c7e2b968SJiri Pirko push_proto = htons(ETH_P_8021Q); 175c7e2b968SJiri Pirko } 176956af371SHadar Hen Zion 177956af371SHadar Hen Zion if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) 178956af371SHadar Hen Zion push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); 179c7e2b968SJiri Pirko break; 180c7e2b968SJiri Pirko default: 1815026c9b1SJamal Hadi Salim if (exists) 18265a206c0SChris Mi tcf_idr_release(*a, bind); 1830190c1d4SVlad Buslov else 1840190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 185c7e2b968SJiri Pirko return -EINVAL; 186c7e2b968SJiri Pirko } 187c7e2b968SJiri Pirko action = parm->v_action; 188c7e2b968SJiri Pirko 1895026c9b1SJamal Hadi Salim if (!exists) { 19065a206c0SChris Mi ret = tcf_idr_create(tn, parm->index, est, a, 191e0496cbbSManish Kurup &act_vlan_ops, bind, true); 1920190c1d4SVlad Buslov if (ret) { 1930190c1d4SVlad Buslov tcf_idr_cleanup(tn, parm->index); 194c7e2b968SJiri Pirko return ret; 1950190c1d4SVlad Buslov } 196c7e2b968SJiri Pirko 197c7e2b968SJiri Pirko ret = ACT_P_CREATED; 1984e8ddd7fSVlad Buslov } else if (!ovr) { 19965a206c0SChris Mi tcf_idr_release(*a, bind); 200c7e2b968SJiri Pirko return -EEXIST; 201c7e2b968SJiri Pirko } 202c7e2b968SJiri Pirko 203a85a970aSWANG Cong v = to_vlan(*a); 204c7e2b968SJiri Pirko 2054c5b9d96SManish Kurup p = kzalloc(sizeof(*p), GFP_KERNEL); 2064c5b9d96SManish Kurup if (!p) { 2074c5b9d96SManish Kurup tcf_idr_release(*a, bind); 2084c5b9d96SManish Kurup return -ENOMEM; 2094c5b9d96SManish Kurup } 210c7e2b968SJiri Pirko 2114c5b9d96SManish Kurup p->tcfv_action = action; 2124c5b9d96SManish Kurup p->tcfv_push_vid = push_vid; 2134c5b9d96SManish Kurup p->tcfv_push_prio = push_prio; 2144c5b9d96SManish Kurup p->tcfv_push_proto = push_proto; 2154c5b9d96SManish Kurup 216*764e9a24SVlad Buslov spin_lock(&v->tcf_lock); 217*764e9a24SVlad Buslov v->tcf_action = parm->action; 218*764e9a24SVlad Buslov rcu_swap_protected(v->vlan_p, p, lockdep_is_held(&v->tcf_lock)); 219*764e9a24SVlad Buslov spin_unlock(&v->tcf_lock); 2204c5b9d96SManish Kurup 221*764e9a24SVlad Buslov if (p) 222*764e9a24SVlad Buslov kfree_rcu(p, rcu); 223c7e2b968SJiri Pirko 224c7e2b968SJiri Pirko if (ret == ACT_P_CREATED) 22565a206c0SChris Mi tcf_idr_insert(tn, *a); 226c7e2b968SJiri Pirko return ret; 227c7e2b968SJiri Pirko } 228c7e2b968SJiri Pirko 2299a63b255SCong Wang static void tcf_vlan_cleanup(struct tc_action *a) 2304c5b9d96SManish Kurup { 2314c5b9d96SManish Kurup struct tcf_vlan *v = to_vlan(a); 2324c5b9d96SManish Kurup struct tcf_vlan_params *p; 2334c5b9d96SManish Kurup 2344c5b9d96SManish Kurup p = rcu_dereference_protected(v->vlan_p, 1); 2351edf8abeSDavide Caratti if (p) 2364c5b9d96SManish Kurup kfree_rcu(p, rcu); 2374c5b9d96SManish Kurup } 2384c5b9d96SManish Kurup 239c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, 240c7e2b968SJiri Pirko int bind, int ref) 241c7e2b968SJiri Pirko { 242c7e2b968SJiri Pirko unsigned char *b = skb_tail_pointer(skb); 243a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 244*764e9a24SVlad Buslov struct tcf_vlan_params *p; 245c7e2b968SJiri Pirko struct tc_vlan opt = { 246c7e2b968SJiri Pirko .index = v->tcf_index, 247036bb443SVlad Buslov .refcnt = refcount_read(&v->tcf_refcnt) - ref, 248036bb443SVlad Buslov .bindcnt = atomic_read(&v->tcf_bindcnt) - bind, 249c7e2b968SJiri Pirko }; 250c7e2b968SJiri Pirko struct tcf_t t; 251c7e2b968SJiri Pirko 252*764e9a24SVlad Buslov spin_lock(&v->tcf_lock); 253*764e9a24SVlad Buslov opt.action = v->tcf_action; 254*764e9a24SVlad Buslov p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock)); 255*764e9a24SVlad Buslov opt.v_action = p->tcfv_action; 256c7e2b968SJiri Pirko if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) 257c7e2b968SJiri Pirko goto nla_put_failure; 258c7e2b968SJiri Pirko 2594c5b9d96SManish Kurup if ((p->tcfv_action == TCA_VLAN_ACT_PUSH || 2604c5b9d96SManish Kurup p->tcfv_action == TCA_VLAN_ACT_MODIFY) && 2614c5b9d96SManish Kurup (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || 2620b0f43feSJamal Hadi Salim nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, 2634c5b9d96SManish Kurup p->tcfv_push_proto) || 264956af371SHadar Hen Zion (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, 2654c5b9d96SManish Kurup p->tcfv_push_prio)))) 266c7e2b968SJiri Pirko goto nla_put_failure; 267c7e2b968SJiri Pirko 26848d8ee16SJamal Hadi Salim tcf_tm_dump(&t, &v->tcf_tm); 2699854518eSNicolas Dichtel if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) 270c7e2b968SJiri Pirko goto nla_put_failure; 271*764e9a24SVlad Buslov spin_unlock(&v->tcf_lock); 272*764e9a24SVlad Buslov 273c7e2b968SJiri Pirko return skb->len; 274c7e2b968SJiri Pirko 275c7e2b968SJiri Pirko nla_put_failure: 276*764e9a24SVlad Buslov spin_unlock(&v->tcf_lock); 277c7e2b968SJiri Pirko nlmsg_trim(skb, b); 278c7e2b968SJiri Pirko return -1; 279c7e2b968SJiri Pirko } 280c7e2b968SJiri Pirko 281ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb, 282ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 28341780105SAlexander Aring const struct tc_action_ops *ops, 28441780105SAlexander Aring struct netlink_ext_ack *extack) 285ddf97ccdSWANG Cong { 286ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 287ddf97ccdSWANG Cong 288b3620145SAlexander Aring return tcf_generic_walker(tn, skb, cb, type, ops, extack); 289ddf97ccdSWANG Cong } 290ddf97ccdSWANG Cong 291331a9295SAlexander Aring static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index, 292331a9295SAlexander Aring struct netlink_ext_ack *extack) 293ddf97ccdSWANG Cong { 294ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 295ddf97ccdSWANG Cong 29665a206c0SChris Mi return tcf_idr_search(tn, a, index); 297ddf97ccdSWANG Cong } 298ddf97ccdSWANG Cong 299b409074eSVlad Buslov static int tcf_vlan_delete(struct net *net, u32 index) 300b409074eSVlad Buslov { 301b409074eSVlad Buslov struct tc_action_net *tn = net_generic(net, vlan_net_id); 302b409074eSVlad Buslov 303b409074eSVlad Buslov return tcf_idr_delete_index(tn, index); 304b409074eSVlad Buslov } 305b409074eSVlad Buslov 306c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = { 307c7e2b968SJiri Pirko .kind = "vlan", 308c7e2b968SJiri Pirko .type = TCA_ACT_VLAN, 309c7e2b968SJiri Pirko .owner = THIS_MODULE, 310c7e2b968SJiri Pirko .act = tcf_vlan, 311c7e2b968SJiri Pirko .dump = tcf_vlan_dump, 312c7e2b968SJiri Pirko .init = tcf_vlan_init, 3134c5b9d96SManish Kurup .cleanup = tcf_vlan_cleanup, 314ddf97ccdSWANG Cong .walk = tcf_vlan_walker, 315ddf97ccdSWANG Cong .lookup = tcf_vlan_search, 316b409074eSVlad Buslov .delete = tcf_vlan_delete, 317a85a970aSWANG Cong .size = sizeof(struct tcf_vlan), 318ddf97ccdSWANG Cong }; 319ddf97ccdSWANG Cong 320ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net) 321ddf97ccdSWANG Cong { 322ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 323ddf97ccdSWANG Cong 324c7e460ceSCong Wang return tc_action_net_init(tn, &act_vlan_ops); 325ddf97ccdSWANG Cong } 326ddf97ccdSWANG Cong 327039af9c6SCong Wang static void __net_exit vlan_exit_net(struct list_head *net_list) 328ddf97ccdSWANG Cong { 329039af9c6SCong Wang tc_action_net_exit(net_list, vlan_net_id); 330ddf97ccdSWANG Cong } 331ddf97ccdSWANG Cong 332ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = { 333ddf97ccdSWANG Cong .init = vlan_init_net, 334039af9c6SCong Wang .exit_batch = vlan_exit_net, 335ddf97ccdSWANG Cong .id = &vlan_net_id, 336ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 337c7e2b968SJiri Pirko }; 338c7e2b968SJiri Pirko 339c7e2b968SJiri Pirko static int __init vlan_init_module(void) 340c7e2b968SJiri Pirko { 341ddf97ccdSWANG Cong return tcf_register_action(&act_vlan_ops, &vlan_net_ops); 342c7e2b968SJiri Pirko } 343c7e2b968SJiri Pirko 344c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void) 345c7e2b968SJiri Pirko { 346ddf97ccdSWANG Cong tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); 347c7e2b968SJiri Pirko } 348c7e2b968SJiri Pirko 349c7e2b968SJiri Pirko module_init(vlan_init_module); 350c7e2b968SJiri Pirko module_exit(vlan_cleanup_module); 351c7e2b968SJiri Pirko 352c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 353c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions"); 354c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2"); 355