1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
3
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>
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
18 static struct tc_action_ops act_mpls_ops;
19
20 #define ACT_MPLS_TTL_DEFAULT 255
21
tcf_mpls_get_lse(struct mpls_shim_hdr * lse,struct tcf_mpls_params * p,bool set_bos)22 static __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
tcf_mpls_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)52 static 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, mac_len;
59
60 tcf_lastuse_update(&m->tcf_tm);
61 bstats_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 mac_len = skb->mac_len;
69 } else {
70 mac_len = skb_network_header(skb) - skb_mac_header(skb);
71 }
72
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:
79 if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
80 skb->dev && skb->dev->type == ARPHRD_ETHER))
81 goto drop;
82 break;
83 case TCA_MPLS_ACT_PUSH:
84 new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
85 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
86 skb->dev && skb->dev->type == ARPHRD_ETHER))
87 goto drop;
88 break;
89 case TCA_MPLS_ACT_MAC_PUSH:
90 if (skb_vlan_tag_present(skb)) {
91 if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
92 skb_vlan_tag_get(skb),
93 ETH_HLEN) < 0)
94 goto drop;
95
96 skb->protocol = skb->vlan_proto;
97 __vlan_hwaccel_clear_tag(skb);
98 }
99
100 new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
101 !eth_p_mpls(skb->protocol));
102
103 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
104 goto drop;
105 break;
106 case TCA_MPLS_ACT_MODIFY:
107 if (!pskb_may_pull(skb,
108 skb_network_offset(skb) + MPLS_HLEN))
109 goto drop;
110 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
111 if (skb_mpls_update_lse(skb, new_lse))
112 goto drop;
113 break;
114 case TCA_MPLS_ACT_DEC_TTL:
115 if (skb_mpls_dec_ttl(skb))
116 goto drop;
117 break;
118 }
119
120 if (skb_at_tc_ingress(skb))
121 skb_pull_rcsum(skb, skb->mac_len);
122
123 return ret;
124
125 drop:
126 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
127 return TC_ACT_SHOT;
128 }
129
valid_label(const struct nlattr * attr,struct netlink_ext_ack * extack)130 static int valid_label(const struct nlattr *attr,
131 struct netlink_ext_ack *extack)
132 {
133 const u32 *label = nla_data(attr);
134
135 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
136 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
137 return -EINVAL;
138 }
139
140 return 0;
141 }
142
143 static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
144 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
145 [TCA_MPLS_PROTO] = { .type = NLA_U16 },
146 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_U32, valid_label),
147 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
148 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
149 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
150 };
151
tcf_mpls_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)152 static int tcf_mpls_init(struct net *net, struct nlattr *nla,
153 struct nlattr *est, struct tc_action **a,
154 struct tcf_proto *tp, u32 flags,
155 struct netlink_ext_ack *extack)
156 {
157 struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
158 bool bind = flags & TCA_ACT_FLAGS_BIND;
159 struct nlattr *tb[TCA_MPLS_MAX + 1];
160 struct tcf_chain *goto_ch = NULL;
161 struct tcf_mpls_params *p;
162 struct tc_mpls *parm;
163 bool exists = false;
164 struct tcf_mpls *m;
165 int ret = 0, err;
166 u8 mpls_ttl = 0;
167 u32 index;
168
169 if (!nla) {
170 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
171 return -EINVAL;
172 }
173
174 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
175 if (err < 0)
176 return err;
177
178 if (!tb[TCA_MPLS_PARMS]) {
179 NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
180 return -EINVAL;
181 }
182 parm = nla_data(tb[TCA_MPLS_PARMS]);
183 index = parm->index;
184
185 /* Verify parameters against action type. */
186 switch (parm->m_action) {
187 case TCA_MPLS_ACT_POP:
188 if (!tb[TCA_MPLS_PROTO]) {
189 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
190 return -EINVAL;
191 }
192 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
193 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
194 return -EINVAL;
195 }
196 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
197 tb[TCA_MPLS_BOS]) {
198 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
199 return -EINVAL;
200 }
201 break;
202 case TCA_MPLS_ACT_DEC_TTL:
203 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
204 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
205 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
206 return -EINVAL;
207 }
208 break;
209 case TCA_MPLS_ACT_PUSH:
210 case TCA_MPLS_ACT_MAC_PUSH:
211 if (!tb[TCA_MPLS_LABEL]) {
212 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
213 return -EINVAL;
214 }
215 if (tb[TCA_MPLS_PROTO] &&
216 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
217 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
218 return -EPROTONOSUPPORT;
219 }
220 /* Push needs a TTL - if not specified, set a default value. */
221 if (!tb[TCA_MPLS_TTL]) {
222 #if IS_ENABLED(CONFIG_MPLS)
223 mpls_ttl = net->mpls.default_ttl ?
224 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
225 #else
226 mpls_ttl = ACT_MPLS_TTL_DEFAULT;
227 #endif
228 }
229 break;
230 case TCA_MPLS_ACT_MODIFY:
231 if (tb[TCA_MPLS_PROTO]) {
232 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
233 return -EINVAL;
234 }
235 break;
236 default:
237 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
238 return -EINVAL;
239 }
240
241 err = tcf_idr_check_alloc(tn, &index, a, bind);
242 if (err < 0)
243 return err;
244 exists = err;
245 if (exists && bind)
246 return 0;
247
248 if (!exists) {
249 ret = tcf_idr_create(tn, index, est, a,
250 &act_mpls_ops, bind, true, flags);
251 if (ret) {
252 tcf_idr_cleanup(tn, index);
253 return ret;
254 }
255
256 ret = ACT_P_CREATED;
257 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
258 tcf_idr_release(*a, bind);
259 return -EEXIST;
260 }
261
262 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
263 if (err < 0)
264 goto release_idr;
265
266 m = to_mpls(*a);
267
268 p = kzalloc(sizeof(*p), GFP_KERNEL);
269 if (!p) {
270 err = -ENOMEM;
271 goto put_chain;
272 }
273
274 p->tcfm_action = parm->m_action;
275 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
276 ACT_MPLS_LABEL_NOT_SET;
277 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
278 ACT_MPLS_TC_NOT_SET;
279 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
280 mpls_ttl;
281 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
282 ACT_MPLS_BOS_NOT_SET;
283 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
284 htons(ETH_P_MPLS_UC);
285
286 spin_lock_bh(&m->tcf_lock);
287 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
288 p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
289 spin_unlock_bh(&m->tcf_lock);
290
291 if (goto_ch)
292 tcf_chain_put_by_act(goto_ch);
293 if (p)
294 kfree_rcu(p, rcu);
295
296 return ret;
297 put_chain:
298 if (goto_ch)
299 tcf_chain_put_by_act(goto_ch);
300 release_idr:
301 tcf_idr_release(*a, bind);
302 return err;
303 }
304
tcf_mpls_cleanup(struct tc_action * a)305 static void tcf_mpls_cleanup(struct tc_action *a)
306 {
307 struct tcf_mpls *m = to_mpls(a);
308 struct tcf_mpls_params *p;
309
310 p = rcu_dereference_protected(m->mpls_p, 1);
311 if (p)
312 kfree_rcu(p, rcu);
313 }
314
tcf_mpls_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)315 static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
316 int bind, int ref)
317 {
318 unsigned char *b = skb_tail_pointer(skb);
319 struct tcf_mpls *m = to_mpls(a);
320 struct tcf_mpls_params *p;
321 struct tc_mpls opt = {
322 .index = m->tcf_index,
323 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
324 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
325 };
326 struct tcf_t t;
327
328 spin_lock_bh(&m->tcf_lock);
329 opt.action = m->tcf_action;
330 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
331 opt.m_action = p->tcfm_action;
332
333 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
334 goto nla_put_failure;
335
336 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
337 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
338 goto nla_put_failure;
339
340 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
341 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
342 goto nla_put_failure;
343
344 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
345 goto nla_put_failure;
346
347 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
348 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
349 goto nla_put_failure;
350
351 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
352 goto nla_put_failure;
353
354 tcf_tm_dump(&t, &m->tcf_tm);
355
356 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
357 goto nla_put_failure;
358
359 spin_unlock_bh(&m->tcf_lock);
360
361 return skb->len;
362
363 nla_put_failure:
364 spin_unlock_bh(&m->tcf_lock);
365 nlmsg_trim(skb, b);
366 return -EMSGSIZE;
367 }
368
tcf_mpls_offload_act_setup(struct tc_action * act,void * entry_data,u32 * index_inc,bool bind,struct netlink_ext_ack * extack)369 static int tcf_mpls_offload_act_setup(struct tc_action *act, void *entry_data,
370 u32 *index_inc, bool bind,
371 struct netlink_ext_ack *extack)
372 {
373 if (bind) {
374 struct flow_action_entry *entry = entry_data;
375
376 switch (tcf_mpls_action(act)) {
377 case TCA_MPLS_ACT_PUSH:
378 entry->id = FLOW_ACTION_MPLS_PUSH;
379 entry->mpls_push.proto = tcf_mpls_proto(act);
380 entry->mpls_push.label = tcf_mpls_label(act);
381 entry->mpls_push.tc = tcf_mpls_tc(act);
382 entry->mpls_push.bos = tcf_mpls_bos(act);
383 entry->mpls_push.ttl = tcf_mpls_ttl(act);
384 break;
385 case TCA_MPLS_ACT_POP:
386 entry->id = FLOW_ACTION_MPLS_POP;
387 entry->mpls_pop.proto = tcf_mpls_proto(act);
388 break;
389 case TCA_MPLS_ACT_MODIFY:
390 entry->id = FLOW_ACTION_MPLS_MANGLE;
391 entry->mpls_mangle.label = tcf_mpls_label(act);
392 entry->mpls_mangle.tc = tcf_mpls_tc(act);
393 entry->mpls_mangle.bos = tcf_mpls_bos(act);
394 entry->mpls_mangle.ttl = tcf_mpls_ttl(act);
395 break;
396 case TCA_MPLS_ACT_DEC_TTL:
397 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"dec_ttl\" option is used");
398 return -EOPNOTSUPP;
399 case TCA_MPLS_ACT_MAC_PUSH:
400 NL_SET_ERR_MSG_MOD(extack, "Offload not supported when \"mac_push\" option is used");
401 return -EOPNOTSUPP;
402 default:
403 NL_SET_ERR_MSG_MOD(extack, "Unsupported MPLS mode offload");
404 return -EOPNOTSUPP;
405 }
406 *index_inc = 1;
407 } else {
408 struct flow_offload_action *fl_action = entry_data;
409
410 switch (tcf_mpls_action(act)) {
411 case TCA_MPLS_ACT_PUSH:
412 fl_action->id = FLOW_ACTION_MPLS_PUSH;
413 break;
414 case TCA_MPLS_ACT_POP:
415 fl_action->id = FLOW_ACTION_MPLS_POP;
416 break;
417 case TCA_MPLS_ACT_MODIFY:
418 fl_action->id = FLOW_ACTION_MPLS_MANGLE;
419 break;
420 default:
421 return -EOPNOTSUPP;
422 }
423 }
424
425 return 0;
426 }
427
428 static struct tc_action_ops act_mpls_ops = {
429 .kind = "mpls",
430 .id = TCA_ID_MPLS,
431 .owner = THIS_MODULE,
432 .act = tcf_mpls_act,
433 .dump = tcf_mpls_dump,
434 .init = tcf_mpls_init,
435 .cleanup = tcf_mpls_cleanup,
436 .offload_act_setup = tcf_mpls_offload_act_setup,
437 .size = sizeof(struct tcf_mpls),
438 };
439
mpls_init_net(struct net * net)440 static __net_init int mpls_init_net(struct net *net)
441 {
442 struct tc_action_net *tn = net_generic(net, act_mpls_ops.net_id);
443
444 return tc_action_net_init(net, tn, &act_mpls_ops);
445 }
446
mpls_exit_net(struct list_head * net_list)447 static void __net_exit mpls_exit_net(struct list_head *net_list)
448 {
449 tc_action_net_exit(net_list, act_mpls_ops.net_id);
450 }
451
452 static struct pernet_operations mpls_net_ops = {
453 .init = mpls_init_net,
454 .exit_batch = mpls_exit_net,
455 .id = &act_mpls_ops.net_id,
456 .size = sizeof(struct tc_action_net),
457 };
458
mpls_init_module(void)459 static int __init mpls_init_module(void)
460 {
461 return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
462 }
463
mpls_cleanup_module(void)464 static void __exit mpls_cleanup_module(void)
465 {
466 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
467 }
468
469 module_init(mpls_init_module);
470 module_exit(mpls_cleanup_module);
471
472 MODULE_SOFTDEP("post: mpls_gso");
473 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
474 MODULE_LICENSE("GPL");
475 MODULE_DESCRIPTION("MPLS manipulation actions");
476