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