18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 28c2ecf20Sopenharmony_ci/* Copyright (C) 2019 Netronome Systems, Inc. */ 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ci#include <linux/if_arp.h> 58c2ecf20Sopenharmony_ci#include <linux/init.h> 68c2ecf20Sopenharmony_ci#include <linux/kernel.h> 78c2ecf20Sopenharmony_ci#include <linux/module.h> 88c2ecf20Sopenharmony_ci#include <linux/mpls.h> 98c2ecf20Sopenharmony_ci#include <linux/rtnetlink.h> 108c2ecf20Sopenharmony_ci#include <linux/skbuff.h> 118c2ecf20Sopenharmony_ci#include <linux/tc_act/tc_mpls.h> 128c2ecf20Sopenharmony_ci#include <net/mpls.h> 138c2ecf20Sopenharmony_ci#include <net/netlink.h> 148c2ecf20Sopenharmony_ci#include <net/pkt_sched.h> 158c2ecf20Sopenharmony_ci#include <net/pkt_cls.h> 168c2ecf20Sopenharmony_ci#include <net/tc_act/tc_mpls.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic unsigned int mpls_net_id; 198c2ecf20Sopenharmony_cistatic struct tc_action_ops act_mpls_ops; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define ACT_MPLS_TTL_DEFAULT 255 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse, 248c2ecf20Sopenharmony_ci struct tcf_mpls_params *p, bool set_bos) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci u32 new_lse = 0; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci if (lse) 298c2ecf20Sopenharmony_ci new_lse = be32_to_cpu(lse->label_stack_entry); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) { 328c2ecf20Sopenharmony_ci new_lse &= ~MPLS_LS_LABEL_MASK; 338c2ecf20Sopenharmony_ci new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT; 348c2ecf20Sopenharmony_ci } 358c2ecf20Sopenharmony_ci if (p->tcfm_ttl) { 368c2ecf20Sopenharmony_ci new_lse &= ~MPLS_LS_TTL_MASK; 378c2ecf20Sopenharmony_ci new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT; 388c2ecf20Sopenharmony_ci } 398c2ecf20Sopenharmony_ci if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) { 408c2ecf20Sopenharmony_ci new_lse &= ~MPLS_LS_TC_MASK; 418c2ecf20Sopenharmony_ci new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT; 428c2ecf20Sopenharmony_ci } 438c2ecf20Sopenharmony_ci if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) { 448c2ecf20Sopenharmony_ci new_lse &= ~MPLS_LS_S_MASK; 458c2ecf20Sopenharmony_ci new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT; 468c2ecf20Sopenharmony_ci } else if (set_bos) { 478c2ecf20Sopenharmony_ci new_lse |= 1 << MPLS_LS_S_SHIFT; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return cpu_to_be32(new_lse); 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int tcf_mpls_act(struct sk_buff *skb, const struct tc_action *a, 548c2ecf20Sopenharmony_ci struct tcf_result *res) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct tcf_mpls *m = to_mpls(a); 578c2ecf20Sopenharmony_ci struct tcf_mpls_params *p; 588c2ecf20Sopenharmony_ci __be32 new_lse; 598c2ecf20Sopenharmony_ci int ret, mac_len; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci tcf_lastuse_update(&m->tcf_tm); 628c2ecf20Sopenharmony_ci bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci /* Ensure 'data' points at mac_header prior calling mpls manipulating 658c2ecf20Sopenharmony_ci * functions. 668c2ecf20Sopenharmony_ci */ 678c2ecf20Sopenharmony_ci if (skb_at_tc_ingress(skb)) { 688c2ecf20Sopenharmony_ci skb_push_rcsum(skb, skb->mac_len); 698c2ecf20Sopenharmony_ci mac_len = skb->mac_len; 708c2ecf20Sopenharmony_ci } else { 718c2ecf20Sopenharmony_ci mac_len = skb_network_header(skb) - skb_mac_header(skb); 728c2ecf20Sopenharmony_ci } 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci ret = READ_ONCE(m->tcf_action); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci p = rcu_dereference_bh(m->mpls_p); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci switch (p->tcfm_action) { 798c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_POP: 808c2ecf20Sopenharmony_ci if (skb_mpls_pop(skb, p->tcfm_proto, mac_len, 818c2ecf20Sopenharmony_ci skb->dev && skb->dev->type == ARPHRD_ETHER)) 828c2ecf20Sopenharmony_ci goto drop; 838c2ecf20Sopenharmony_ci break; 848c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_PUSH: 858c2ecf20Sopenharmony_ci new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true))); 868c2ecf20Sopenharmony_ci if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len, 878c2ecf20Sopenharmony_ci skb->dev && skb->dev->type == ARPHRD_ETHER)) 888c2ecf20Sopenharmony_ci goto drop; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_MAC_PUSH: 918c2ecf20Sopenharmony_ci if (skb_vlan_tag_present(skb)) { 928c2ecf20Sopenharmony_ci if (__vlan_insert_inner_tag(skb, skb->vlan_proto, 938c2ecf20Sopenharmony_ci skb_vlan_tag_get(skb), 948c2ecf20Sopenharmony_ci ETH_HLEN) < 0) 958c2ecf20Sopenharmony_ci goto drop; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci skb->protocol = skb->vlan_proto; 988c2ecf20Sopenharmony_ci __vlan_hwaccel_clear_tag(skb); 998c2ecf20Sopenharmony_ci } 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci new_lse = tcf_mpls_get_lse(NULL, p, mac_len || 1028c2ecf20Sopenharmony_ci !eth_p_mpls(skb->protocol)); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false)) 1058c2ecf20Sopenharmony_ci goto drop; 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_MODIFY: 1088c2ecf20Sopenharmony_ci if (!pskb_may_pull(skb, 1098c2ecf20Sopenharmony_ci skb_network_offset(skb) + MPLS_HLEN)) 1108c2ecf20Sopenharmony_ci goto drop; 1118c2ecf20Sopenharmony_ci new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false); 1128c2ecf20Sopenharmony_ci if (skb_mpls_update_lse(skb, new_lse)) 1138c2ecf20Sopenharmony_ci goto drop; 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_DEC_TTL: 1168c2ecf20Sopenharmony_ci if (skb_mpls_dec_ttl(skb)) 1178c2ecf20Sopenharmony_ci goto drop; 1188c2ecf20Sopenharmony_ci break; 1198c2ecf20Sopenharmony_ci } 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci if (skb_at_tc_ingress(skb)) 1228c2ecf20Sopenharmony_ci skb_pull_rcsum(skb, skb->mac_len); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return ret; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_cidrop: 1278c2ecf20Sopenharmony_ci qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats)); 1288c2ecf20Sopenharmony_ci return TC_ACT_SHOT; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic int valid_label(const struct nlattr *attr, 1328c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci const u32 *label = nla_data(attr); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (nla_len(attr) != sizeof(*label)) { 1378c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length"); 1388c2ecf20Sopenharmony_ci return -EINVAL; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) { 1428c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range"); 1438c2ecf20Sopenharmony_ci return -EINVAL; 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = { 1508c2ecf20Sopenharmony_ci [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)), 1518c2ecf20Sopenharmony_ci [TCA_MPLS_PROTO] = { .type = NLA_U16 }, 1528c2ecf20Sopenharmony_ci [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_BINARY, 1538c2ecf20Sopenharmony_ci valid_label), 1548c2ecf20Sopenharmony_ci [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7), 1558c2ecf20Sopenharmony_ci [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1), 1568c2ecf20Sopenharmony_ci [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1), 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic int tcf_mpls_init(struct net *net, struct nlattr *nla, 1608c2ecf20Sopenharmony_ci struct nlattr *est, struct tc_action **a, 1618c2ecf20Sopenharmony_ci int ovr, int bind, bool rtnl_held, 1628c2ecf20Sopenharmony_ci struct tcf_proto *tp, u32 flags, 1638c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, mpls_net_id); 1668c2ecf20Sopenharmony_ci struct nlattr *tb[TCA_MPLS_MAX + 1]; 1678c2ecf20Sopenharmony_ci struct tcf_chain *goto_ch = NULL; 1688c2ecf20Sopenharmony_ci struct tcf_mpls_params *p; 1698c2ecf20Sopenharmony_ci struct tc_mpls *parm; 1708c2ecf20Sopenharmony_ci bool exists = false; 1718c2ecf20Sopenharmony_ci struct tcf_mpls *m; 1728c2ecf20Sopenharmony_ci int ret = 0, err; 1738c2ecf20Sopenharmony_ci u8 mpls_ttl = 0; 1748c2ecf20Sopenharmony_ci u32 index; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci if (!nla) { 1778c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes"); 1788c2ecf20Sopenharmony_ci return -EINVAL; 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack); 1828c2ecf20Sopenharmony_ci if (err < 0) 1838c2ecf20Sopenharmony_ci return err; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (!tb[TCA_MPLS_PARMS]) { 1868c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "No MPLS params"); 1878c2ecf20Sopenharmony_ci return -EINVAL; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci parm = nla_data(tb[TCA_MPLS_PARMS]); 1908c2ecf20Sopenharmony_ci index = parm->index; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci /* Verify parameters against action type. */ 1938c2ecf20Sopenharmony_ci switch (parm->m_action) { 1948c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_POP: 1958c2ecf20Sopenharmony_ci if (!tb[TCA_MPLS_PROTO]) { 1968c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop"); 1978c2ecf20Sopenharmony_ci return -EINVAL; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) { 2008c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop"); 2018c2ecf20Sopenharmony_ci return -EINVAL; 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || 2048c2ecf20Sopenharmony_ci tb[TCA_MPLS_BOS]) { 2058c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop"); 2068c2ecf20Sopenharmony_ci return -EINVAL; 2078c2ecf20Sopenharmony_ci } 2088c2ecf20Sopenharmony_ci break; 2098c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_DEC_TTL: 2108c2ecf20Sopenharmony_ci if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] || 2118c2ecf20Sopenharmony_ci tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) { 2128c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl"); 2138c2ecf20Sopenharmony_ci return -EINVAL; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci break; 2168c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_PUSH: 2178c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_MAC_PUSH: 2188c2ecf20Sopenharmony_ci if (!tb[TCA_MPLS_LABEL]) { 2198c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push"); 2208c2ecf20Sopenharmony_ci return -EINVAL; 2218c2ecf20Sopenharmony_ci } 2228c2ecf20Sopenharmony_ci if (tb[TCA_MPLS_PROTO] && 2238c2ecf20Sopenharmony_ci !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) { 2248c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push"); 2258c2ecf20Sopenharmony_ci return -EPROTONOSUPPORT; 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci /* Push needs a TTL - if not specified, set a default value. */ 2288c2ecf20Sopenharmony_ci if (!tb[TCA_MPLS_TTL]) { 2298c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_MPLS) 2308c2ecf20Sopenharmony_ci mpls_ttl = net->mpls.default_ttl ? 2318c2ecf20Sopenharmony_ci net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT; 2328c2ecf20Sopenharmony_ci#else 2338c2ecf20Sopenharmony_ci mpls_ttl = ACT_MPLS_TTL_DEFAULT; 2348c2ecf20Sopenharmony_ci#endif 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci break; 2378c2ecf20Sopenharmony_ci case TCA_MPLS_ACT_MODIFY: 2388c2ecf20Sopenharmony_ci if (tb[TCA_MPLS_PROTO]) { 2398c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify"); 2408c2ecf20Sopenharmony_ci return -EINVAL; 2418c2ecf20Sopenharmony_ci } 2428c2ecf20Sopenharmony_ci break; 2438c2ecf20Sopenharmony_ci default: 2448c2ecf20Sopenharmony_ci NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action"); 2458c2ecf20Sopenharmony_ci return -EINVAL; 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci err = tcf_idr_check_alloc(tn, &index, a, bind); 2498c2ecf20Sopenharmony_ci if (err < 0) 2508c2ecf20Sopenharmony_ci return err; 2518c2ecf20Sopenharmony_ci exists = err; 2528c2ecf20Sopenharmony_ci if (exists && bind) 2538c2ecf20Sopenharmony_ci return 0; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci if (!exists) { 2568c2ecf20Sopenharmony_ci ret = tcf_idr_create(tn, index, est, a, 2578c2ecf20Sopenharmony_ci &act_mpls_ops, bind, true, flags); 2588c2ecf20Sopenharmony_ci if (ret) { 2598c2ecf20Sopenharmony_ci tcf_idr_cleanup(tn, index); 2608c2ecf20Sopenharmony_ci return ret; 2618c2ecf20Sopenharmony_ci } 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci ret = ACT_P_CREATED; 2648c2ecf20Sopenharmony_ci } else if (!ovr) { 2658c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 2668c2ecf20Sopenharmony_ci return -EEXIST; 2678c2ecf20Sopenharmony_ci } 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 2708c2ecf20Sopenharmony_ci if (err < 0) 2718c2ecf20Sopenharmony_ci goto release_idr; 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci m = to_mpls(*a); 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci p = kzalloc(sizeof(*p), GFP_KERNEL); 2768c2ecf20Sopenharmony_ci if (!p) { 2778c2ecf20Sopenharmony_ci err = -ENOMEM; 2788c2ecf20Sopenharmony_ci goto put_chain; 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci p->tcfm_action = parm->m_action; 2828c2ecf20Sopenharmony_ci p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) : 2838c2ecf20Sopenharmony_ci ACT_MPLS_LABEL_NOT_SET; 2848c2ecf20Sopenharmony_ci p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) : 2858c2ecf20Sopenharmony_ci ACT_MPLS_TC_NOT_SET; 2868c2ecf20Sopenharmony_ci p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) : 2878c2ecf20Sopenharmony_ci mpls_ttl; 2888c2ecf20Sopenharmony_ci p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) : 2898c2ecf20Sopenharmony_ci ACT_MPLS_BOS_NOT_SET; 2908c2ecf20Sopenharmony_ci p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) : 2918c2ecf20Sopenharmony_ci htons(ETH_P_MPLS_UC); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci spin_lock_bh(&m->tcf_lock); 2948c2ecf20Sopenharmony_ci goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 2958c2ecf20Sopenharmony_ci p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock)); 2968c2ecf20Sopenharmony_ci spin_unlock_bh(&m->tcf_lock); 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci if (goto_ch) 2998c2ecf20Sopenharmony_ci tcf_chain_put_by_act(goto_ch); 3008c2ecf20Sopenharmony_ci if (p) 3018c2ecf20Sopenharmony_ci kfree_rcu(p, rcu); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci return ret; 3048c2ecf20Sopenharmony_ciput_chain: 3058c2ecf20Sopenharmony_ci if (goto_ch) 3068c2ecf20Sopenharmony_ci tcf_chain_put_by_act(goto_ch); 3078c2ecf20Sopenharmony_cirelease_idr: 3088c2ecf20Sopenharmony_ci tcf_idr_release(*a, bind); 3098c2ecf20Sopenharmony_ci return err; 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic void tcf_mpls_cleanup(struct tc_action *a) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct tcf_mpls *m = to_mpls(a); 3158c2ecf20Sopenharmony_ci struct tcf_mpls_params *p; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci p = rcu_dereference_protected(m->mpls_p, 1); 3188c2ecf20Sopenharmony_ci if (p) 3198c2ecf20Sopenharmony_ci kfree_rcu(p, rcu); 3208c2ecf20Sopenharmony_ci} 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_cistatic int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a, 3238c2ecf20Sopenharmony_ci int bind, int ref) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci unsigned char *b = skb_tail_pointer(skb); 3268c2ecf20Sopenharmony_ci struct tcf_mpls *m = to_mpls(a); 3278c2ecf20Sopenharmony_ci struct tcf_mpls_params *p; 3288c2ecf20Sopenharmony_ci struct tc_mpls opt = { 3298c2ecf20Sopenharmony_ci .index = m->tcf_index, 3308c2ecf20Sopenharmony_ci .refcnt = refcount_read(&m->tcf_refcnt) - ref, 3318c2ecf20Sopenharmony_ci .bindcnt = atomic_read(&m->tcf_bindcnt) - bind, 3328c2ecf20Sopenharmony_ci }; 3338c2ecf20Sopenharmony_ci struct tcf_t t; 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci spin_lock_bh(&m->tcf_lock); 3368c2ecf20Sopenharmony_ci opt.action = m->tcf_action; 3378c2ecf20Sopenharmony_ci p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock)); 3388c2ecf20Sopenharmony_ci opt.m_action = p->tcfm_action; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt)) 3418c2ecf20Sopenharmony_ci goto nla_put_failure; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET && 3448c2ecf20Sopenharmony_ci nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label)) 3458c2ecf20Sopenharmony_ci goto nla_put_failure; 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET && 3488c2ecf20Sopenharmony_ci nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc)) 3498c2ecf20Sopenharmony_ci goto nla_put_failure; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl)) 3528c2ecf20Sopenharmony_ci goto nla_put_failure; 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET && 3558c2ecf20Sopenharmony_ci nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos)) 3568c2ecf20Sopenharmony_ci goto nla_put_failure; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto)) 3598c2ecf20Sopenharmony_ci goto nla_put_failure; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci tcf_tm_dump(&t, &m->tcf_tm); 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD)) 3648c2ecf20Sopenharmony_ci goto nla_put_failure; 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci spin_unlock_bh(&m->tcf_lock); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci return skb->len; 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cinla_put_failure: 3718c2ecf20Sopenharmony_ci spin_unlock_bh(&m->tcf_lock); 3728c2ecf20Sopenharmony_ci nlmsg_trim(skb, b); 3738c2ecf20Sopenharmony_ci return -EMSGSIZE; 3748c2ecf20Sopenharmony_ci} 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_cistatic int tcf_mpls_walker(struct net *net, struct sk_buff *skb, 3778c2ecf20Sopenharmony_ci struct netlink_callback *cb, int type, 3788c2ecf20Sopenharmony_ci const struct tc_action_ops *ops, 3798c2ecf20Sopenharmony_ci struct netlink_ext_ack *extack) 3808c2ecf20Sopenharmony_ci{ 3818c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, mpls_net_id); 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci return tcf_generic_walker(tn, skb, cb, type, ops, extack); 3848c2ecf20Sopenharmony_ci} 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_cistatic int tcf_mpls_search(struct net *net, struct tc_action **a, u32 index) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, mpls_net_id); 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci return tcf_idr_search(tn, a, index); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_cistatic struct tc_action_ops act_mpls_ops = { 3948c2ecf20Sopenharmony_ci .kind = "mpls", 3958c2ecf20Sopenharmony_ci .id = TCA_ID_MPLS, 3968c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 3978c2ecf20Sopenharmony_ci .act = tcf_mpls_act, 3988c2ecf20Sopenharmony_ci .dump = tcf_mpls_dump, 3998c2ecf20Sopenharmony_ci .init = tcf_mpls_init, 4008c2ecf20Sopenharmony_ci .cleanup = tcf_mpls_cleanup, 4018c2ecf20Sopenharmony_ci .walk = tcf_mpls_walker, 4028c2ecf20Sopenharmony_ci .lookup = tcf_mpls_search, 4038c2ecf20Sopenharmony_ci .size = sizeof(struct tcf_mpls), 4048c2ecf20Sopenharmony_ci}; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_cistatic __net_init int mpls_init_net(struct net *net) 4078c2ecf20Sopenharmony_ci{ 4088c2ecf20Sopenharmony_ci struct tc_action_net *tn = net_generic(net, mpls_net_id); 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci return tc_action_net_init(net, tn, &act_mpls_ops); 4118c2ecf20Sopenharmony_ci} 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_cistatic void __net_exit mpls_exit_net(struct list_head *net_list) 4148c2ecf20Sopenharmony_ci{ 4158c2ecf20Sopenharmony_ci tc_action_net_exit(net_list, mpls_net_id); 4168c2ecf20Sopenharmony_ci} 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_cistatic struct pernet_operations mpls_net_ops = { 4198c2ecf20Sopenharmony_ci .init = mpls_init_net, 4208c2ecf20Sopenharmony_ci .exit_batch = mpls_exit_net, 4218c2ecf20Sopenharmony_ci .id = &mpls_net_id, 4228c2ecf20Sopenharmony_ci .size = sizeof(struct tc_action_net), 4238c2ecf20Sopenharmony_ci}; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic int __init mpls_init_module(void) 4268c2ecf20Sopenharmony_ci{ 4278c2ecf20Sopenharmony_ci return tcf_register_action(&act_mpls_ops, &mpls_net_ops); 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_cistatic void __exit mpls_cleanup_module(void) 4318c2ecf20Sopenharmony_ci{ 4328c2ecf20Sopenharmony_ci tcf_unregister_action(&act_mpls_ops, &mpls_net_ops); 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_cimodule_init(mpls_init_module); 4368c2ecf20Sopenharmony_cimodule_exit(mpls_cleanup_module); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ciMODULE_SOFTDEP("post: mpls_gso"); 4398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>"); 4408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 4418c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MPLS manipulation actions"); 442