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