xref: /openbmc/linux/net/sched/act_mpls.c (revision 9e17f99220d111ea031b44153fdfe364b0024ff2)
12a2ea508SJohn Hurley // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
22a2ea508SJohn Hurley /* Copyright (C) 2019 Netronome Systems, Inc. */
32a2ea508SJohn Hurley 
4040b5cfbSMartin Varghese #include <linux/if_arp.h>
52a2ea508SJohn Hurley #include <linux/init.h>
62a2ea508SJohn Hurley #include <linux/kernel.h>
72a2ea508SJohn Hurley #include <linux/module.h>
82a2ea508SJohn Hurley #include <linux/mpls.h>
92a2ea508SJohn Hurley #include <linux/rtnetlink.h>
102a2ea508SJohn Hurley #include <linux/skbuff.h>
112a2ea508SJohn Hurley #include <linux/tc_act/tc_mpls.h>
122a2ea508SJohn Hurley #include <net/mpls.h>
132a2ea508SJohn Hurley #include <net/netlink.h>
142a2ea508SJohn Hurley #include <net/pkt_sched.h>
152a2ea508SJohn Hurley #include <net/pkt_cls.h>
162a2ea508SJohn Hurley #include <net/tc_act/tc_mpls.h>
17871cf386SPedro Tammela #include <net/tc_wrapper.h>
182a2ea508SJohn Hurley 
192a2ea508SJohn Hurley static struct tc_action_ops act_mpls_ops;
202a2ea508SJohn Hurley 
212a2ea508SJohn Hurley #define ACT_MPLS_TTL_DEFAULT	255
222a2ea508SJohn Hurley 
232a2ea508SJohn Hurley static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
242a2ea508SJohn Hurley 			       struct tcf_mpls_params *p, bool set_bos)
252a2ea508SJohn Hurley {
262a2ea508SJohn Hurley 	u32 new_lse = 0;
272a2ea508SJohn Hurley 
282a2ea508SJohn Hurley 	if (lse)
292a2ea508SJohn Hurley 		new_lse = be32_to_cpu(lse->label_stack_entry);
302a2ea508SJohn Hurley 
312a2ea508SJohn Hurley 	if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
322a2ea508SJohn Hurley 		new_lse &= ~MPLS_LS_LABEL_MASK;
332a2ea508SJohn Hurley 		new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
342a2ea508SJohn Hurley 	}
352a2ea508SJohn Hurley 	if (p->tcfm_ttl) {
362a2ea508SJohn Hurley 		new_lse &= ~MPLS_LS_TTL_MASK;
372a2ea508SJohn Hurley 		new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
382a2ea508SJohn Hurley 	}
392a2ea508SJohn Hurley 	if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
402a2ea508SJohn Hurley 		new_lse &= ~MPLS_LS_TC_MASK;
412a2ea508SJohn Hurley 		new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
422a2ea508SJohn Hurley 	}
432a2ea508SJohn Hurley 	if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
442a2ea508SJohn Hurley 		new_lse &= ~MPLS_LS_S_MASK;
452a2ea508SJohn Hurley 		new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
462a2ea508SJohn Hurley 	} else if (set_bos) {
472a2ea508SJohn Hurley 		new_lse |= 1 << MPLS_LS_S_SHIFT;
482a2ea508SJohn Hurley 	}
492a2ea508SJohn Hurley 
502a2ea508SJohn Hurley 	return cpu_to_be32(new_lse);
512a2ea508SJohn Hurley }
522a2ea508SJohn Hurley 
53871cf386SPedro Tammela TC_INDIRECT_SCOPE int tcf_mpls_act(struct sk_buff *skb,
54871cf386SPedro Tammela 				   const struct tc_action *a,
552a2ea508SJohn Hurley 				   struct tcf_result *res)
562a2ea508SJohn Hurley {
572a2ea508SJohn Hurley 	struct tcf_mpls *m = to_mpls(a);
582a2ea508SJohn Hurley 	struct tcf_mpls_params *p;
592a2ea508SJohn Hurley 	__be32 new_lse;
60fa4e0f88SDavide Caratti 	int ret, mac_len;
612a2ea508SJohn Hurley 
622a2ea508SJohn Hurley 	tcf_lastuse_update(&m->tcf_tm);
6350dc9a85SAhmed S. Darwish 	bstats_update(this_cpu_ptr(m->common.cpu_bstats), skb);
642a2ea508SJohn Hurley 
652a2ea508SJohn Hurley 	/* Ensure 'data' points at mac_header prior calling mpls manipulating
662a2ea508SJohn Hurley 	 * functions.
672a2ea508SJohn Hurley 	 */
68fa4e0f88SDavide Caratti 	if (skb_at_tc_ingress(skb)) {
692a2ea508SJohn Hurley 		skb_push_rcsum(skb, skb->mac_len);
70fa4e0f88SDavide Caratti 		mac_len = skb->mac_len;
71fa4e0f88SDavide Caratti 	} else {
72fa4e0f88SDavide Caratti 		mac_len = skb_network_header(skb) - skb_mac_header(skb);
73fa4e0f88SDavide Caratti 	}
742a2ea508SJohn Hurley 
752a2ea508SJohn Hurley 	ret = READ_ONCE(m->tcf_action);
762a2ea508SJohn Hurley 
772a2ea508SJohn Hurley 	p = rcu_dereference_bh(m->mpls_p);
782a2ea508SJohn Hurley 
792a2ea508SJohn Hurley 	switch (p->tcfm_action) {
802a2ea508SJohn Hurley 	case TCA_MPLS_ACT_POP:
81040b5cfbSMartin Varghese 		if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
82040b5cfbSMartin Varghese 				 skb->dev && skb->dev->type == ARPHRD_ETHER))
832a2ea508SJohn Hurley 			goto drop;
842a2ea508SJohn Hurley 		break;
852a2ea508SJohn Hurley 	case TCA_MPLS_ACT_PUSH:
86d7bf2ebeSToke Høiland-Jørgensen 		new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
87d04ac224SMartin Varghese 		if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
88d04ac224SMartin Varghese 				  skb->dev && skb->dev->type == ARPHRD_ETHER))
892a2ea508SJohn Hurley 			goto drop;
902a2ea508SJohn Hurley 		break;
91a45294afSGuillaume Nault 	case TCA_MPLS_ACT_MAC_PUSH:
92a45294afSGuillaume Nault 		if (skb_vlan_tag_present(skb)) {
93a45294afSGuillaume Nault 			if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
94a45294afSGuillaume Nault 						    skb_vlan_tag_get(skb),
95a45294afSGuillaume Nault 						    ETH_HLEN) < 0)
96a45294afSGuillaume Nault 				goto drop;
97a45294afSGuillaume Nault 
98a45294afSGuillaume Nault 			skb->protocol = skb->vlan_proto;
99a45294afSGuillaume Nault 			__vlan_hwaccel_clear_tag(skb);
100a45294afSGuillaume Nault 		}
101a45294afSGuillaume Nault 
102a45294afSGuillaume Nault 		new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
103a45294afSGuillaume Nault 					   !eth_p_mpls(skb->protocol));
104a45294afSGuillaume Nault 
105a45294afSGuillaume Nault 		if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
106a45294afSGuillaume Nault 			goto drop;
107a45294afSGuillaume Nault 		break;
1082a2ea508SJohn Hurley 	case TCA_MPLS_ACT_MODIFY:
1099608fa65SDavide Caratti 		if (!pskb_may_pull(skb,
1109608fa65SDavide Caratti 				   skb_network_offset(skb) + MPLS_HLEN))
1119608fa65SDavide Caratti 			goto drop;
1122a2ea508SJohn Hurley 		new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
1132a2ea508SJohn Hurley 		if (skb_mpls_update_lse(skb, new_lse))
1142a2ea508SJohn Hurley 			goto drop;
1152a2ea508SJohn Hurley 		break;
1162a2ea508SJohn Hurley 	case TCA_MPLS_ACT_DEC_TTL:
1172a2ea508SJohn Hurley 		if (skb_mpls_dec_ttl(skb))
1182a2ea508SJohn Hurley 			goto drop;
1192a2ea508SJohn Hurley 		break;
1202a2ea508SJohn Hurley 	}
1212a2ea508SJohn Hurley 
1222a2ea508SJohn Hurley 	if (skb_at_tc_ingress(skb))
1232a2ea508SJohn Hurley 		skb_pull_rcsum(skb, skb->mac_len);
1242a2ea508SJohn Hurley 
1252a2ea508SJohn Hurley 	return ret;
1262a2ea508SJohn Hurley 
1272a2ea508SJohn Hurley drop:
1282a2ea508SJohn Hurley 	qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
1292a2ea508SJohn Hurley 	return TC_ACT_SHOT;
1302a2ea508SJohn Hurley }
1312a2ea508SJohn Hurley 
1322a2ea508SJohn Hurley static int valid_label(const struct nlattr *attr,
1332a2ea508SJohn Hurley 		       struct netlink_ext_ack *extack)
1342a2ea508SJohn Hurley {
1352a2ea508SJohn Hurley 	const u32 *label = nla_data(attr);
1362a2ea508SJohn Hurley 
137*9e17f992SIdo Schimmel 	if (nla_len(attr) != sizeof(*label)) {
138*9e17f992SIdo Schimmel 		NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length");
139*9e17f992SIdo Schimmel 		return -EINVAL;
140*9e17f992SIdo Schimmel 	}
141*9e17f992SIdo Schimmel 
1422a2ea508SJohn Hurley 	if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
1432a2ea508SJohn Hurley 		NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
1442a2ea508SJohn Hurley 		return -EINVAL;
1452a2ea508SJohn Hurley 	}
1462a2ea508SJohn Hurley 
1472a2ea508SJohn Hurley 	return 0;
1482a2ea508SJohn Hurley }
1492a2ea508SJohn Hurley 
1502a2ea508SJohn Hurley static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
1512a2ea508SJohn Hurley 	[TCA_MPLS_PARMS]	= NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
1522a2ea508SJohn Hurley 	[TCA_MPLS_PROTO]	= { .type = NLA_U16 },
153*9e17f992SIdo Schimmel 	[TCA_MPLS_LABEL]	= NLA_POLICY_VALIDATE_FN(NLA_BINARY,
154*9e17f992SIdo Schimmel 							 valid_label),
1552a2ea508SJohn Hurley 	[TCA_MPLS_TC]		= NLA_POLICY_RANGE(NLA_U8, 0, 7),
1562a2ea508SJohn Hurley 	[TCA_MPLS_TTL]		= NLA_POLICY_MIN(NLA_U8, 1),
1572a2ea508SJohn Hurley 	[TCA_MPLS_BOS]		= NLA_POLICY_RANGE(NLA_U8, 0, 1),
1582a2ea508SJohn Hurley };
1592a2ea508SJohn Hurley 
1602a2ea508SJohn Hurley static int tcf_mpls_init(struct net *net, struct nlattr *nla,
1612a2ea508SJohn Hurley 			 struct nlattr *est, struct tc_action **a,
162abbb0d33SVlad Buslov 			 struct tcf_proto *tp, u32 flags,
163abbb0d33SVlad Buslov 			 struct netlink_ext_ack *extack)
1642a2ea508SJohn Hurley {
165acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
166695176bfSCong Wang 	bool bind = flags & TCA_ACT_FLAGS_BIND;
1672a2ea508SJohn Hurley 	struct nlattr *tb[TCA_MPLS_MAX + 1];
1682a2ea508SJohn Hurley 	struct tcf_chain *goto_ch = NULL;
1692a2ea508SJohn Hurley 	struct tcf_mpls_params *p;
1702a2ea508SJohn Hurley 	struct tc_mpls *parm;
1712a2ea508SJohn Hurley 	bool exists = false;
1722a2ea508SJohn Hurley 	struct tcf_mpls *m;
1732a2ea508SJohn Hurley 	int ret = 0, err;
1742a2ea508SJohn Hurley 	u8 mpls_ttl = 0;
1757be8ef2cSDmytro Linkin 	u32 index;
1762a2ea508SJohn Hurley 
1772a2ea508SJohn Hurley 	if (!nla) {
1782a2ea508SJohn Hurley 		NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
1792a2ea508SJohn Hurley 		return -EINVAL;
1802a2ea508SJohn Hurley 	}
1812a2ea508SJohn Hurley 
1822a2ea508SJohn Hurley 	err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
1832a2ea508SJohn Hurley 	if (err < 0)
1842a2ea508SJohn Hurley 		return err;
1852a2ea508SJohn Hurley 
1862a2ea508SJohn Hurley 	if (!tb[TCA_MPLS_PARMS]) {
1872a2ea508SJohn Hurley 		NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
1882a2ea508SJohn Hurley 		return -EINVAL;
1892a2ea508SJohn Hurley 	}
1902a2ea508SJohn Hurley 	parm = nla_data(tb[TCA_MPLS_PARMS]);
1917be8ef2cSDmytro Linkin 	index = parm->index;
1922a2ea508SJohn Hurley 
1932a2ea508SJohn Hurley 	/* Verify parameters against action type. */
1942a2ea508SJohn Hurley 	switch (parm->m_action) {
1952a2ea508SJohn Hurley 	case TCA_MPLS_ACT_POP:
1962a2ea508SJohn Hurley 		if (!tb[TCA_MPLS_PROTO]) {
1972a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
1982a2ea508SJohn Hurley 			return -EINVAL;
1992a2ea508SJohn Hurley 		}
2002a2ea508SJohn Hurley 		if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
2012a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
2022a2ea508SJohn Hurley 			return -EINVAL;
2032a2ea508SJohn Hurley 		}
2042a2ea508SJohn Hurley 		if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
2052a2ea508SJohn Hurley 		    tb[TCA_MPLS_BOS]) {
2062a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
2072a2ea508SJohn Hurley 			return -EINVAL;
2082a2ea508SJohn Hurley 		}
2092a2ea508SJohn Hurley 		break;
2102a2ea508SJohn Hurley 	case TCA_MPLS_ACT_DEC_TTL:
2112a2ea508SJohn Hurley 		if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
2122a2ea508SJohn Hurley 		    tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
2132a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
2142a2ea508SJohn Hurley 			return -EINVAL;
2152a2ea508SJohn Hurley 		}
2162a2ea508SJohn Hurley 		break;
2172a2ea508SJohn Hurley 	case TCA_MPLS_ACT_PUSH:
218a45294afSGuillaume Nault 	case TCA_MPLS_ACT_MAC_PUSH:
2192a2ea508SJohn Hurley 		if (!tb[TCA_MPLS_LABEL]) {
2202a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
2212a2ea508SJohn Hurley 			return -EINVAL;
2222a2ea508SJohn Hurley 		}
2232a2ea508SJohn Hurley 		if (tb[TCA_MPLS_PROTO] &&
2242a2ea508SJohn Hurley 		    !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
2252a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
2262a2ea508SJohn Hurley 			return -EPROTONOSUPPORT;
2272a2ea508SJohn Hurley 		}
2282a2ea508SJohn Hurley 		/* Push needs a TTL - if not specified, set a default value. */
2292a2ea508SJohn Hurley 		if (!tb[TCA_MPLS_TTL]) {
2302a2ea508SJohn Hurley #if IS_ENABLED(CONFIG_MPLS)
2312a2ea508SJohn Hurley 			mpls_ttl = net->mpls.default_ttl ?
2322a2ea508SJohn Hurley 				   net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
2332a2ea508SJohn Hurley #else
2342a2ea508SJohn Hurley 			mpls_ttl = ACT_MPLS_TTL_DEFAULT;
2352a2ea508SJohn Hurley #endif
2362a2ea508SJohn Hurley 		}
2372a2ea508SJohn Hurley 		break;
2382a2ea508SJohn Hurley 	case TCA_MPLS_ACT_MODIFY:
2392a2ea508SJohn Hurley 		if (tb[TCA_MPLS_PROTO]) {
2402a2ea508SJohn Hurley 			NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
2412a2ea508SJohn Hurley 			return -EINVAL;
2422a2ea508SJohn Hurley 		}
2432a2ea508SJohn Hurley 		break;
2442a2ea508SJohn Hurley 	default:
2452a2ea508SJohn Hurley 		NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
2462a2ea508SJohn Hurley 		return -EINVAL;
2472a2ea508SJohn Hurley 	}
2482a2ea508SJohn Hurley 
2497be8ef2cSDmytro Linkin 	err = tcf_idr_check_alloc(tn, &index, a, bind);
2502a2ea508SJohn Hurley 	if (err < 0)
2512a2ea508SJohn Hurley 		return err;
2522a2ea508SJohn Hurley 	exists = err;
2532a2ea508SJohn Hurley 	if (exists && bind)
2542a2ea508SJohn Hurley 		return 0;
2552a2ea508SJohn Hurley 
2562a2ea508SJohn Hurley 	if (!exists) {
2577be8ef2cSDmytro Linkin 		ret = tcf_idr_create(tn, index, est, a,
25840bd094dSBaowen Zheng 				     &act_mpls_ops, bind, true, flags);
2592a2ea508SJohn Hurley 		if (ret) {
2607be8ef2cSDmytro Linkin 			tcf_idr_cleanup(tn, index);
2612a2ea508SJohn Hurley 			return ret;
2622a2ea508SJohn Hurley 		}
2632a2ea508SJohn Hurley 
2642a2ea508SJohn Hurley 		ret = ACT_P_CREATED;
265695176bfSCong Wang 	} else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
2662a2ea508SJohn Hurley 		tcf_idr_release(*a, bind);
2672a2ea508SJohn Hurley 		return -EEXIST;
2682a2ea508SJohn Hurley 	}
2692a2ea508SJohn Hurley 
2702a2ea508SJohn Hurley 	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
2712a2ea508SJohn Hurley 	if (err < 0)
2722a2ea508SJohn Hurley 		goto release_idr;
2732a2ea508SJohn Hurley 
2742a2ea508SJohn Hurley 	m = to_mpls(*a);
2752a2ea508SJohn Hurley 
2762a2ea508SJohn Hurley 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2772a2ea508SJohn Hurley 	if (!p) {
2782a2ea508SJohn Hurley 		err = -ENOMEM;
2792a2ea508SJohn Hurley 		goto put_chain;
2802a2ea508SJohn Hurley 	}
2812a2ea508SJohn Hurley 
2822a2ea508SJohn Hurley 	p->tcfm_action = parm->m_action;
2832a2ea508SJohn Hurley 	p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
2842a2ea508SJohn Hurley 					     ACT_MPLS_LABEL_NOT_SET;
2852a2ea508SJohn Hurley 	p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
2862a2ea508SJohn Hurley 				       ACT_MPLS_TC_NOT_SET;
2872a2ea508SJohn Hurley 	p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
2882a2ea508SJohn Hurley 					 mpls_ttl;
2892a2ea508SJohn Hurley 	p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
2902a2ea508SJohn Hurley 					 ACT_MPLS_BOS_NOT_SET;
2912a2ea508SJohn Hurley 	p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
2922a2ea508SJohn Hurley 					     htons(ETH_P_MPLS_UC);
2932a2ea508SJohn Hurley 
2942a2ea508SJohn Hurley 	spin_lock_bh(&m->tcf_lock);
2952a2ea508SJohn Hurley 	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
296445d3749SPaul E. McKenney 	p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
2972a2ea508SJohn Hurley 	spin_unlock_bh(&m->tcf_lock);
2982a2ea508SJohn Hurley 
2992a2ea508SJohn Hurley 	if (goto_ch)
3002a2ea508SJohn Hurley 		tcf_chain_put_by_act(goto_ch);
3012a2ea508SJohn Hurley 	if (p)
3022a2ea508SJohn Hurley 		kfree_rcu(p, rcu);
3032a2ea508SJohn Hurley 
3042a2ea508SJohn Hurley 	return ret;
3052a2ea508SJohn Hurley put_chain:
3062a2ea508SJohn Hurley 	if (goto_ch)
3072a2ea508SJohn Hurley 		tcf_chain_put_by_act(goto_ch);
3082a2ea508SJohn Hurley release_idr:
3092a2ea508SJohn Hurley 	tcf_idr_release(*a, bind);
3102a2ea508SJohn Hurley 	return err;
3112a2ea508SJohn Hurley }
3122a2ea508SJohn Hurley 
3132a2ea508SJohn Hurley static void tcf_mpls_cleanup(struct tc_action *a)
3142a2ea508SJohn Hurley {
3152a2ea508SJohn Hurley 	struct tcf_mpls *m = to_mpls(a);
3162a2ea508SJohn Hurley 	struct tcf_mpls_params *p;
3172a2ea508SJohn Hurley 
3182a2ea508SJohn Hurley 	p = rcu_dereference_protected(m->mpls_p, 1);
3192a2ea508SJohn Hurley 	if (p)
3202a2ea508SJohn Hurley 		kfree_rcu(p, rcu);
3212a2ea508SJohn Hurley }
3222a2ea508SJohn Hurley 
3232a2ea508SJohn Hurley static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
3242a2ea508SJohn Hurley 			 int bind, int ref)
3252a2ea508SJohn Hurley {
3262a2ea508SJohn Hurley 	unsigned char *b = skb_tail_pointer(skb);
3272a2ea508SJohn Hurley 	struct tcf_mpls *m = to_mpls(a);
3282a2ea508SJohn Hurley 	struct tcf_mpls_params *p;
3292a2ea508SJohn Hurley 	struct tc_mpls opt = {
3302a2ea508SJohn Hurley 		.index    = m->tcf_index,
3312a2ea508SJohn Hurley 		.refcnt   = refcount_read(&m->tcf_refcnt) - ref,
3322a2ea508SJohn Hurley 		.bindcnt  = atomic_read(&m->tcf_bindcnt) - bind,
3332a2ea508SJohn Hurley 	};
3342a2ea508SJohn Hurley 	struct tcf_t t;
3352a2ea508SJohn Hurley 
3362a2ea508SJohn Hurley 	spin_lock_bh(&m->tcf_lock);
3372a2ea508SJohn Hurley 	opt.action = m->tcf_action;
3382a2ea508SJohn Hurley 	p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
3392a2ea508SJohn Hurley 	opt.m_action = p->tcfm_action;
3402a2ea508SJohn Hurley 
3412a2ea508SJohn Hurley 	if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
3422a2ea508SJohn Hurley 		goto nla_put_failure;
3432a2ea508SJohn Hurley 
3442a2ea508SJohn Hurley 	if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
3452a2ea508SJohn Hurley 	    nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
3462a2ea508SJohn Hurley 		goto nla_put_failure;
3472a2ea508SJohn Hurley 
3482a2ea508SJohn Hurley 	if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
3492a2ea508SJohn Hurley 	    nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
3502a2ea508SJohn Hurley 		goto nla_put_failure;
3512a2ea508SJohn Hurley 
3522a2ea508SJohn Hurley 	if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
3532a2ea508SJohn Hurley 		goto nla_put_failure;
3542a2ea508SJohn Hurley 
3552a2ea508SJohn Hurley 	if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
3562a2ea508SJohn Hurley 	    nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
3572a2ea508SJohn Hurley 		goto nla_put_failure;
3582a2ea508SJohn Hurley 
3592a2ea508SJohn Hurley 	if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
3602a2ea508SJohn Hurley 		goto nla_put_failure;
3612a2ea508SJohn Hurley 
3622a2ea508SJohn Hurley 	tcf_tm_dump(&t, &m->tcf_tm);
3632a2ea508SJohn Hurley 
3642a2ea508SJohn Hurley 	if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
3652a2ea508SJohn Hurley 		goto nla_put_failure;
3662a2ea508SJohn Hurley 
3672a2ea508SJohn Hurley 	spin_unlock_bh(&m->tcf_lock);
3682a2ea508SJohn Hurley 
3692a2ea508SJohn Hurley 	return skb->len;
3702a2ea508SJohn Hurley 
3712a2ea508SJohn Hurley nla_put_failure:
3722a2ea508SJohn Hurley 	spin_unlock_bh(&m->tcf_lock);
3732a2ea508SJohn Hurley 	nlmsg_trim(skb, b);
3742a2ea508SJohn Hurley 	return -EMSGSIZE;
3752a2ea508SJohn Hurley }
3762a2ea508SJohn Hurley 
377c54e1d92SBaowen Zheng static int tcf_mpls_offload_act_setup(struct tc_action *act, void *entry_data,
378c2ccf84eSIdo Schimmel 				      u32 *index_inc, bool bind,
379c2ccf84eSIdo Schimmel 				      struct netlink_ext_ack *extack)
380c54e1d92SBaowen Zheng {
381c54e1d92SBaowen Zheng 	if (bind) {
382c54e1d92SBaowen Zheng 		struct flow_action_entry *entry = entry_data;
383c54e1d92SBaowen Zheng 
384c54e1d92SBaowen Zheng 		switch (tcf_mpls_action(act)) {
385c54e1d92SBaowen Zheng 		case TCA_MPLS_ACT_PUSH:
386c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_MPLS_PUSH;
387c54e1d92SBaowen Zheng 			entry->mpls_push.proto = tcf_mpls_proto(act);
388c54e1d92SBaowen Zheng 			entry->mpls_push.label = tcf_mpls_label(act);
389c54e1d92SBaowen Zheng 			entry->mpls_push.tc = tcf_mpls_tc(act);
390c54e1d92SBaowen Zheng 			entry->mpls_push.bos = tcf_mpls_bos(act);
391c54e1d92SBaowen Zheng 			entry->mpls_push.ttl = tcf_mpls_ttl(act);
392c54e1d92SBaowen Zheng 			break;
393c54e1d92SBaowen Zheng 		case TCA_MPLS_ACT_POP:
394c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_MPLS_POP;
395c54e1d92SBaowen Zheng 			entry->mpls_pop.proto = tcf_mpls_proto(act);
396c54e1d92SBaowen Zheng 			break;
397c54e1d92SBaowen Zheng 		case TCA_MPLS_ACT_MODIFY:
398c54e1d92SBaowen Zheng 			entry->id = FLOW_ACTION_MPLS_MANGLE;
399c54e1d92SBaowen Zheng 			entry->mpls_mangle.label = tcf_mpls_label(act);
400c54e1d92SBaowen Zheng 			entry->mpls_mangle.tc = tcf_mpls_tc(act);
401c54e1d92SBaowen Zheng 			entry->mpls_mangle.bos = tcf_mpls_bos(act);
402c54e1d92SBaowen Zheng 			entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
403c54e1d92SBaowen Zheng 			break;
404bca3821dSIdo Schimmel 		case TCA_MPLS_ACT_DEC_TTL:
405bca3821dSIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"dec_ttl\" option is used");
406bca3821dSIdo Schimmel 			return -EOPNOTSUPP;
407bca3821dSIdo Schimmel 		case TCA_MPLS_ACT_MAC_PUSH:
408bca3821dSIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"mac_push\" option is used");
409bca3821dSIdo Schimmel 			return -EOPNOTSUPP;
410c54e1d92SBaowen Zheng 		default:
411bca3821dSIdo Schimmel 			NL_SET_ERR_MSG_MOD(extack, "Unsupported MPLS mode offload");
412c54e1d92SBaowen Zheng 			return -EOPNOTSUPP;
413c54e1d92SBaowen Zheng 		}
414c54e1d92SBaowen Zheng 		*index_inc = 1;
415c54e1d92SBaowen Zheng 	} else {
4168cbfe939SBaowen Zheng 		struct flow_offload_action *fl_action = entry_data;
4178cbfe939SBaowen Zheng 
4188cbfe939SBaowen Zheng 		switch (tcf_mpls_action(act)) {
4198cbfe939SBaowen Zheng 		case TCA_MPLS_ACT_PUSH:
4208cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_MPLS_PUSH;
4218cbfe939SBaowen Zheng 			break;
4228cbfe939SBaowen Zheng 		case TCA_MPLS_ACT_POP:
4238cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_MPLS_POP;
4248cbfe939SBaowen Zheng 			break;
4258cbfe939SBaowen Zheng 		case TCA_MPLS_ACT_MODIFY:
4268cbfe939SBaowen Zheng 			fl_action->id = FLOW_ACTION_MPLS_MANGLE;
4278cbfe939SBaowen Zheng 			break;
4288cbfe939SBaowen Zheng 		default:
429c54e1d92SBaowen Zheng 			return -EOPNOTSUPP;
430c54e1d92SBaowen Zheng 		}
4318cbfe939SBaowen Zheng 	}
432c54e1d92SBaowen Zheng 
433c54e1d92SBaowen Zheng 	return 0;
434c54e1d92SBaowen Zheng }
435c54e1d92SBaowen Zheng 
4362a2ea508SJohn Hurley static struct tc_action_ops act_mpls_ops = {
4372a2ea508SJohn Hurley 	.kind		=	"mpls",
4382a2ea508SJohn Hurley 	.id		=	TCA_ID_MPLS,
4392a2ea508SJohn Hurley 	.owner		=	THIS_MODULE,
4402a2ea508SJohn Hurley 	.act		=	tcf_mpls_act,
4412a2ea508SJohn Hurley 	.dump		=	tcf_mpls_dump,
4422a2ea508SJohn Hurley 	.init		=	tcf_mpls_init,
4432a2ea508SJohn Hurley 	.cleanup	=	tcf_mpls_cleanup,
444c54e1d92SBaowen Zheng 	.offload_act_setup =	tcf_mpls_offload_act_setup,
4452a2ea508SJohn Hurley 	.size		=	sizeof(struct tcf_mpls),
4462a2ea508SJohn Hurley };
4472a2ea508SJohn Hurley 
4482a2ea508SJohn Hurley static __net_init int mpls_init_net(struct net *net)
4492a2ea508SJohn Hurley {
450acd0a7abSZhengchao Shao 	struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
4512a2ea508SJohn Hurley 
452981471bdSCong Wang 	return tc_action_net_init(net, tn, &act_mpls_ops);
4532a2ea508SJohn Hurley }
4542a2ea508SJohn Hurley 
4552a2ea508SJohn Hurley static void __net_exit mpls_exit_net(struct list_head *net_list)
4562a2ea508SJohn Hurley {
457acd0a7abSZhengchao Shao 	tc_action_net_exit(net_list, act_mpls_ops.net_id);
4582a2ea508SJohn Hurley }
4592a2ea508SJohn Hurley 
4602a2ea508SJohn Hurley static struct pernet_operations mpls_net_ops = {
4612a2ea508SJohn Hurley 	.init = mpls_init_net,
4622a2ea508SJohn Hurley 	.exit_batch = mpls_exit_net,
463acd0a7abSZhengchao Shao 	.id   = &act_mpls_ops.net_id,
4642a2ea508SJohn Hurley 	.size = sizeof(struct tc_action_net),
4652a2ea508SJohn Hurley };
4662a2ea508SJohn Hurley 
4672a2ea508SJohn Hurley static int __init mpls_init_module(void)
4682a2ea508SJohn Hurley {
4692a2ea508SJohn Hurley 	return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
4702a2ea508SJohn Hurley }
4712a2ea508SJohn Hurley 
4722a2ea508SJohn Hurley static void __exit mpls_cleanup_module(void)
4732a2ea508SJohn Hurley {
4742a2ea508SJohn Hurley 	tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
4752a2ea508SJohn Hurley }
4762a2ea508SJohn Hurley 
4772a2ea508SJohn Hurley module_init(mpls_init_module);
4782a2ea508SJohn Hurley module_exit(mpls_cleanup_module);
4792a2ea508SJohn Hurley 
480501b72aeSGuillaume Nault MODULE_SOFTDEP("post: mpls_gso");
4812a2ea508SJohn Hurley MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
4822a2ea508SJohn Hurley MODULE_LICENSE("GPL");
4832a2ea508SJohn Hurley MODULE_DESCRIPTION("MPLS manipulation actions");
484