Merge tag 'regmap-v5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.git] / net / sched / act_mpls.c
CommitLineData
2a2ea508
JH
1// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Netronome Systems, Inc. */
3
4#include <linux/init.h>
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/mpls.h>
8#include <linux/rtnetlink.h>
9#include <linux/skbuff.h>
10#include <linux/tc_act/tc_mpls.h>
11#include <net/mpls.h>
12#include <net/netlink.h>
13#include <net/pkt_sched.h>
14#include <net/pkt_cls.h>
15#include <net/tc_act/tc_mpls.h>
16
17static unsigned int mpls_net_id;
18static struct tc_action_ops act_mpls_ops;
19
20#define ACT_MPLS_TTL_DEFAULT 255
21
22static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
23 struct tcf_mpls_params *p, bool set_bos)
24{
25 u32 new_lse = 0;
26
27 if (lse)
28 new_lse = be32_to_cpu(lse->label_stack_entry);
29
30 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
31 new_lse &= ~MPLS_LS_LABEL_MASK;
32 new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
33 }
34 if (p->tcfm_ttl) {
35 new_lse &= ~MPLS_LS_TTL_MASK;
36 new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
37 }
38 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
39 new_lse &= ~MPLS_LS_TC_MASK;
40 new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
41 }
42 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
43 new_lse &= ~MPLS_LS_S_MASK;
44 new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
45 } else if (set_bos) {
46 new_lse |= 1 << MPLS_LS_S_SHIFT;
47 }
48
49 return cpu_to_be32(new_lse);
50}
51
52static int tcf_mpls_act(struct sk_buff *skb, const struct tc_action *a,
53 struct tcf_result *res)
54{
55 struct tcf_mpls *m = to_mpls(a);
56 struct tcf_mpls_params *p;
57 __be32 new_lse;
58 int ret;
59
60 tcf_lastuse_update(&m->tcf_tm);
61 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
62
63 /* Ensure 'data' points at mac_header prior calling mpls manipulating
64 * functions.
65 */
66 if (skb_at_tc_ingress(skb))
67 skb_push_rcsum(skb, skb->mac_len);
68
69 ret = READ_ONCE(m->tcf_action);
70
71 p = rcu_dereference_bh(m->mpls_p);
72
73 switch (p->tcfm_action) {
74 case TCA_MPLS_ACT_POP:
75 if (skb_mpls_pop(skb, p->tcfm_proto))
76 goto drop;
77 break;
78 case TCA_MPLS_ACT_PUSH:
79 new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb->protocol));
80 if (skb_mpls_push(skb, new_lse, p->tcfm_proto))
81 goto drop;
82 break;
83 case TCA_MPLS_ACT_MODIFY:
84 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
85 if (skb_mpls_update_lse(skb, new_lse))
86 goto drop;
87 break;
88 case TCA_MPLS_ACT_DEC_TTL:
89 if (skb_mpls_dec_ttl(skb))
90 goto drop;
91 break;
92 }
93
94 if (skb_at_tc_ingress(skb))
95 skb_pull_rcsum(skb, skb->mac_len);
96
97 return ret;
98
99drop:
100 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
101 return TC_ACT_SHOT;
102}
103
104static int valid_label(const struct nlattr *attr,
105 struct netlink_ext_ack *extack)
106{
107 const u32 *label = nla_data(attr);
108
109 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
110 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
111 return -EINVAL;
112 }
113
114 return 0;
115}
116
117static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
118 [TCA_MPLS_UNSPEC] = { .strict_start_type = TCA_MPLS_UNSPEC + 1 },
119 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
120 [TCA_MPLS_PROTO] = { .type = NLA_U16 },
121 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_U32, valid_label),
122 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
123 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
124 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
125};
126
127static int tcf_mpls_init(struct net *net, struct nlattr *nla,
128 struct nlattr *est, struct tc_action **a,
129 int ovr, int bind, bool rtnl_held,
130 struct tcf_proto *tp, struct netlink_ext_ack *extack)
131{
132 struct tc_action_net *tn = net_generic(net, mpls_net_id);
133 struct nlattr *tb[TCA_MPLS_MAX + 1];
134 struct tcf_chain *goto_ch = NULL;
135 struct tcf_mpls_params *p;
136 struct tc_mpls *parm;
137 bool exists = false;
138 struct tcf_mpls *m;
139 int ret = 0, err;
140 u8 mpls_ttl = 0;
7be8ef2c 141 u32 index;
2a2ea508
JH
142
143 if (!nla) {
144 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
145 return -EINVAL;
146 }
147
148 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
149 if (err < 0)
150 return err;
151
152 if (!tb[TCA_MPLS_PARMS]) {
153 NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
154 return -EINVAL;
155 }
156 parm = nla_data(tb[TCA_MPLS_PARMS]);
7be8ef2c 157 index = parm->index;
2a2ea508
JH
158
159 /* Verify parameters against action type. */
160 switch (parm->m_action) {
161 case TCA_MPLS_ACT_POP:
162 if (!tb[TCA_MPLS_PROTO]) {
163 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
164 return -EINVAL;
165 }
166 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
167 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
168 return -EINVAL;
169 }
170 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
171 tb[TCA_MPLS_BOS]) {
172 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
173 return -EINVAL;
174 }
175 break;
176 case TCA_MPLS_ACT_DEC_TTL:
177 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
178 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
179 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
180 return -EINVAL;
181 }
182 break;
183 case TCA_MPLS_ACT_PUSH:
184 if (!tb[TCA_MPLS_LABEL]) {
185 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
186 return -EINVAL;
187 }
188 if (tb[TCA_MPLS_PROTO] &&
189 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
190 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
191 return -EPROTONOSUPPORT;
192 }
193 /* Push needs a TTL - if not specified, set a default value. */
194 if (!tb[TCA_MPLS_TTL]) {
195#if IS_ENABLED(CONFIG_MPLS)
196 mpls_ttl = net->mpls.default_ttl ?
197 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
198#else
199 mpls_ttl = ACT_MPLS_TTL_DEFAULT;
200#endif
201 }
202 break;
203 case TCA_MPLS_ACT_MODIFY:
204 if (tb[TCA_MPLS_PROTO]) {
205 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
206 return -EINVAL;
207 }
208 break;
209 default:
210 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
211 return -EINVAL;
212 }
213
7be8ef2c 214 err = tcf_idr_check_alloc(tn, &index, a, bind);
2a2ea508
JH
215 if (err < 0)
216 return err;
217 exists = err;
218 if (exists && bind)
219 return 0;
220
221 if (!exists) {
7be8ef2c 222 ret = tcf_idr_create(tn, index, est, a,
2a2ea508
JH
223 &act_mpls_ops, bind, true);
224 if (ret) {
7be8ef2c 225 tcf_idr_cleanup(tn, index);
2a2ea508
JH
226 return ret;
227 }
228
229 ret = ACT_P_CREATED;
230 } else if (!ovr) {
231 tcf_idr_release(*a, bind);
232 return -EEXIST;
233 }
234
235 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
236 if (err < 0)
237 goto release_idr;
238
239 m = to_mpls(*a);
240
241 p = kzalloc(sizeof(*p), GFP_KERNEL);
242 if (!p) {
243 err = -ENOMEM;
244 goto put_chain;
245 }
246
247 p->tcfm_action = parm->m_action;
248 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
249 ACT_MPLS_LABEL_NOT_SET;
250 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
251 ACT_MPLS_TC_NOT_SET;
252 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
253 mpls_ttl;
254 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
255 ACT_MPLS_BOS_NOT_SET;
256 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
257 htons(ETH_P_MPLS_UC);
258
259 spin_lock_bh(&m->tcf_lock);
260 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
261 rcu_swap_protected(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
262 spin_unlock_bh(&m->tcf_lock);
263
264 if (goto_ch)
265 tcf_chain_put_by_act(goto_ch);
266 if (p)
267 kfree_rcu(p, rcu);
268
269 if (ret == ACT_P_CREATED)
270 tcf_idr_insert(tn, *a);
271 return ret;
272put_chain:
273 if (goto_ch)
274 tcf_chain_put_by_act(goto_ch);
275release_idr:
276 tcf_idr_release(*a, bind);
277 return err;
278}
279
280static void tcf_mpls_cleanup(struct tc_action *a)
281{
282 struct tcf_mpls *m = to_mpls(a);
283 struct tcf_mpls_params *p;
284
285 p = rcu_dereference_protected(m->mpls_p, 1);
286 if (p)
287 kfree_rcu(p, rcu);
288}
289
290static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
291 int bind, int ref)
292{
293 unsigned char *b = skb_tail_pointer(skb);
294 struct tcf_mpls *m = to_mpls(a);
295 struct tcf_mpls_params *p;
296 struct tc_mpls opt = {
297 .index = m->tcf_index,
298 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
299 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
300 };
301 struct tcf_t t;
302
303 spin_lock_bh(&m->tcf_lock);
304 opt.action = m->tcf_action;
305 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
306 opt.m_action = p->tcfm_action;
307
308 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
309 goto nla_put_failure;
310
311 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
312 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
313 goto nla_put_failure;
314
315 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
316 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
317 goto nla_put_failure;
318
319 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
320 goto nla_put_failure;
321
322 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
323 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
324 goto nla_put_failure;
325
326 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
327 goto nla_put_failure;
328
329 tcf_tm_dump(&t, &m->tcf_tm);
330
331 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
332 goto nla_put_failure;
333
334 spin_unlock_bh(&m->tcf_lock);
335
336 return skb->len;
337
338nla_put_failure:
339 spin_unlock_bh(&m->tcf_lock);
340 nlmsg_trim(skb, b);
341 return -EMSGSIZE;
342}
343
344static int tcf_mpls_walker(struct net *net, struct sk_buff *skb,
345 struct netlink_callback *cb, int type,
346 const struct tc_action_ops *ops,
347 struct netlink_ext_ack *extack)
348{
349 struct tc_action_net *tn = net_generic(net, mpls_net_id);
350
351 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
352}
353
354static int tcf_mpls_search(struct net *net, struct tc_action **a, u32 index)
355{
356 struct tc_action_net *tn = net_generic(net, mpls_net_id);
357
358 return tcf_idr_search(tn, a, index);
359}
360
361static struct tc_action_ops act_mpls_ops = {
362 .kind = "mpls",
363 .id = TCA_ID_MPLS,
364 .owner = THIS_MODULE,
365 .act = tcf_mpls_act,
366 .dump = tcf_mpls_dump,
367 .init = tcf_mpls_init,
368 .cleanup = tcf_mpls_cleanup,
369 .walk = tcf_mpls_walker,
370 .lookup = tcf_mpls_search,
371 .size = sizeof(struct tcf_mpls),
372};
373
374static __net_init int mpls_init_net(struct net *net)
375{
376 struct tc_action_net *tn = net_generic(net, mpls_net_id);
377
981471bd 378 return tc_action_net_init(net, tn, &act_mpls_ops);
2a2ea508
JH
379}
380
381static void __net_exit mpls_exit_net(struct list_head *net_list)
382{
383 tc_action_net_exit(net_list, mpls_net_id);
384}
385
386static struct pernet_operations mpls_net_ops = {
387 .init = mpls_init_net,
388 .exit_batch = mpls_exit_net,
389 .id = &mpls_net_id,
390 .size = sizeof(struct tc_action_net),
391};
392
393static int __init mpls_init_module(void)
394{
395 return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
396}
397
398static void __exit mpls_cleanup_module(void)
399{
400 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
401}
402
403module_init(mpls_init_module);
404module_exit(mpls_cleanup_module);
405
406MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
407MODULE_LICENSE("GPL");
408MODULE_DESCRIPTION("MPLS manipulation actions");