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