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 rcu_read_lock(); 444c5b9d96SManish Kurup 454c5b9d96SManish Kurup action = READ_ONCE(v->tcf_action); 464c5b9d96SManish Kurup 474c5b9d96SManish Kurup p = rcu_dereference(v->vlan_p); 484c5b9d96SManish Kurup 494c5b9d96SManish Kurup switch (p->tcfv_action) { 50c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 51c7e2b968SJiri Pirko err = skb_vlan_pop(skb); 52c7e2b968SJiri Pirko if (err) 53c7e2b968SJiri Pirko goto drop; 54c7e2b968SJiri Pirko break; 55c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 564c5b9d96SManish Kurup err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid | 574c5b9d96SManish Kurup (p->tcfv_push_prio << VLAN_PRIO_SHIFT)); 58c7e2b968SJiri Pirko if (err) 59c7e2b968SJiri Pirko goto drop; 60c7e2b968SJiri Pirko break; 6145a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 6245a497f2SShmulik Ladkani /* No-op if no vlan tag (either hw-accel or in-payload) */ 6345a497f2SShmulik Ladkani if (!skb_vlan_tagged(skb)) 6445a497f2SShmulik Ladkani goto unlock; 6545a497f2SShmulik Ladkani /* extract existing tag (and guarantee no hw-accel tag) */ 6645a497f2SShmulik Ladkani if (skb_vlan_tag_present(skb)) { 6745a497f2SShmulik Ladkani tci = skb_vlan_tag_get(skb); 6845a497f2SShmulik Ladkani skb->vlan_tci = 0; 6945a497f2SShmulik Ladkani } else { 7045a497f2SShmulik Ladkani /* in-payload vlan tag, pop it */ 7145a497f2SShmulik Ladkani err = __skb_vlan_pop(skb, &tci); 7245a497f2SShmulik Ladkani if (err) 7345a497f2SShmulik Ladkani goto drop; 7445a497f2SShmulik Ladkani } 7545a497f2SShmulik Ladkani /* replace the vid */ 764c5b9d96SManish Kurup tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid; 7745a497f2SShmulik Ladkani /* replace prio bits, if tcfv_push_prio specified */ 784c5b9d96SManish Kurup if (p->tcfv_push_prio) { 7945a497f2SShmulik Ladkani tci &= ~VLAN_PRIO_MASK; 804c5b9d96SManish Kurup tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT; 8145a497f2SShmulik Ladkani } 8245a497f2SShmulik Ladkani /* put updated tci as hwaccel tag */ 834c5b9d96SManish Kurup __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci); 8445a497f2SShmulik Ladkani break; 85c7e2b968SJiri Pirko default: 86c7e2b968SJiri Pirko BUG(); 87c7e2b968SJiri Pirko } 88c7e2b968SJiri Pirko 89c7e2b968SJiri Pirko goto unlock; 90c7e2b968SJiri Pirko 91c7e2b968SJiri Pirko drop: 92c7e2b968SJiri Pirko action = TC_ACT_SHOT; 93e0496cbbSManish Kurup qstats_drop_inc(this_cpu_ptr(v->common.cpu_qstats)); 94e0496cbbSManish Kurup 95c7e2b968SJiri Pirko unlock: 964c5b9d96SManish Kurup rcu_read_unlock(); 97f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 98f39acc84SShmulik Ladkani skb_pull_rcsum(skb, skb->mac_len); 99f39acc84SShmulik Ladkani 100c7e2b968SJiri Pirko return action; 101c7e2b968SJiri Pirko } 102c7e2b968SJiri Pirko 103c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { 104c7e2b968SJiri Pirko [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, 105c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, 106c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, 107956af371SHadar Hen Zion [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, 108c7e2b968SJiri Pirko }; 109c7e2b968SJiri Pirko 110c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla, 111a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 112589dad6dSAlexander Aring int ovr, int bind, struct netlink_ext_ack *extack) 113c7e2b968SJiri Pirko { 114ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 115c7e2b968SJiri Pirko struct nlattr *tb[TCA_VLAN_MAX + 1]; 1164c5b9d96SManish Kurup struct tcf_vlan_params *p, *p_old; 117c7e2b968SJiri Pirko struct tc_vlan *parm; 118c7e2b968SJiri Pirko struct tcf_vlan *v; 119c7e2b968SJiri Pirko int action; 120*94cb5492SDavide Caratti u16 push_vid = 0; 121c7e2b968SJiri Pirko __be16 push_proto = 0; 122956af371SHadar Hen Zion u8 push_prio = 0; 123b2313077SWANG Cong bool exists = false; 124b2313077SWANG Cong int ret = 0, err; 125c7e2b968SJiri Pirko 126c7e2b968SJiri Pirko if (!nla) 127c7e2b968SJiri Pirko return -EINVAL; 128c7e2b968SJiri Pirko 129fceb6435SJohannes Berg err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy, NULL); 130c7e2b968SJiri Pirko if (err < 0) 131c7e2b968SJiri Pirko return err; 132c7e2b968SJiri Pirko 133c7e2b968SJiri Pirko if (!tb[TCA_VLAN_PARMS]) 134c7e2b968SJiri Pirko return -EINVAL; 135c7e2b968SJiri Pirko parm = nla_data(tb[TCA_VLAN_PARMS]); 13665a206c0SChris Mi exists = tcf_idr_check(tn, parm->index, a, bind); 1375026c9b1SJamal Hadi Salim if (exists && bind) 1385026c9b1SJamal Hadi Salim return 0; 1395026c9b1SJamal Hadi Salim 140c7e2b968SJiri Pirko switch (parm->v_action) { 141c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 142c7e2b968SJiri Pirko break; 143c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 14445a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 1455026c9b1SJamal Hadi Salim if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { 1465026c9b1SJamal Hadi Salim if (exists) 14765a206c0SChris Mi tcf_idr_release(*a, bind); 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); 154c7e2b968SJiri Pirko return -ERANGE; 1555026c9b1SJamal Hadi Salim } 156c7e2b968SJiri Pirko 157c7e2b968SJiri Pirko if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { 158c7e2b968SJiri Pirko push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); 159c7e2b968SJiri Pirko switch (push_proto) { 160c7e2b968SJiri Pirko case htons(ETH_P_8021Q): 161c7e2b968SJiri Pirko case htons(ETH_P_8021AD): 162c7e2b968SJiri Pirko break; 163c7e2b968SJiri Pirko default: 164c7e2b968SJiri Pirko return -EPROTONOSUPPORT; 165c7e2b968SJiri Pirko } 166c7e2b968SJiri Pirko } else { 167c7e2b968SJiri Pirko push_proto = htons(ETH_P_8021Q); 168c7e2b968SJiri Pirko } 169956af371SHadar Hen Zion 170956af371SHadar Hen Zion if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) 171956af371SHadar Hen Zion push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); 172c7e2b968SJiri Pirko break; 173c7e2b968SJiri Pirko default: 1745026c9b1SJamal Hadi Salim if (exists) 17565a206c0SChris Mi tcf_idr_release(*a, bind); 176c7e2b968SJiri Pirko return -EINVAL; 177c7e2b968SJiri Pirko } 178c7e2b968SJiri Pirko action = parm->v_action; 179c7e2b968SJiri Pirko 1805026c9b1SJamal Hadi Salim if (!exists) { 18165a206c0SChris Mi ret = tcf_idr_create(tn, parm->index, est, a, 182e0496cbbSManish Kurup &act_vlan_ops, bind, true); 183c7e2b968SJiri Pirko if (ret) 184c7e2b968SJiri Pirko return ret; 185c7e2b968SJiri Pirko 186c7e2b968SJiri Pirko ret = ACT_P_CREATED; 187c7e2b968SJiri Pirko } else { 18865a206c0SChris Mi tcf_idr_release(*a, bind); 189c7e2b968SJiri Pirko if (!ovr) 190c7e2b968SJiri Pirko return -EEXIST; 191c7e2b968SJiri Pirko } 192c7e2b968SJiri Pirko 193a85a970aSWANG Cong v = to_vlan(*a); 194c7e2b968SJiri Pirko 1954c5b9d96SManish Kurup ASSERT_RTNL(); 1964c5b9d96SManish Kurup p = kzalloc(sizeof(*p), GFP_KERNEL); 1974c5b9d96SManish Kurup if (!p) { 198d7f20015SDavide Caratti if (ret == ACT_P_CREATED) 1994c5b9d96SManish Kurup tcf_idr_release(*a, bind); 2004c5b9d96SManish Kurup return -ENOMEM; 2014c5b9d96SManish Kurup } 202c7e2b968SJiri Pirko 203c7e2b968SJiri Pirko v->tcf_action = parm->action; 204c7e2b968SJiri Pirko 2054c5b9d96SManish Kurup p_old = rtnl_dereference(v->vlan_p); 2064c5b9d96SManish Kurup 2074c5b9d96SManish Kurup p->tcfv_action = action; 2084c5b9d96SManish Kurup p->tcfv_push_vid = push_vid; 2094c5b9d96SManish Kurup p->tcfv_push_prio = push_prio; 2104c5b9d96SManish Kurup p->tcfv_push_proto = push_proto; 2114c5b9d96SManish Kurup 2124c5b9d96SManish Kurup rcu_assign_pointer(v->vlan_p, p); 2134c5b9d96SManish Kurup 2144c5b9d96SManish Kurup if (p_old) 2154c5b9d96SManish Kurup kfree_rcu(p_old, rcu); 216c7e2b968SJiri Pirko 217c7e2b968SJiri Pirko if (ret == ACT_P_CREATED) 21865a206c0SChris Mi tcf_idr_insert(tn, *a); 219c7e2b968SJiri Pirko return ret; 220c7e2b968SJiri Pirko } 221c7e2b968SJiri Pirko 2229a63b255SCong Wang static void tcf_vlan_cleanup(struct tc_action *a) 2234c5b9d96SManish Kurup { 2244c5b9d96SManish Kurup struct tcf_vlan *v = to_vlan(a); 2254c5b9d96SManish Kurup struct tcf_vlan_params *p; 2264c5b9d96SManish Kurup 2274c5b9d96SManish Kurup p = rcu_dereference_protected(v->vlan_p, 1); 2281edf8abeSDavide Caratti if (p) 2294c5b9d96SManish Kurup kfree_rcu(p, rcu); 2304c5b9d96SManish Kurup } 2314c5b9d96SManish Kurup 232c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, 233c7e2b968SJiri Pirko int bind, int ref) 234c7e2b968SJiri Pirko { 235c7e2b968SJiri Pirko unsigned char *b = skb_tail_pointer(skb); 236a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 2374c5b9d96SManish Kurup struct tcf_vlan_params *p = rtnl_dereference(v->vlan_p); 238c7e2b968SJiri Pirko struct tc_vlan opt = { 239c7e2b968SJiri Pirko .index = v->tcf_index, 240c7e2b968SJiri Pirko .refcnt = v->tcf_refcnt - ref, 241c7e2b968SJiri Pirko .bindcnt = v->tcf_bindcnt - bind, 242c7e2b968SJiri Pirko .action = v->tcf_action, 2434c5b9d96SManish Kurup .v_action = p->tcfv_action, 244c7e2b968SJiri Pirko }; 245c7e2b968SJiri Pirko struct tcf_t t; 246c7e2b968SJiri Pirko 247c7e2b968SJiri Pirko if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) 248c7e2b968SJiri Pirko goto nla_put_failure; 249c7e2b968SJiri Pirko 2504c5b9d96SManish Kurup if ((p->tcfv_action == TCA_VLAN_ACT_PUSH || 2514c5b9d96SManish Kurup p->tcfv_action == TCA_VLAN_ACT_MODIFY) && 2524c5b9d96SManish Kurup (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || 2530b0f43feSJamal Hadi Salim nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, 2544c5b9d96SManish Kurup p->tcfv_push_proto) || 255956af371SHadar Hen Zion (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, 2564c5b9d96SManish Kurup p->tcfv_push_prio)))) 257c7e2b968SJiri Pirko goto nla_put_failure; 258c7e2b968SJiri Pirko 25948d8ee16SJamal Hadi Salim tcf_tm_dump(&t, &v->tcf_tm); 2609854518eSNicolas Dichtel if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) 261c7e2b968SJiri Pirko goto nla_put_failure; 262c7e2b968SJiri Pirko return skb->len; 263c7e2b968SJiri Pirko 264c7e2b968SJiri Pirko nla_put_failure: 265c7e2b968SJiri Pirko nlmsg_trim(skb, b); 266c7e2b968SJiri Pirko return -1; 267c7e2b968SJiri Pirko } 268c7e2b968SJiri Pirko 269ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb, 270ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 27141780105SAlexander Aring const struct tc_action_ops *ops, 27241780105SAlexander Aring struct netlink_ext_ack *extack) 273ddf97ccdSWANG Cong { 274ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 275ddf97ccdSWANG Cong 276b3620145SAlexander Aring return tcf_generic_walker(tn, skb, cb, type, ops, extack); 277ddf97ccdSWANG Cong } 278ddf97ccdSWANG Cong 279331a9295SAlexander Aring static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index, 280331a9295SAlexander Aring struct netlink_ext_ack *extack) 281ddf97ccdSWANG Cong { 282ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 283ddf97ccdSWANG Cong 28465a206c0SChris Mi return tcf_idr_search(tn, a, index); 285ddf97ccdSWANG Cong } 286ddf97ccdSWANG Cong 287c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = { 288c7e2b968SJiri Pirko .kind = "vlan", 289c7e2b968SJiri Pirko .type = TCA_ACT_VLAN, 290c7e2b968SJiri Pirko .owner = THIS_MODULE, 291c7e2b968SJiri Pirko .act = tcf_vlan, 292c7e2b968SJiri Pirko .dump = tcf_vlan_dump, 293c7e2b968SJiri Pirko .init = tcf_vlan_init, 2944c5b9d96SManish Kurup .cleanup = tcf_vlan_cleanup, 295ddf97ccdSWANG Cong .walk = tcf_vlan_walker, 296ddf97ccdSWANG Cong .lookup = tcf_vlan_search, 297a85a970aSWANG Cong .size = sizeof(struct tcf_vlan), 298ddf97ccdSWANG Cong }; 299ddf97ccdSWANG Cong 300ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net) 301ddf97ccdSWANG Cong { 302ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 303ddf97ccdSWANG Cong 304c7e460ceSCong Wang return tc_action_net_init(tn, &act_vlan_ops); 305ddf97ccdSWANG Cong } 306ddf97ccdSWANG Cong 307039af9c6SCong Wang static void __net_exit vlan_exit_net(struct list_head *net_list) 308ddf97ccdSWANG Cong { 309039af9c6SCong Wang tc_action_net_exit(net_list, vlan_net_id); 310ddf97ccdSWANG Cong } 311ddf97ccdSWANG Cong 312ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = { 313ddf97ccdSWANG Cong .init = vlan_init_net, 314039af9c6SCong Wang .exit_batch = vlan_exit_net, 315ddf97ccdSWANG Cong .id = &vlan_net_id, 316ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 317685ecfb1SKirill Tkhai .async = true, 318c7e2b968SJiri Pirko }; 319c7e2b968SJiri Pirko 320c7e2b968SJiri Pirko static int __init vlan_init_module(void) 321c7e2b968SJiri Pirko { 322ddf97ccdSWANG Cong return tcf_register_action(&act_vlan_ops, &vlan_net_ops); 323c7e2b968SJiri Pirko } 324c7e2b968SJiri Pirko 325c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void) 326c7e2b968SJiri Pirko { 327ddf97ccdSWANG Cong tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); 328c7e2b968SJiri Pirko } 329c7e2b968SJiri Pirko 330c7e2b968SJiri Pirko module_init(vlan_init_module); 331c7e2b968SJiri Pirko module_exit(vlan_cleanup_module); 332c7e2b968SJiri Pirko 333c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 334c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions"); 335c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2"); 336