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>
18 static unsigned int mpls_net_id
;
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 static int tcf_mpls_act(struct sk_buff
*skb
, const struct tc_action
*a
,
54 struct tcf_result
*res
)
56 struct tcf_mpls
*m
= to_mpls(a
);
57 struct tcf_mpls_params
*p
;
61 tcf_lastuse_update(&m
->tcf_tm
);
62 bstats_cpu_update(this_cpu_ptr(m
->common
.cpu_bstats
), skb
);
64 /* Ensure 'data' points at mac_header prior calling mpls manipulating
67 if (skb_at_tc_ingress(skb
)) {
68 skb_push_rcsum(skb
, skb
->mac_len
);
69 mac_len
= skb
->mac_len
;
71 mac_len
= skb_network_header(skb
) - skb_mac_header(skb
);
74 ret
= READ_ONCE(m
->tcf_action
);
76 p
= rcu_dereference_bh(m
->mpls_p
);
78 switch (p
->tcfm_action
) {
79 case TCA_MPLS_ACT_POP
:
80 if (skb_mpls_pop(skb
, p
->tcfm_proto
, mac_len
,
81 skb
->dev
&& skb
->dev
->type
== ARPHRD_ETHER
))
84 case TCA_MPLS_ACT_PUSH
:
85 new_lse
= tcf_mpls_get_lse(NULL
, p
, !eth_p_mpls(skb
->protocol
));
86 if (skb_mpls_push(skb
, new_lse
, p
->tcfm_proto
, mac_len
,
87 skb
->dev
&& skb
->dev
->type
== ARPHRD_ETHER
))
90 case TCA_MPLS_ACT_MODIFY
:
91 new_lse
= tcf_mpls_get_lse(mpls_hdr(skb
), p
, false);
92 if (skb_mpls_update_lse(skb
, new_lse
))
95 case TCA_MPLS_ACT_DEC_TTL
:
96 if (skb_mpls_dec_ttl(skb
))
101 if (skb_at_tc_ingress(skb
))
102 skb_pull_rcsum(skb
, skb
->mac_len
);
107 qstats_drop_inc(this_cpu_ptr(m
->common
.cpu_qstats
));
111 static int valid_label(const struct nlattr
*attr
,
112 struct netlink_ext_ack
*extack
)
114 const u32
*label
= nla_data(attr
);
116 if (*label
& ~MPLS_LABEL_MASK
|| *label
== MPLS_LABEL_IMPLNULL
) {
117 NL_SET_ERR_MSG_MOD(extack
, "MPLS label out of range");
124 static const struct nla_policy mpls_policy
[TCA_MPLS_MAX
+ 1] = {
125 [TCA_MPLS_PARMS
] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls
)),
126 [TCA_MPLS_PROTO
] = { .type
= NLA_U16
},
127 [TCA_MPLS_LABEL
] = NLA_POLICY_VALIDATE_FN(NLA_U32
, valid_label
),
128 [TCA_MPLS_TC
] = NLA_POLICY_RANGE(NLA_U8
, 0, 7),
129 [TCA_MPLS_TTL
] = NLA_POLICY_MIN(NLA_U8
, 1),
130 [TCA_MPLS_BOS
] = NLA_POLICY_RANGE(NLA_U8
, 0, 1),
133 static int tcf_mpls_init(struct net
*net
, struct nlattr
*nla
,
134 struct nlattr
*est
, struct tc_action
**a
,
135 int ovr
, int bind
, bool rtnl_held
,
136 struct tcf_proto
*tp
, u32 flags
,
137 struct netlink_ext_ack
*extack
)
139 struct tc_action_net
*tn
= net_generic(net
, mpls_net_id
);
140 struct nlattr
*tb
[TCA_MPLS_MAX
+ 1];
141 struct tcf_chain
*goto_ch
= NULL
;
142 struct tcf_mpls_params
*p
;
143 struct tc_mpls
*parm
;
151 NL_SET_ERR_MSG_MOD(extack
, "Missing netlink attributes");
155 err
= nla_parse_nested(tb
, TCA_MPLS_MAX
, nla
, mpls_policy
, extack
);
159 if (!tb
[TCA_MPLS_PARMS
]) {
160 NL_SET_ERR_MSG_MOD(extack
, "No MPLS params");
163 parm
= nla_data(tb
[TCA_MPLS_PARMS
]);
166 /* Verify parameters against action type. */
167 switch (parm
->m_action
) {
168 case TCA_MPLS_ACT_POP
:
169 if (!tb
[TCA_MPLS_PROTO
]) {
170 NL_SET_ERR_MSG_MOD(extack
, "Protocol must be set for MPLS pop");
173 if (!eth_proto_is_802_3(nla_get_be16(tb
[TCA_MPLS_PROTO
]))) {
174 NL_SET_ERR_MSG_MOD(extack
, "Invalid protocol type for MPLS pop");
177 if (tb
[TCA_MPLS_LABEL
] || tb
[TCA_MPLS_TTL
] || tb
[TCA_MPLS_TC
] ||
179 NL_SET_ERR_MSG_MOD(extack
, "Label, TTL, TC or BOS cannot be used with MPLS pop");
183 case TCA_MPLS_ACT_DEC_TTL
:
184 if (tb
[TCA_MPLS_PROTO
] || tb
[TCA_MPLS_LABEL
] ||
185 tb
[TCA_MPLS_TTL
] || tb
[TCA_MPLS_TC
] || tb
[TCA_MPLS_BOS
]) {
186 NL_SET_ERR_MSG_MOD(extack
, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
190 case TCA_MPLS_ACT_PUSH
:
191 if (!tb
[TCA_MPLS_LABEL
]) {
192 NL_SET_ERR_MSG_MOD(extack
, "Label is required for MPLS push");
195 if (tb
[TCA_MPLS_PROTO
] &&
196 !eth_p_mpls(nla_get_be16(tb
[TCA_MPLS_PROTO
]))) {
197 NL_SET_ERR_MSG_MOD(extack
, "Protocol must be an MPLS type for MPLS push");
198 return -EPROTONOSUPPORT
;
200 /* Push needs a TTL - if not specified, set a default value. */
201 if (!tb
[TCA_MPLS_TTL
]) {
202 #if IS_ENABLED(CONFIG_MPLS)
203 mpls_ttl
= net
->mpls
.default_ttl
?
204 net
->mpls
.default_ttl
: ACT_MPLS_TTL_DEFAULT
;
206 mpls_ttl
= ACT_MPLS_TTL_DEFAULT
;
210 case TCA_MPLS_ACT_MODIFY
:
211 if (tb
[TCA_MPLS_PROTO
]) {
212 NL_SET_ERR_MSG_MOD(extack
, "Protocol cannot be used with MPLS modify");
217 NL_SET_ERR_MSG_MOD(extack
, "Unknown MPLS action");
221 err
= tcf_idr_check_alloc(tn
, &index
, a
, bind
);
229 ret
= tcf_idr_create(tn
, index
, est
, a
,
230 &act_mpls_ops
, bind
, true, 0);
232 tcf_idr_cleanup(tn
, index
);
238 tcf_idr_release(*a
, bind
);
242 err
= tcf_action_check_ctrlact(parm
->action
, tp
, &goto_ch
, extack
);
248 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
254 p
->tcfm_action
= parm
->m_action
;
255 p
->tcfm_label
= tb
[TCA_MPLS_LABEL
] ? nla_get_u32(tb
[TCA_MPLS_LABEL
]) :
256 ACT_MPLS_LABEL_NOT_SET
;
257 p
->tcfm_tc
= tb
[TCA_MPLS_TC
] ? nla_get_u8(tb
[TCA_MPLS_TC
]) :
259 p
->tcfm_ttl
= tb
[TCA_MPLS_TTL
] ? nla_get_u8(tb
[TCA_MPLS_TTL
]) :
261 p
->tcfm_bos
= tb
[TCA_MPLS_BOS
] ? nla_get_u8(tb
[TCA_MPLS_BOS
]) :
262 ACT_MPLS_BOS_NOT_SET
;
263 p
->tcfm_proto
= tb
[TCA_MPLS_PROTO
] ? nla_get_be16(tb
[TCA_MPLS_PROTO
]) :
264 htons(ETH_P_MPLS_UC
);
266 spin_lock_bh(&m
->tcf_lock
);
267 goto_ch
= tcf_action_set_ctrlact(*a
, parm
->action
, goto_ch
);
268 p
= rcu_replace_pointer(m
->mpls_p
, p
, lockdep_is_held(&m
->tcf_lock
));
269 spin_unlock_bh(&m
->tcf_lock
);
272 tcf_chain_put_by_act(goto_ch
);
276 if (ret
== ACT_P_CREATED
)
277 tcf_idr_insert(tn
, *a
);
281 tcf_chain_put_by_act(goto_ch
);
283 tcf_idr_release(*a
, bind
);
287 static void tcf_mpls_cleanup(struct tc_action
*a
)
289 struct tcf_mpls
*m
= to_mpls(a
);
290 struct tcf_mpls_params
*p
;
292 p
= rcu_dereference_protected(m
->mpls_p
, 1);
297 static int tcf_mpls_dump(struct sk_buff
*skb
, struct tc_action
*a
,
300 unsigned char *b
= skb_tail_pointer(skb
);
301 struct tcf_mpls
*m
= to_mpls(a
);
302 struct tcf_mpls_params
*p
;
303 struct tc_mpls opt
= {
304 .index
= m
->tcf_index
,
305 .refcnt
= refcount_read(&m
->tcf_refcnt
) - ref
,
306 .bindcnt
= atomic_read(&m
->tcf_bindcnt
) - bind
,
310 spin_lock_bh(&m
->tcf_lock
);
311 opt
.action
= m
->tcf_action
;
312 p
= rcu_dereference_protected(m
->mpls_p
, lockdep_is_held(&m
->tcf_lock
));
313 opt
.m_action
= p
->tcfm_action
;
315 if (nla_put(skb
, TCA_MPLS_PARMS
, sizeof(opt
), &opt
))
316 goto nla_put_failure
;
318 if (p
->tcfm_label
!= ACT_MPLS_LABEL_NOT_SET
&&
319 nla_put_u32(skb
, TCA_MPLS_LABEL
, p
->tcfm_label
))
320 goto nla_put_failure
;
322 if (p
->tcfm_tc
!= ACT_MPLS_TC_NOT_SET
&&
323 nla_put_u8(skb
, TCA_MPLS_TC
, p
->tcfm_tc
))
324 goto nla_put_failure
;
326 if (p
->tcfm_ttl
&& nla_put_u8(skb
, TCA_MPLS_TTL
, p
->tcfm_ttl
))
327 goto nla_put_failure
;
329 if (p
->tcfm_bos
!= ACT_MPLS_BOS_NOT_SET
&&
330 nla_put_u8(skb
, TCA_MPLS_BOS
, p
->tcfm_bos
))
331 goto nla_put_failure
;
333 if (nla_put_be16(skb
, TCA_MPLS_PROTO
, p
->tcfm_proto
))
334 goto nla_put_failure
;
336 tcf_tm_dump(&t
, &m
->tcf_tm
);
338 if (nla_put_64bit(skb
, TCA_MPLS_TM
, sizeof(t
), &t
, TCA_MPLS_PAD
))
339 goto nla_put_failure
;
341 spin_unlock_bh(&m
->tcf_lock
);
346 spin_unlock_bh(&m
->tcf_lock
);
351 static int tcf_mpls_walker(struct net
*net
, struct sk_buff
*skb
,
352 struct netlink_callback
*cb
, int type
,
353 const struct tc_action_ops
*ops
,
354 struct netlink_ext_ack
*extack
)
356 struct tc_action_net
*tn
= net_generic(net
, mpls_net_id
);
358 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
361 static int tcf_mpls_search(struct net
*net
, struct tc_action
**a
, u32 index
)
363 struct tc_action_net
*tn
= net_generic(net
, mpls_net_id
);
365 return tcf_idr_search(tn
, a
, index
);
368 static struct tc_action_ops act_mpls_ops
= {
371 .owner
= THIS_MODULE
,
373 .dump
= tcf_mpls_dump
,
374 .init
= tcf_mpls_init
,
375 .cleanup
= tcf_mpls_cleanup
,
376 .walk
= tcf_mpls_walker
,
377 .lookup
= tcf_mpls_search
,
378 .size
= sizeof(struct tcf_mpls
),
381 static __net_init
int mpls_init_net(struct net
*net
)
383 struct tc_action_net
*tn
= net_generic(net
, mpls_net_id
);
385 return tc_action_net_init(net
, tn
, &act_mpls_ops
);
388 static void __net_exit
mpls_exit_net(struct list_head
*net_list
)
390 tc_action_net_exit(net_list
, mpls_net_id
);
393 static struct pernet_operations mpls_net_ops
= {
394 .init
= mpls_init_net
,
395 .exit_batch
= mpls_exit_net
,
397 .size
= sizeof(struct tc_action_net
),
400 static int __init
mpls_init_module(void)
402 return tcf_register_action(&act_mpls_ops
, &mpls_net_ops
);
405 static void __exit
mpls_cleanup_module(void)
407 tcf_unregister_action(&act_mpls_ops
, &mpls_net_ops
);
410 module_init(mpls_init_module
);
411 module_exit(mpls_cleanup_module
);
413 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
414 MODULE_LICENSE("GPL");
415 MODULE_DESCRIPTION("MPLS manipulation actions");