18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/module.h> 78c2ecf20Sopenharmony_ci#include <linux/init.h> 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 108c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h> 118c2ecf20Sopenharmony_ci#include <linux/if_vlan.h> 128c2ecf20Sopenharmony_ci#include <net/netlink.h> 138c2ecf20Sopenharmony_ci#include <net/pkt_sched.h> 148c2ecf20Sopenharmony_ci#include <net/pkt_cls.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <linux/tc_act/tc_vlan.h> 178c2ecf20Sopenharmony_ci#include <net/tc_act/tc_vlan.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic unsigned int vlan_net_id; 208c2ecf20Sopenharmony_cistatic struct tc_action_ops act_vlan_ops; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a, 238c2ecf20Sopenharmony_ci struct tcf_result *res) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct tcf_vlan *v = to_vlan(a); 268c2ecf20Sopenharmony_ci struct tcf_vlan_params *p; 278c2ecf20Sopenharmony_ci int action; 288c2ecf20Sopenharmony_ci int err; 298c2ecf20Sopenharmony_ci u16 tci; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci tcf_lastuse_update(&v->tcf_tm); 328c2ecf20Sopenharmony_ci tcf_action_update_bstats(&v->common, skb); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci /* Ensure 'data' points at mac_header prior calling vlan manipulating 358c2ecf20Sopenharmony_ci * functions. 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci if (skb_at_tc_ingress(skb)) 388c2ecf20Sopenharmony_ci skb_push_rcsum(skb, skb->mac_len); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci action = READ_ONCE(v->tcf_action); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci p = rcu_dereference_bh(v->vlan_p); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci switch (p->tcfv_action) { 458c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_POP: 468c2ecf20Sopenharmony_ci err = skb_vlan_pop(skb); 478c2ecf20Sopenharmony_ci if (err) 488c2ecf20Sopenharmony_ci goto drop; 498c2ecf20Sopenharmony_ci break; 508c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_PUSH: 518c2ecf20Sopenharmony_ci err = skb_vlan_push(skb, p->tcfv_push_proto, p->tcfv_push_vid | 528c2ecf20Sopenharmony_ci (p->tcfv_push_prio << VLAN_PRIO_SHIFT)); 538c2ecf20Sopenharmony_ci if (err) 548c2ecf20Sopenharmony_ci goto drop; 558c2ecf20Sopenharmony_ci break; 568c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_MODIFY: 578c2ecf20Sopenharmony_ci /* No-op if no vlan tag (either hw-accel or in-payload) */ 588c2ecf20Sopenharmony_ci if (!skb_vlan_tagged(skb)) 598c2ecf20Sopenharmony_ci goto out; 608c2ecf20Sopenharmony_ci /* extract existing tag (and guarantee no hw-accel tag) */ 618c2ecf20Sopenharmony_ci if (skb_vlan_tag_present(skb)) { 628c2ecf20Sopenharmony_ci tci = skb_vlan_tag_get(skb); 638c2ecf20Sopenharmony_ci __vlan_hwaccel_clear_tag(skb); 648c2ecf20Sopenharmony_ci } else { 658c2ecf20Sopenharmony_ci /* in-payload vlan tag, pop it */ 668c2ecf20Sopenharmony_ci err = __skb_vlan_pop(skb, &tci); 678c2ecf20Sopenharmony_ci if (err) 688c2ecf20Sopenharmony_ci goto drop; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci /* replace the vid */ 718c2ecf20Sopenharmony_ci tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid; 728c2ecf20Sopenharmony_ci /* replace prio bits, if tcfv_push_prio specified */ 738c2ecf20Sopenharmony_ci if (p->tcfv_push_prio_exists) { 748c2ecf20Sopenharmony_ci tci &= ~VLAN_PRIO_MASK; 758c2ecf20Sopenharmony_ci tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT; 768c2ecf20Sopenharmony_ci } 778c2ecf20Sopenharmony_ci /* put updated tci as hwaccel tag */ 788c2ecf20Sopenharmony_ci __vlan_hwaccel_put_tag(skb, p->tcfv_push_proto, tci); 798c2ecf20Sopenharmony_ci break; 808c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_POP_ETH: 818c2ecf20Sopenharmony_ci err = skb_eth_pop(skb); 828c2ecf20Sopenharmony_ci if (err) 838c2ecf20Sopenharmony_ci goto drop; 848c2ecf20Sopenharmony_ci break; 858c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_PUSH_ETH: 868c2ecf20Sopenharmony_ci err = skb_eth_push(skb, p->tcfv_push_dst, p->tcfv_push_src); 878c2ecf20Sopenharmony_ci if (err) 888c2ecf20Sopenharmony_ci goto drop; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci default: 918c2ecf20Sopenharmony_ci BUG(); 928c2ecf20Sopenharmony_ci } 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ciout: 958c2ecf20Sopenharmony_ci if (skb_at_tc_ingress(skb)) 968c2ecf20Sopenharmony_ci skb_pull_rcsum(skb, skb->mac_len); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci return action; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cidrop: 1018c2ecf20Sopenharmony_ci tcf_action_inc_drop_qstats(&v->common); 1028c2ecf20Sopenharmony_ci return TC_ACT_SHOT; 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_cistatic const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { 1068c2ecf20Sopenharmony_ci [TCA_VLAN_UNSPEC] = { .strict_start_type = TCA_VLAN_PUSH_ETH_DST }, 1078c2ecf20Sopenharmony_ci [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, 1088c2ecf20Sopenharmony_ci [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, 1098c2ecf20Sopenharmony_ci [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, 1108c2ecf20Sopenharmony_ci [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, 1118c2ecf20Sopenharmony_ci [TCA_VLAN_PUSH_ETH_DST] = NLA_POLICY_ETH_ADDR, 1128c2ecf20Sopenharmony_ci [TCA_VLAN_PUSH_ETH_SRC] = NLA_POLICY_ETH_ADDR, 1138c2ecf20Sopenharmony_ci}; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cistatic int tcf_vlan_init(struct net *net, struct nlattr *nla, 1168c2ecf20Sopenharmony_ci struct nlattr *est, struct tc_action **a, 1178c2ecf20Sopenharmony_ci int ovr, int bind, bool rtnl_held, 1188c2ecf20Sopenharmony_ci struct tcf_proto *tp, u32 flags, 1198c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, vlan_net_id); 1228c2ecf20Sopenharmony_ci struct nlattr *tb[TCA_VLAN_MAX + 1]; 1238c2ecf20Sopenharmony_ci struct tcf_chain *goto_ch = NULL; 1248c2ecf20Sopenharmony_ci bool push_prio_exists = false; 1258c2ecf20Sopenharmony_ci struct tcf_vlan_params *p; 1268c2ecf20Sopenharmony_ci struct tc_vlan *parm; 1278c2ecf20Sopenharmony_ci struct tcf_vlan *v; 1288c2ecf20Sopenharmony_ci int action; 1298c2ecf20Sopenharmony_ci u16 push_vid = 0; 1308c2ecf20Sopenharmony_ci __be16 push_proto = 0; 1318c2ecf20Sopenharmony_ci u8 push_prio = 0; 1328c2ecf20Sopenharmony_ci bool exists = false; 1338c2ecf20Sopenharmony_ci int ret = 0, err; 1348c2ecf20Sopenharmony_ci u32 index; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (!nla) 1378c2ecf20Sopenharmony_ci return -EINVAL; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci err = nla_parse_nested_deprecated(tb, TCA_VLAN_MAX, nla, vlan_policy, 1408c2ecf20Sopenharmony_ci NULL); 1418c2ecf20Sopenharmony_ci if (err < 0) 1428c2ecf20Sopenharmony_ci return err; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci if (!tb[TCA_VLAN_PARMS]) 1458c2ecf20Sopenharmony_ci return -EINVAL; 1468c2ecf20Sopenharmony_ci parm = nla_data(tb[TCA_VLAN_PARMS]); 1478c2ecf20Sopenharmony_ci index = parm->index; 1488c2ecf20Sopenharmony_ci err = tcf_idr_check_alloc(tn, &index, a, bind); 1498c2ecf20Sopenharmony_ci if (err < 0) 1508c2ecf20Sopenharmony_ci return err; 1518c2ecf20Sopenharmony_ci exists = err; 1528c2ecf20Sopenharmony_ci if (exists && bind) 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci switch (parm->v_action) { 1568c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_POP: 1578c2ecf20Sopenharmony_ci break; 1588c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_PUSH: 1598c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_MODIFY: 1608c2ecf20Sopenharmony_ci if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { 1618c2ecf20Sopenharmony_ci if (exists) 1628c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 1638c2ecf20Sopenharmony_ci else 1648c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 1658c2ecf20Sopenharmony_ci return -EINVAL; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); 1688c2ecf20Sopenharmony_ci if (push_vid >= VLAN_VID_MASK) { 1698c2ecf20Sopenharmony_ci if (exists) 1708c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 1718c2ecf20Sopenharmony_ci else 1728c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 1738c2ecf20Sopenharmony_ci return -ERANGE; 1748c2ecf20Sopenharmony_ci } 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { 1778c2ecf20Sopenharmony_ci push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); 1788c2ecf20Sopenharmony_ci switch (push_proto) { 1798c2ecf20Sopenharmony_ci case htons(ETH_P_8021Q): 1808c2ecf20Sopenharmony_ci case htons(ETH_P_8021AD): 1818c2ecf20Sopenharmony_ci break; 1828c2ecf20Sopenharmony_ci default: 1838c2ecf20Sopenharmony_ci if (exists) 1848c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 1858c2ecf20Sopenharmony_ci else 1868c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 1878c2ecf20Sopenharmony_ci return -EPROTONOSUPPORT; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci } else { 1908c2ecf20Sopenharmony_ci push_proto = htons(ETH_P_8021Q); 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY]; 1948c2ecf20Sopenharmony_ci if (push_prio_exists) 1958c2ecf20Sopenharmony_ci push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); 1968c2ecf20Sopenharmony_ci break; 1978c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_POP_ETH: 1988c2ecf20Sopenharmony_ci break; 1998c2ecf20Sopenharmony_ci case TCA_VLAN_ACT_PUSH_ETH: 2008c2ecf20Sopenharmony_ci if (!tb[TCA_VLAN_PUSH_ETH_DST] || !tb[TCA_VLAN_PUSH_ETH_SRC]) { 2018c2ecf20Sopenharmony_ci if (exists) 2028c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 2038c2ecf20Sopenharmony_ci else 2048c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 2058c2ecf20Sopenharmony_ci return -EINVAL; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci break; 2088c2ecf20Sopenharmony_ci default: 2098c2ecf20Sopenharmony_ci if (exists) 2108c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 2118c2ecf20Sopenharmony_ci else 2128c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci action = parm->v_action; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci if (!exists) { 2188c2ecf20Sopenharmony_ci ret = tcf_idr_create_from_flags(tn, index, est, a, 2198c2ecf20Sopenharmony_ci &act_vlan_ops, bind, flags); 2208c2ecf20Sopenharmony_ci if (ret) { 2218c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 2228c2ecf20Sopenharmony_ci return ret; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci ret = ACT_P_CREATED; 2268c2ecf20Sopenharmony_ci } else if (!ovr) { 2278c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 2288c2ecf20Sopenharmony_ci return -EEXIST; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 2328c2ecf20Sopenharmony_ci if (err < 0) 2338c2ecf20Sopenharmony_ci goto release_idr; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci v = to_vlan(*a); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci p = kzalloc(sizeof(*p), GFP_KERNEL); 2388c2ecf20Sopenharmony_ci if (!p) { 2398c2ecf20Sopenharmony_ci err = -ENOMEM; 2408c2ecf20Sopenharmony_ci goto put_chain; 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci p->tcfv_action = action; 2448c2ecf20Sopenharmony_ci p->tcfv_push_vid = push_vid; 2458c2ecf20Sopenharmony_ci p->tcfv_push_prio = push_prio; 2468c2ecf20Sopenharmony_ci p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH; 2478c2ecf20Sopenharmony_ci p->tcfv_push_proto = push_proto; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci if (action == TCA_VLAN_ACT_PUSH_ETH) { 2508c2ecf20Sopenharmony_ci nla_memcpy(&p->tcfv_push_dst, tb[TCA_VLAN_PUSH_ETH_DST], 2518c2ecf20Sopenharmony_ci ETH_ALEN); 2528c2ecf20Sopenharmony_ci nla_memcpy(&p->tcfv_push_src, tb[TCA_VLAN_PUSH_ETH_SRC], 2538c2ecf20Sopenharmony_ci ETH_ALEN); 2548c2ecf20Sopenharmony_ci } 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci spin_lock_bh(&v->tcf_lock); 2578c2ecf20Sopenharmony_ci goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 2588c2ecf20Sopenharmony_ci p = rcu_replace_pointer(v->vlan_p, p, lockdep_is_held(&v->tcf_lock)); 2598c2ecf20Sopenharmony_ci spin_unlock_bh(&v->tcf_lock); 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci if (goto_ch) 2628c2ecf20Sopenharmony_ci tcf_chain_put_by_act(goto_ch); 2638c2ecf20Sopenharmony_ci if (p) 2648c2ecf20Sopenharmony_ci kfree_rcu(p, rcu); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci return ret; 2678c2ecf20Sopenharmony_ciput_chain: 2688c2ecf20Sopenharmony_ci if (goto_ch) 2698c2ecf20Sopenharmony_ci tcf_chain_put_by_act(goto_ch); 2708c2ecf20Sopenharmony_cirelease_idr: 2718c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 2728c2ecf20Sopenharmony_ci return err; 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic void tcf_vlan_cleanup(struct tc_action *a) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci struct tcf_vlan *v = to_vlan(a); 2788c2ecf20Sopenharmony_ci struct tcf_vlan_params *p; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci p = rcu_dereference_protected(v->vlan_p, 1); 2818c2ecf20Sopenharmony_ci if (p) 2828c2ecf20Sopenharmony_ci kfree_rcu(p, rcu); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, 2868c2ecf20Sopenharmony_ci int bind, int ref) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci unsigned char *b = skb_tail_pointer(skb); 2898c2ecf20Sopenharmony_ci struct tcf_vlan *v = to_vlan(a); 2908c2ecf20Sopenharmony_ci struct tcf_vlan_params *p; 2918c2ecf20Sopenharmony_ci struct tc_vlan opt = { 2928c2ecf20Sopenharmony_ci .index = v->tcf_index, 2938c2ecf20Sopenharmony_ci .refcnt = refcount_read(&v->tcf_refcnt) - ref, 2948c2ecf20Sopenharmony_ci .bindcnt = atomic_read(&v->tcf_bindcnt) - bind, 2958c2ecf20Sopenharmony_ci }; 2968c2ecf20Sopenharmony_ci struct tcf_t t; 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci spin_lock_bh(&v->tcf_lock); 2998c2ecf20Sopenharmony_ci opt.action = v->tcf_action; 3008c2ecf20Sopenharmony_ci p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock)); 3018c2ecf20Sopenharmony_ci opt.v_action = p->tcfv_action; 3028c2ecf20Sopenharmony_ci if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) 3038c2ecf20Sopenharmony_ci goto nla_put_failure; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci if ((p->tcfv_action == TCA_VLAN_ACT_PUSH || 3068c2ecf20Sopenharmony_ci p->tcfv_action == TCA_VLAN_ACT_MODIFY) && 3078c2ecf20Sopenharmony_ci (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || 3088c2ecf20Sopenharmony_ci nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, 3098c2ecf20Sopenharmony_ci p->tcfv_push_proto) || 3108c2ecf20Sopenharmony_ci (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, 3118c2ecf20Sopenharmony_ci p->tcfv_push_prio)))) 3128c2ecf20Sopenharmony_ci goto nla_put_failure; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) { 3158c2ecf20Sopenharmony_ci if (nla_put(skb, TCA_VLAN_PUSH_ETH_DST, ETH_ALEN, 3168c2ecf20Sopenharmony_ci p->tcfv_push_dst)) 3178c2ecf20Sopenharmony_ci goto nla_put_failure; 3188c2ecf20Sopenharmony_ci if (nla_put(skb, TCA_VLAN_PUSH_ETH_SRC, ETH_ALEN, 3198c2ecf20Sopenharmony_ci p->tcfv_push_src)) 3208c2ecf20Sopenharmony_ci goto nla_put_failure; 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci tcf_tm_dump(&t, &v->tcf_tm); 3248c2ecf20Sopenharmony_ci if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) 3258c2ecf20Sopenharmony_ci goto nla_put_failure; 3268c2ecf20Sopenharmony_ci spin_unlock_bh(&v->tcf_lock); 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci return skb->len; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_cinla_put_failure: 3318c2ecf20Sopenharmony_ci spin_unlock_bh(&v->tcf_lock); 3328c2ecf20Sopenharmony_ci nlmsg_trim(skb, b); 3338c2ecf20Sopenharmony_ci return -1; 3348c2ecf20Sopenharmony_ci} 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_cistatic int tcf_vlan_walker(struct net *net, struct sk_buff *skb, 3378c2ecf20Sopenharmony_ci struct netlink_callback *cb, int type, 3388c2ecf20Sopenharmony_ci const struct tc_action_ops *ops, 3398c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, vlan_net_id); 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci return tcf_generic_walker(tn, skb, cb, type, ops, extack); 3448c2ecf20Sopenharmony_ci} 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_cistatic void tcf_vlan_stats_update(struct tc_action *a, u64 bytes, u64 packets, 3478c2ecf20Sopenharmony_ci u64 drops, u64 lastuse, bool hw) 3488c2ecf20Sopenharmony_ci{ 3498c2ecf20Sopenharmony_ci struct tcf_vlan *v = to_vlan(a); 3508c2ecf20Sopenharmony_ci struct tcf_t *tm = &v->tcf_tm; 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci tcf_action_update_stats(a, bytes, packets, drops, hw); 3538c2ecf20Sopenharmony_ci tm->lastuse = max_t(u64, tm->lastuse, lastuse); 3548c2ecf20Sopenharmony_ci} 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_cistatic int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, vlan_net_id); 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci return tcf_idr_search(tn, a, index); 3618c2ecf20Sopenharmony_ci} 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_cistatic size_t tcf_vlan_get_fill_size(const struct tc_action *act) 3648c2ecf20Sopenharmony_ci{ 3658c2ecf20Sopenharmony_ci return nla_total_size(sizeof(struct tc_vlan)) 3668c2ecf20Sopenharmony_ci + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */ 3678c2ecf20Sopenharmony_ci + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */ 3688c2ecf20Sopenharmony_ci + nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */ 3698c2ecf20Sopenharmony_ci} 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_cistatic struct tc_action_ops act_vlan_ops = { 3728c2ecf20Sopenharmony_ci .kind = "vlan", 3738c2ecf20Sopenharmony_ci .id = TCA_ID_VLAN, 3748c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 3758c2ecf20Sopenharmony_ci .act = tcf_vlan_act, 3768c2ecf20Sopenharmony_ci .dump = tcf_vlan_dump, 3778c2ecf20Sopenharmony_ci .init = tcf_vlan_init, 3788c2ecf20Sopenharmony_ci .cleanup = tcf_vlan_cleanup, 3798c2ecf20Sopenharmony_ci .walk = tcf_vlan_walker, 3808c2ecf20Sopenharmony_ci .stats_update = tcf_vlan_stats_update, 3818c2ecf20Sopenharmony_ci .get_fill_size = tcf_vlan_get_fill_size, 3828c2ecf20Sopenharmony_ci .lookup = tcf_vlan_search, 3838c2ecf20Sopenharmony_ci .size = sizeof(struct tcf_vlan), 3848c2ecf20Sopenharmony_ci}; 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic __net_init int vlan_init_net(struct net *net) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, vlan_net_id); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci return tc_action_net_init(net, tn, &act_vlan_ops); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_cistatic void __net_exit vlan_exit_net(struct list_head *net_list) 3948c2ecf20Sopenharmony_ci{ 3958c2ecf20Sopenharmony_ci tc_action_net_exit(net_list, vlan_net_id); 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_cistatic struct pernet_operations vlan_net_ops = { 3998c2ecf20Sopenharmony_ci .init = vlan_init_net, 4008c2ecf20Sopenharmony_ci .exit_batch = vlan_exit_net, 4018c2ecf20Sopenharmony_ci .id = &vlan_net_id, 4028c2ecf20Sopenharmony_ci .size = sizeof(struct tc_action_net), 4038c2ecf20Sopenharmony_ci}; 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic int __init vlan_init_module(void) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci return tcf_register_action(&act_vlan_ops, &vlan_net_ops); 4088c2ecf20Sopenharmony_ci} 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_cistatic void __exit vlan_cleanup_module(void) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); 4138c2ecf20Sopenharmony_ci} 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_cimodule_init(vlan_init_module); 4168c2ecf20Sopenharmony_cimodule_exit(vlan_cleanup_module); 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); 4198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("vlan manipulation actions"); 4208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 421