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