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); 29c7e2b968SJiri Pirko int action; 30c7e2b968SJiri Pirko int err; 3145a497f2SShmulik Ladkani u16 tci; 32c7e2b968SJiri Pirko 33c7e2b968SJiri Pirko spin_lock(&v->tcf_lock); 349c4a4e48SJamal Hadi Salim tcf_lastuse_update(&v->tcf_tm); 35c7e2b968SJiri Pirko bstats_update(&v->tcf_bstats, skb); 36c7e2b968SJiri Pirko action = v->tcf_action; 37c7e2b968SJiri Pirko 38f39acc84SShmulik Ladkani /* Ensure 'data' points at mac_header prior calling vlan manipulating 39f39acc84SShmulik Ladkani * functions. 40f39acc84SShmulik Ladkani */ 41f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 42f39acc84SShmulik Ladkani skb_push_rcsum(skb, skb->mac_len); 43f39acc84SShmulik Ladkani 44c7e2b968SJiri Pirko switch (v->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: 51956af371SHadar Hen Zion err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid | 52956af371SHadar Hen Zion (v->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)) 5945a497f2SShmulik Ladkani goto unlock; 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); 6345a497f2SShmulik Ladkani skb->vlan_tci = 0; 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 */ 7145a497f2SShmulik Ladkani tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid; 7245a497f2SShmulik Ladkani /* replace prio bits, if tcfv_push_prio specified */ 7345a497f2SShmulik Ladkani if (v->tcfv_push_prio) { 7445a497f2SShmulik Ladkani tci &= ~VLAN_PRIO_MASK; 7545a497f2SShmulik Ladkani tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT; 7645a497f2SShmulik Ladkani } 7745a497f2SShmulik Ladkani /* put updated tci as hwaccel tag */ 7845a497f2SShmulik Ladkani __vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci); 7945a497f2SShmulik Ladkani break; 80c7e2b968SJiri Pirko default: 81c7e2b968SJiri Pirko BUG(); 82c7e2b968SJiri Pirko } 83c7e2b968SJiri Pirko 84c7e2b968SJiri Pirko goto unlock; 85c7e2b968SJiri Pirko 86c7e2b968SJiri Pirko drop: 87c7e2b968SJiri Pirko action = TC_ACT_SHOT; 88c7e2b968SJiri Pirko v->tcf_qstats.drops++; 89c7e2b968SJiri Pirko unlock: 90f39acc84SShmulik Ladkani if (skb_at_tc_ingress(skb)) 91f39acc84SShmulik Ladkani skb_pull_rcsum(skb, skb->mac_len); 92f39acc84SShmulik Ladkani 93c7e2b968SJiri Pirko spin_unlock(&v->tcf_lock); 94c7e2b968SJiri Pirko return action; 95c7e2b968SJiri Pirko } 96c7e2b968SJiri Pirko 97c7e2b968SJiri Pirko static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { 98c7e2b968SJiri Pirko [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, 99c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, 100c7e2b968SJiri Pirko [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, 101956af371SHadar Hen Zion [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, 102c7e2b968SJiri Pirko }; 103c7e2b968SJiri Pirko 104c7e2b968SJiri Pirko static int tcf_vlan_init(struct net *net, struct nlattr *nla, 105a85a970aSWANG Cong struct nlattr *est, struct tc_action **a, 106c7e2b968SJiri Pirko int ovr, int bind) 107c7e2b968SJiri Pirko { 108ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 109c7e2b968SJiri Pirko struct nlattr *tb[TCA_VLAN_MAX + 1]; 110c7e2b968SJiri Pirko struct tc_vlan *parm; 111c7e2b968SJiri Pirko struct tcf_vlan *v; 112c7e2b968SJiri Pirko int action; 113c7e2b968SJiri Pirko __be16 push_vid = 0; 114c7e2b968SJiri Pirko __be16 push_proto = 0; 115956af371SHadar Hen Zion u8 push_prio = 0; 116b2313077SWANG Cong bool exists = false; 117b2313077SWANG Cong int ret = 0, err; 118c7e2b968SJiri Pirko 119c7e2b968SJiri Pirko if (!nla) 120c7e2b968SJiri Pirko return -EINVAL; 121c7e2b968SJiri Pirko 122fceb6435SJohannes Berg err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy, NULL); 123c7e2b968SJiri Pirko if (err < 0) 124c7e2b968SJiri Pirko return err; 125c7e2b968SJiri Pirko 126c7e2b968SJiri Pirko if (!tb[TCA_VLAN_PARMS]) 127c7e2b968SJiri Pirko return -EINVAL; 128c7e2b968SJiri Pirko parm = nla_data(tb[TCA_VLAN_PARMS]); 129*65a206c0SChris Mi exists = tcf_idr_check(tn, parm->index, a, bind); 1305026c9b1SJamal Hadi Salim if (exists && bind) 1315026c9b1SJamal Hadi Salim return 0; 1325026c9b1SJamal Hadi Salim 133c7e2b968SJiri Pirko switch (parm->v_action) { 134c7e2b968SJiri Pirko case TCA_VLAN_ACT_POP: 135c7e2b968SJiri Pirko break; 136c7e2b968SJiri Pirko case TCA_VLAN_ACT_PUSH: 13745a497f2SShmulik Ladkani case TCA_VLAN_ACT_MODIFY: 1385026c9b1SJamal Hadi Salim if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { 1395026c9b1SJamal Hadi Salim if (exists) 140*65a206c0SChris Mi tcf_idr_release(*a, bind); 141c7e2b968SJiri Pirko return -EINVAL; 1425026c9b1SJamal Hadi Salim } 143c7e2b968SJiri Pirko push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); 1445026c9b1SJamal Hadi Salim if (push_vid >= VLAN_VID_MASK) { 1455026c9b1SJamal Hadi Salim if (exists) 146*65a206c0SChris Mi tcf_idr_release(*a, bind); 147c7e2b968SJiri Pirko return -ERANGE; 1485026c9b1SJamal Hadi Salim } 149c7e2b968SJiri Pirko 150c7e2b968SJiri Pirko if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { 151c7e2b968SJiri Pirko push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); 152c7e2b968SJiri Pirko switch (push_proto) { 153c7e2b968SJiri Pirko case htons(ETH_P_8021Q): 154c7e2b968SJiri Pirko case htons(ETH_P_8021AD): 155c7e2b968SJiri Pirko break; 156c7e2b968SJiri Pirko default: 157c7e2b968SJiri Pirko return -EPROTONOSUPPORT; 158c7e2b968SJiri Pirko } 159c7e2b968SJiri Pirko } else { 160c7e2b968SJiri Pirko push_proto = htons(ETH_P_8021Q); 161c7e2b968SJiri Pirko } 162956af371SHadar Hen Zion 163956af371SHadar Hen Zion if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) 164956af371SHadar Hen Zion push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); 165c7e2b968SJiri Pirko break; 166c7e2b968SJiri Pirko default: 1675026c9b1SJamal Hadi Salim if (exists) 168*65a206c0SChris Mi tcf_idr_release(*a, bind); 169c7e2b968SJiri Pirko return -EINVAL; 170c7e2b968SJiri Pirko } 171c7e2b968SJiri Pirko action = parm->v_action; 172c7e2b968SJiri Pirko 1735026c9b1SJamal Hadi Salim if (!exists) { 174*65a206c0SChris Mi ret = tcf_idr_create(tn, parm->index, est, a, 175a85a970aSWANG Cong &act_vlan_ops, bind, false); 176c7e2b968SJiri Pirko if (ret) 177c7e2b968SJiri Pirko return ret; 178c7e2b968SJiri Pirko 179c7e2b968SJiri Pirko ret = ACT_P_CREATED; 180c7e2b968SJiri Pirko } else { 181*65a206c0SChris Mi tcf_idr_release(*a, bind); 182c7e2b968SJiri Pirko if (!ovr) 183c7e2b968SJiri Pirko return -EEXIST; 184c7e2b968SJiri Pirko } 185c7e2b968SJiri Pirko 186a85a970aSWANG Cong v = to_vlan(*a); 187c7e2b968SJiri Pirko 188c7e2b968SJiri Pirko spin_lock_bh(&v->tcf_lock); 189c7e2b968SJiri Pirko 190c7e2b968SJiri Pirko v->tcfv_action = action; 191c7e2b968SJiri Pirko v->tcfv_push_vid = push_vid; 192956af371SHadar Hen Zion v->tcfv_push_prio = push_prio; 193c7e2b968SJiri Pirko v->tcfv_push_proto = push_proto; 194c7e2b968SJiri Pirko 195c7e2b968SJiri Pirko v->tcf_action = parm->action; 196c7e2b968SJiri Pirko 197c7e2b968SJiri Pirko spin_unlock_bh(&v->tcf_lock); 198c7e2b968SJiri Pirko 199c7e2b968SJiri Pirko if (ret == ACT_P_CREATED) 200*65a206c0SChris Mi tcf_idr_insert(tn, *a); 201c7e2b968SJiri Pirko return ret; 202c7e2b968SJiri Pirko } 203c7e2b968SJiri Pirko 204c7e2b968SJiri Pirko static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, 205c7e2b968SJiri Pirko int bind, int ref) 206c7e2b968SJiri Pirko { 207c7e2b968SJiri Pirko unsigned char *b = skb_tail_pointer(skb); 208a85a970aSWANG Cong struct tcf_vlan *v = to_vlan(a); 209c7e2b968SJiri Pirko struct tc_vlan opt = { 210c7e2b968SJiri Pirko .index = v->tcf_index, 211c7e2b968SJiri Pirko .refcnt = v->tcf_refcnt - ref, 212c7e2b968SJiri Pirko .bindcnt = v->tcf_bindcnt - bind, 213c7e2b968SJiri Pirko .action = v->tcf_action, 214c7e2b968SJiri Pirko .v_action = v->tcfv_action, 215c7e2b968SJiri Pirko }; 216c7e2b968SJiri Pirko struct tcf_t t; 217c7e2b968SJiri Pirko 218c7e2b968SJiri Pirko if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) 219c7e2b968SJiri Pirko goto nla_put_failure; 220c7e2b968SJiri Pirko 22145a497f2SShmulik Ladkani if ((v->tcfv_action == TCA_VLAN_ACT_PUSH || 22245a497f2SShmulik Ladkani v->tcfv_action == TCA_VLAN_ACT_MODIFY) && 223c7e2b968SJiri Pirko (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) || 2240b0f43feSJamal Hadi Salim nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, 225956af371SHadar Hen Zion v->tcfv_push_proto) || 226956af371SHadar Hen Zion (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, 227956af371SHadar Hen Zion v->tcfv_push_prio)))) 228c7e2b968SJiri Pirko goto nla_put_failure; 229c7e2b968SJiri Pirko 23048d8ee16SJamal Hadi Salim tcf_tm_dump(&t, &v->tcf_tm); 2319854518eSNicolas Dichtel if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) 232c7e2b968SJiri Pirko goto nla_put_failure; 233c7e2b968SJiri Pirko return skb->len; 234c7e2b968SJiri Pirko 235c7e2b968SJiri Pirko nla_put_failure: 236c7e2b968SJiri Pirko nlmsg_trim(skb, b); 237c7e2b968SJiri Pirko return -1; 238c7e2b968SJiri Pirko } 239c7e2b968SJiri Pirko 240ddf97ccdSWANG Cong static int tcf_vlan_walker(struct net *net, struct sk_buff *skb, 241ddf97ccdSWANG Cong struct netlink_callback *cb, int type, 242a85a970aSWANG Cong const struct tc_action_ops *ops) 243ddf97ccdSWANG Cong { 244ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 245ddf97ccdSWANG Cong 246a85a970aSWANG Cong return tcf_generic_walker(tn, skb, cb, type, ops); 247ddf97ccdSWANG Cong } 248ddf97ccdSWANG Cong 249a85a970aSWANG Cong static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) 250ddf97ccdSWANG Cong { 251ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 252ddf97ccdSWANG Cong 253*65a206c0SChris Mi return tcf_idr_search(tn, a, index); 254ddf97ccdSWANG Cong } 255ddf97ccdSWANG Cong 256c7e2b968SJiri Pirko static struct tc_action_ops act_vlan_ops = { 257c7e2b968SJiri Pirko .kind = "vlan", 258c7e2b968SJiri Pirko .type = TCA_ACT_VLAN, 259c7e2b968SJiri Pirko .owner = THIS_MODULE, 260c7e2b968SJiri Pirko .act = tcf_vlan, 261c7e2b968SJiri Pirko .dump = tcf_vlan_dump, 262c7e2b968SJiri Pirko .init = tcf_vlan_init, 263ddf97ccdSWANG Cong .walk = tcf_vlan_walker, 264ddf97ccdSWANG Cong .lookup = tcf_vlan_search, 265a85a970aSWANG Cong .size = sizeof(struct tcf_vlan), 266ddf97ccdSWANG Cong }; 267ddf97ccdSWANG Cong 268ddf97ccdSWANG Cong static __net_init int vlan_init_net(struct net *net) 269ddf97ccdSWANG Cong { 270ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 271ddf97ccdSWANG Cong 272*65a206c0SChris Mi return tc_action_net_init(tn, &act_vlan_ops); 273ddf97ccdSWANG Cong } 274ddf97ccdSWANG Cong 275ddf97ccdSWANG Cong static void __net_exit vlan_exit_net(struct net *net) 276ddf97ccdSWANG Cong { 277ddf97ccdSWANG Cong struct tc_action_net *tn = net_generic(net, vlan_net_id); 278ddf97ccdSWANG Cong 279ddf97ccdSWANG Cong tc_action_net_exit(tn); 280ddf97ccdSWANG Cong } 281ddf97ccdSWANG Cong 282ddf97ccdSWANG Cong static struct pernet_operations vlan_net_ops = { 283ddf97ccdSWANG Cong .init = vlan_init_net, 284ddf97ccdSWANG Cong .exit = vlan_exit_net, 285ddf97ccdSWANG Cong .id = &vlan_net_id, 286ddf97ccdSWANG Cong .size = sizeof(struct tc_action_net), 287c7e2b968SJiri Pirko }; 288c7e2b968SJiri Pirko 289c7e2b968SJiri Pirko static int __init vlan_init_module(void) 290c7e2b968SJiri Pirko { 291ddf97ccdSWANG Cong return tcf_register_action(&act_vlan_ops, &vlan_net_ops); 292c7e2b968SJiri Pirko } 293c7e2b968SJiri Pirko 294c7e2b968SJiri Pirko static void __exit vlan_cleanup_module(void) 295c7e2b968SJiri Pirko { 296ddf97ccdSWANG Cong tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); 297c7e2b968SJiri Pirko } 298c7e2b968SJiri Pirko 299c7e2b968SJiri Pirko module_init(vlan_init_module); 300c7e2b968SJiri Pirko module_exit(vlan_cleanup_module); 301c7e2b968SJiri Pirko 302c7e2b968SJiri Pirko MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 303c7e2b968SJiri Pirko MODULE_DESCRIPTION("vlan manipulation actions"); 304c7e2b968SJiri Pirko MODULE_LICENSE("GPL v2"); 305