1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
4 #include <linux/if_arp.h>
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/mpls.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/skbuff.h>
11 #include <linux/tc_act/tc_mpls.h>
13 #include <net/netlink.h>
14 #include <net/pkt_sched.h>
15 #include <net/pkt_cls.h>
16 #include <net/tc_act/tc_mpls.h>
17 #include <net/tc_wrapper.h>
19 static struct tc_action_ops act_mpls_ops;
21 #define ACT_MPLS_TTL_DEFAULT 255
23 static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
24 struct tcf_mpls_params *p, bool set_bos)
29 new_lse = be32_to_cpu(lse->label_stack_entry);
31 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
32 new_lse &= ~MPLS_LS_LABEL_MASK;
33 new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
36 new_lse &= ~MPLS_LS_TTL_MASK;
37 new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
39 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
40 new_lse &= ~MPLS_LS_TC_MASK;
41 new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
43 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
44 new_lse &= ~MPLS_LS_S_MASK;
45 new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
47 new_lse |= 1 << MPLS_LS_S_SHIFT;
50 return cpu_to_be32(new_lse);
53 TC_INDIRECT_SCOPE int tcf_mpls_act(struct sk_buff *skb,
54 const struct tc_action *a,
55 struct tcf_result *res)
57 struct tcf_mpls *m = to_mpls(a);
58 struct tcf_mpls_params *p;
62 tcf_lastuse_update(&m->tcf_tm);
63 bstats_update(this_cpu_ptr(m->common.cpu_bstats), skb);
65 /* Ensure 'data' points at mac_header prior calling mpls manipulating
68 if (skb_at_tc_ingress(skb)) {
69 skb_push_rcsum(skb, skb->mac_len);
70 mac_len = skb->mac_len;
72 mac_len = skb_network_header(skb) - skb_mac_header(skb);
75 ret = READ_ONCE(m->tcf_action);
77 p = rcu_dereference_bh(m->mpls_p);
79 switch (p->tcfm_action) {
80 case TCA_MPLS_ACT_POP:
81 if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
82 skb->dev && skb->dev->type == ARPHRD_ETHER))
85 case TCA_MPLS_ACT_PUSH:
86 new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
87 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
88 skb->dev && skb->dev->type == ARPHRD_ETHER))
91 case TCA_MPLS_ACT_MAC_PUSH:
92 if (skb_vlan_tag_present(skb)) {
93 if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
94 skb_vlan_tag_get(skb),
98 skb->protocol = skb->vlan_proto;
99 __vlan_hwaccel_clear_tag(skb);
102 new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
103 !eth_p_mpls(skb->protocol));
105 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
108 case TCA_MPLS_ACT_MODIFY:
109 if (!pskb_may_pull(skb,
110 skb_network_offset(skb) + MPLS_HLEN))
112 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
113 if (skb_mpls_update_lse(skb, new_lse))
116 case TCA_MPLS_ACT_DEC_TTL:
117 if (skb_mpls_dec_ttl(skb))
122 if (skb_at_tc_ingress(skb))
123 skb_pull_rcsum(skb, skb->mac_len);
128 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
132 static int valid_label(const struct nlattr *attr,
133 struct netlink_ext_ack *extack)
135 const u32 *label = nla_data(attr);
137 if (nla_len(attr) != sizeof(*label)) {
138 NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length");
142 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
143 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
150 static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
151 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
152 [TCA_MPLS_PROTO] = { .type = NLA_U16 },
153 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
155 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
156 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
157 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
160 static int tcf_mpls_init(struct net *net, struct nlattr *nla,
161 struct nlattr *est, struct tc_action **a,
162 struct tcf_proto *tp, u32 flags,
163 struct netlink_ext_ack *extack)
165 struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
166 bool bind = flags & TCA_ACT_FLAGS_BIND;
167 struct nlattr *tb[TCA_MPLS_MAX + 1];
168 struct tcf_chain *goto_ch = NULL;
169 struct tcf_mpls_params *p;
170 struct tc_mpls *parm;
178 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
182 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
186 if (!tb[TCA_MPLS_PARMS]) {
187 NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
190 parm = nla_data(tb[TCA_MPLS_PARMS]);
193 /* Verify parameters against action type. */
194 switch (parm->m_action) {
195 case TCA_MPLS_ACT_POP:
196 if (!tb[TCA_MPLS_PROTO]) {
197 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
200 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
201 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
204 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
206 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
210 case TCA_MPLS_ACT_DEC_TTL:
211 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
212 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
213 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
217 case TCA_MPLS_ACT_PUSH:
218 case TCA_MPLS_ACT_MAC_PUSH:
219 if (!tb[TCA_MPLS_LABEL]) {
220 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
223 if (tb[TCA_MPLS_PROTO] &&
224 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
225 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
226 return -EPROTONOSUPPORT;
228 /* Push needs a TTL - if not specified, set a default value. */
229 if (!tb[TCA_MPLS_TTL]) {
230 #if IS_ENABLED(CONFIG_MPLS)
231 mpls_ttl = net->mpls.default_ttl ?
232 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
234 mpls_ttl = ACT_MPLS_TTL_DEFAULT;
238 case TCA_MPLS_ACT_MODIFY:
239 if (tb[TCA_MPLS_PROTO]) {
240 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
245 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
249 err = tcf_idr_check_alloc(tn, &index, a, bind);
257 ret = tcf_idr_create(tn, index, est, a,
258 &act_mpls_ops, bind, true, flags);
260 tcf_idr_cleanup(tn, index);
265 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
266 tcf_idr_release(*a, bind);
270 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
276 p = kzalloc(sizeof(*p), GFP_KERNEL);
282 p->tcfm_action = parm->m_action;
283 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
284 ACT_MPLS_LABEL_NOT_SET;
285 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
287 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
289 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
290 ACT_MPLS_BOS_NOT_SET;
291 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
292 htons(ETH_P_MPLS_UC);
294 spin_lock_bh(&m->tcf_lock);
295 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
296 p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
297 spin_unlock_bh(&m->tcf_lock);
300 tcf_chain_put_by_act(goto_ch);
307 tcf_chain_put_by_act(goto_ch);
309 tcf_idr_release(*a, bind);
313 static void tcf_mpls_cleanup(struct tc_action *a)
315 struct tcf_mpls *m = to_mpls(a);
316 struct tcf_mpls_params *p;
318 p = rcu_dereference_protected(m->mpls_p, 1);
323 static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
326 unsigned char *b = skb_tail_pointer(skb);
327 struct tcf_mpls *m = to_mpls(a);
328 struct tcf_mpls_params *p;
329 struct tc_mpls opt = {
330 .index = m->tcf_index,
331 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
332 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
336 spin_lock_bh(&m->tcf_lock);
337 opt.action = m->tcf_action;
338 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
339 opt.m_action = p->tcfm_action;
341 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
342 goto nla_put_failure;
344 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
345 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
346 goto nla_put_failure;
348 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
349 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
350 goto nla_put_failure;
352 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
353 goto nla_put_failure;
355 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
356 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
357 goto nla_put_failure;
359 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
360 goto nla_put_failure;
362 tcf_tm_dump(&t, &m->tcf_tm);
364 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
365 goto nla_put_failure;
367 spin_unlock_bh(&m->tcf_lock);
372 spin_unlock_bh(&m->tcf_lock);
377 static int tcf_mpls_offload_act_setup(struct tc_action *act, void *entry_data,
378 u32 *index_inc, bool bind,
379 struct netlink_ext_ack *extack)
382 struct flow_action_entry *entry = entry_data;
384 switch (tcf_mpls_action(act)) {
385 case TCA_MPLS_ACT_PUSH:
386 entry->id = FLOW_ACTION_MPLS_PUSH;
387 entry->mpls_push.proto = tcf_mpls_proto(act);
388 entry->mpls_push.label = tcf_mpls_label(act);
389 entry->mpls_push.tc = tcf_mpls_tc(act);
390 entry->mpls_push.bos = tcf_mpls_bos(act);
391 entry->mpls_push.ttl = tcf_mpls_ttl(act);
393 case TCA_MPLS_ACT_POP:
394 entry->id = FLOW_ACTION_MPLS_POP;
395 entry->mpls_pop.proto = tcf_mpls_proto(act);
397 case TCA_MPLS_ACT_MODIFY:
398 entry->id = FLOW_ACTION_MPLS_MANGLE;
399 entry->mpls_mangle.label = tcf_mpls_label(act);
400 entry->mpls_mangle.tc = tcf_mpls_tc(act);
401 entry->mpls_mangle.bos = tcf_mpls_bos(act);
402 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
404 case TCA_MPLS_ACT_DEC_TTL:
405 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"dec_ttl\" option is used");
407 case TCA_MPLS_ACT_MAC_PUSH:
408 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"mac_push\" option is used");
411 NL_SET_ERR_MSG_MOD(extack, "Unsupported MPLS mode offload");
416 struct flow_offload_action *fl_action = entry_data;
418 switch (tcf_mpls_action(act)) {
419 case TCA_MPLS_ACT_PUSH:
420 fl_action->id = FLOW_ACTION_MPLS_PUSH;
422 case TCA_MPLS_ACT_POP:
423 fl_action->id = FLOW_ACTION_MPLS_POP;
425 case TCA_MPLS_ACT_MODIFY:
426 fl_action->id = FLOW_ACTION_MPLS_MANGLE;
436 static struct tc_action_ops act_mpls_ops = {
439 .owner = THIS_MODULE,
441 .dump = tcf_mpls_dump,
442 .init = tcf_mpls_init,
443 .cleanup = tcf_mpls_cleanup,
444 .offload_act_setup = tcf_mpls_offload_act_setup,
445 .size = sizeof(struct tcf_mpls),
448 static __net_init int mpls_init_net(struct net *net)
450 struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
452 return tc_action_net_init(net, tn, &act_mpls_ops);
455 static void __net_exit mpls_exit_net(struct list_head *net_list)
457 tc_action_net_exit(net_list, act_mpls_ops.net_id);
460 static struct pernet_operations mpls_net_ops = {
461 .init = mpls_init_net,
462 .exit_batch = mpls_exit_net,
463 .id = &act_mpls_ops.net_id,
464 .size = sizeof(struct tc_action_net),
467 static int __init mpls_init_module(void)
469 return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
472 static void __exit mpls_cleanup_module(void)
474 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
477 module_init(mpls_init_module);
478 module_exit(mpls_cleanup_module);
480 MODULE_SOFTDEP("post: mpls_gso");
481 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
482 MODULE_LICENSE("GPL");
483 MODULE_DESCRIPTION("MPLS manipulation actions");