blob: 0fccae356dc140850f2d148b49187e37756de64f [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2/* Copyright (C) 2019 Netronome Systems, Inc. */
3
Olivier Deprez0e641232021-09-23 10:07:05 +02004#include <linux/if_arp.h>
David Brazdil0f672f62019-12-10 10:32:29 +00005#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;
59 int ret, mac_len;
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 */
67 if (skb_at_tc_ingress(skb)) {
68 skb_push_rcsum(skb, skb->mac_len);
69 mac_len = skb->mac_len;
70 } else {
71 mac_len = skb_network_header(skb) - skb_mac_header(skb);
72 }
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:
Olivier Deprez0e641232021-09-23 10:07:05 +020080 if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
81 skb->dev && skb->dev->type == ARPHRD_ETHER))
David Brazdil0f672f62019-12-10 10:32:29 +000082 goto drop;
83 break;
84 case TCA_MPLS_ACT_PUSH:
Olivier Deprez0e641232021-09-23 10:07:05 +020085 new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
86 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
87 skb->dev && skb->dev->type == ARPHRD_ETHER))
David Brazdil0f672f62019-12-10 10:32:29 +000088 goto drop;
89 break;
90 case TCA_MPLS_ACT_MODIFY:
Olivier Deprez0e641232021-09-23 10:07:05 +020091 if (!pskb_may_pull(skb,
92 skb_network_offset(skb) + MPLS_HLEN))
93 goto drop;
David Brazdil0f672f62019-12-10 10:32:29 +000094 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
95 if (skb_mpls_update_lse(skb, new_lse))
96 goto drop;
97 break;
98 case TCA_MPLS_ACT_DEC_TTL:
99 if (skb_mpls_dec_ttl(skb))
100 goto drop;
101 break;
102 }
103
104 if (skb_at_tc_ingress(skb))
105 skb_pull_rcsum(skb, skb->mac_len);
106
107 return ret;
108
109drop:
110 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
111 return TC_ACT_SHOT;
112}
113
114static int valid_label(const struct nlattr *attr,
115 struct netlink_ext_ack *extack)
116{
117 const u32 *label = nla_data(attr);
118
119 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
120 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
121 return -EINVAL;
122 }
123
124 return 0;
125}
126
127static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
128 [TCA_MPLS_UNSPEC] = { .strict_start_type = TCA_MPLS_UNSPEC + 1 },
129 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
130 [TCA_MPLS_PROTO] = { .type = NLA_U16 },
131 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_U32, valid_label),
132 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
133 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
134 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
135};
136
137static int tcf_mpls_init(struct net *net, struct nlattr *nla,
138 struct nlattr *est, struct tc_action **a,
139 int ovr, int bind, bool rtnl_held,
140 struct tcf_proto *tp, struct netlink_ext_ack *extack)
141{
142 struct tc_action_net *tn = net_generic(net, mpls_net_id);
143 struct nlattr *tb[TCA_MPLS_MAX + 1];
144 struct tcf_chain *goto_ch = NULL;
145 struct tcf_mpls_params *p;
146 struct tc_mpls *parm;
147 bool exists = false;
148 struct tcf_mpls *m;
149 int ret = 0, err;
150 u8 mpls_ttl = 0;
151 u32 index;
152
153 if (!nla) {
154 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
155 return -EINVAL;
156 }
157
158 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
159 if (err < 0)
160 return err;
161
162 if (!tb[TCA_MPLS_PARMS]) {
163 NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
164 return -EINVAL;
165 }
166 parm = nla_data(tb[TCA_MPLS_PARMS]);
167 index = parm->index;
168
169 /* Verify parameters against action type. */
170 switch (parm->m_action) {
171 case TCA_MPLS_ACT_POP:
172 if (!tb[TCA_MPLS_PROTO]) {
173 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
174 return -EINVAL;
175 }
176 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
177 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
178 return -EINVAL;
179 }
180 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
181 tb[TCA_MPLS_BOS]) {
182 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
183 return -EINVAL;
184 }
185 break;
186 case TCA_MPLS_ACT_DEC_TTL:
187 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
188 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
189 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
190 return -EINVAL;
191 }
192 break;
193 case TCA_MPLS_ACT_PUSH:
194 if (!tb[TCA_MPLS_LABEL]) {
195 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
196 return -EINVAL;
197 }
198 if (tb[TCA_MPLS_PROTO] &&
199 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
200 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
201 return -EPROTONOSUPPORT;
202 }
203 /* Push needs a TTL - if not specified, set a default value. */
204 if (!tb[TCA_MPLS_TTL]) {
205#if IS_ENABLED(CONFIG_MPLS)
206 mpls_ttl = net->mpls.default_ttl ?
207 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
208#else
209 mpls_ttl = ACT_MPLS_TTL_DEFAULT;
210#endif
211 }
212 break;
213 case TCA_MPLS_ACT_MODIFY:
214 if (tb[TCA_MPLS_PROTO]) {
215 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
216 return -EINVAL;
217 }
218 break;
219 default:
220 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
221 return -EINVAL;
222 }
223
224 err = tcf_idr_check_alloc(tn, &index, a, bind);
225 if (err < 0)
226 return err;
227 exists = err;
228 if (exists && bind)
229 return 0;
230
231 if (!exists) {
232 ret = tcf_idr_create(tn, index, est, a,
233 &act_mpls_ops, bind, true);
234 if (ret) {
235 tcf_idr_cleanup(tn, index);
236 return ret;
237 }
238
239 ret = ACT_P_CREATED;
240 } else if (!ovr) {
241 tcf_idr_release(*a, bind);
242 return -EEXIST;
243 }
244
245 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
246 if (err < 0)
247 goto release_idr;
248
249 m = to_mpls(*a);
250
251 p = kzalloc(sizeof(*p), GFP_KERNEL);
252 if (!p) {
253 err = -ENOMEM;
254 goto put_chain;
255 }
256
257 p->tcfm_action = parm->m_action;
258 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
259 ACT_MPLS_LABEL_NOT_SET;
260 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
261 ACT_MPLS_TC_NOT_SET;
262 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
263 mpls_ttl;
264 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
265 ACT_MPLS_BOS_NOT_SET;
266 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
267 htons(ETH_P_MPLS_UC);
268
269 spin_lock_bh(&m->tcf_lock);
270 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
271 rcu_swap_protected(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
272 spin_unlock_bh(&m->tcf_lock);
273
274 if (goto_ch)
275 tcf_chain_put_by_act(goto_ch);
276 if (p)
277 kfree_rcu(p, rcu);
278
David Brazdil0f672f62019-12-10 10:32:29 +0000279 return ret;
280put_chain:
281 if (goto_ch)
282 tcf_chain_put_by_act(goto_ch);
283release_idr:
284 tcf_idr_release(*a, bind);
285 return err;
286}
287
288static void tcf_mpls_cleanup(struct tc_action *a)
289{
290 struct tcf_mpls *m = to_mpls(a);
291 struct tcf_mpls_params *p;
292
293 p = rcu_dereference_protected(m->mpls_p, 1);
294 if (p)
295 kfree_rcu(p, rcu);
296}
297
298static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
299 int bind, int ref)
300{
301 unsigned char *b = skb_tail_pointer(skb);
302 struct tcf_mpls *m = to_mpls(a);
303 struct tcf_mpls_params *p;
304 struct tc_mpls opt = {
305 .index = m->tcf_index,
306 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
307 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
308 };
309 struct tcf_t t;
310
311 spin_lock_bh(&m->tcf_lock);
312 opt.action = m->tcf_action;
313 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
314 opt.m_action = p->tcfm_action;
315
316 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
317 goto nla_put_failure;
318
319 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
320 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
321 goto nla_put_failure;
322
323 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
324 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
325 goto nla_put_failure;
326
327 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
328 goto nla_put_failure;
329
330 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
331 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
332 goto nla_put_failure;
333
334 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
335 goto nla_put_failure;
336
337 tcf_tm_dump(&t, &m->tcf_tm);
338
339 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
340 goto nla_put_failure;
341
342 spin_unlock_bh(&m->tcf_lock);
343
344 return skb->len;
345
346nla_put_failure:
347 spin_unlock_bh(&m->tcf_lock);
348 nlmsg_trim(skb, b);
349 return -EMSGSIZE;
350}
351
352static int tcf_mpls_walker(struct net *net, struct sk_buff *skb,
353 struct netlink_callback *cb, int type,
354 const struct tc_action_ops *ops,
355 struct netlink_ext_ack *extack)
356{
357 struct tc_action_net *tn = net_generic(net, mpls_net_id);
358
359 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
360}
361
362static int tcf_mpls_search(struct net *net, struct tc_action **a, u32 index)
363{
364 struct tc_action_net *tn = net_generic(net, mpls_net_id);
365
366 return tcf_idr_search(tn, a, index);
367}
368
369static struct tc_action_ops act_mpls_ops = {
370 .kind = "mpls",
371 .id = TCA_ID_MPLS,
372 .owner = THIS_MODULE,
373 .act = tcf_mpls_act,
374 .dump = tcf_mpls_dump,
375 .init = tcf_mpls_init,
376 .cleanup = tcf_mpls_cleanup,
377 .walk = tcf_mpls_walker,
378 .lookup = tcf_mpls_search,
379 .size = sizeof(struct tcf_mpls),
380};
381
382static __net_init int mpls_init_net(struct net *net)
383{
384 struct tc_action_net *tn = net_generic(net, mpls_net_id);
385
386 return tc_action_net_init(net, tn, &act_mpls_ops);
387}
388
389static void __net_exit mpls_exit_net(struct list_head *net_list)
390{
391 tc_action_net_exit(net_list, mpls_net_id);
392}
393
394static struct pernet_operations mpls_net_ops = {
395 .init = mpls_init_net,
396 .exit_batch = mpls_exit_net,
397 .id = &mpls_net_id,
398 .size = sizeof(struct tc_action_net),
399};
400
401static int __init mpls_init_module(void)
402{
403 return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
404}
405
406static void __exit mpls_cleanup_module(void)
407{
408 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
409}
410
411module_init(mpls_init_module);
412module_exit(mpls_cleanup_module);
413
Olivier Deprez0e641232021-09-23 10:07:05 +0200414MODULE_SOFTDEP("post: mpls_gso");
David Brazdil0f672f62019-12-10 10:32:29 +0000415MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
416MODULE_LICENSE("GPL");
417MODULE_DESCRIPTION("MPLS manipulation actions");