Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
  4 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
 
 
 
 
 
  5 */
  6
  7#include <linux/module.h>
  8#include <linux/init.h>
  9#include <linux/kernel.h>
 10#include <linux/skbuff.h>
 11#include <linux/rtnetlink.h>
 12#include <net/geneve.h>
 13#include <net/netlink.h>
 14#include <net/pkt_sched.h>
 15#include <net/dst.h>
 16#include <net/pkt_cls.h>
 17
 18#include <linux/tc_act/tc_tunnel_key.h>
 19#include <net/tc_act/tc_tunnel_key.h>
 20
 21static unsigned int tunnel_key_net_id;
 22static struct tc_action_ops act_tunnel_key_ops;
 23
 24static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
 25			  struct tcf_result *res)
 26{
 27	struct tcf_tunnel_key *t = to_tunnel_key(a);
 28	struct tcf_tunnel_key_params *params;
 29	int action;
 30
 31	params = rcu_dereference_bh(t->params);
 
 
 32
 33	tcf_lastuse_update(&t->tcf_tm);
 34	bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
 35	action = READ_ONCE(t->tcf_action);
 36
 37	switch (params->tcft_action) {
 38	case TCA_TUNNEL_KEY_ACT_RELEASE:
 39		skb_dst_drop(skb);
 40		break;
 41	case TCA_TUNNEL_KEY_ACT_SET:
 42		skb_dst_drop(skb);
 43		skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
 44		break;
 45	default:
 46		WARN_ONCE(1, "Bad tunnel_key action %d.\n",
 47			  params->tcft_action);
 48		break;
 49	}
 50
 51	return action;
 52}
 53
 54static const struct nla_policy
 55enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = {
 56	[TCA_TUNNEL_KEY_ENC_OPTS_GENEVE]	= { .type = NLA_NESTED },
 57};
 58
 59static const struct nla_policy
 60geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = {
 61	[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]	   = { .type = NLA_U16 },
 62	[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]	   = { .type = NLA_U8 },
 63	[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]	   = { .type = NLA_BINARY,
 64						       .len = 128 },
 65};
 66
 67static int
 68tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len,
 69			   struct netlink_ext_ack *extack)
 70{
 71	struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1];
 72	int err, data_len, opt_len;
 73	u8 *data;
 74
 75	err = nla_parse_nested_deprecated(tb,
 76					  TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX,
 77					  nla, geneve_opt_policy, extack);
 78	if (err < 0)
 79		return err;
 80
 81	if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] ||
 82	    !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] ||
 83	    !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) {
 84		NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
 85		return -EINVAL;
 86	}
 87
 88	data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
 89	data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]);
 90	if (data_len < 4) {
 91		NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
 92		return -ERANGE;
 93	}
 94	if (data_len % 4) {
 95		NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
 96		return -ERANGE;
 97	}
 98
 99	opt_len = sizeof(struct geneve_opt) + data_len;
100	if (dst) {
101		struct geneve_opt *opt = dst;
102
103		WARN_ON(dst_len < opt_len);
104
105		opt->opt_class =
106			nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]);
107		opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]);
108		opt->length = data_len / 4; /* length is in units of 4 bytes */
109		opt->r1 = 0;
110		opt->r2 = 0;
111		opt->r3 = 0;
112
113		memcpy(opt + 1, data, data_len);
114	}
115
116	return opt_len;
117}
118
119static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst,
120				int dst_len, struct netlink_ext_ack *extack)
121{
122	int err, rem, opt_len, len = nla_len(nla), opts_len = 0;
123	const struct nlattr *attr, *head = nla_data(nla);
124
125	err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX,
126				      enc_opts_policy, extack);
127	if (err)
128		return err;
129
130	nla_for_each_attr(attr, head, len, rem) {
131		switch (nla_type(attr)) {
132		case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
133			opt_len = tunnel_key_copy_geneve_opt(attr, dst,
134							     dst_len, extack);
135			if (opt_len < 0)
136				return opt_len;
137			opts_len += opt_len;
138			if (opts_len > IP_TUNNEL_OPTS_MAX) {
139				NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
140				return -EINVAL;
141			}
142			if (dst) {
143				dst_len -= opt_len;
144				dst += opt_len;
145			}
146			break;
147		}
148	}
149
150	if (!opts_len) {
151		NL_SET_ERR_MSG(extack, "Empty list of tunnel options");
152		return -EINVAL;
153	}
154
155	if (rem > 0) {
156		NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes");
157		return -EINVAL;
158	}
159
160	return opts_len;
161}
162
163static int tunnel_key_get_opts_len(struct nlattr *nla,
164				   struct netlink_ext_ack *extack)
165{
166	return tunnel_key_copy_opts(nla, NULL, 0, extack);
167}
168
169static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info,
170			       int opts_len, struct netlink_ext_ack *extack)
171{
172	info->options_len = opts_len;
173	switch (nla_type(nla_data(nla))) {
174	case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE:
175#if IS_ENABLED(CONFIG_INET)
176		info->key.tun_flags |= TUNNEL_GENEVE_OPT;
177		return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info),
178					    opts_len, extack);
179#else
180		return -EAFNOSUPPORT;
181#endif
182	default:
183		NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type");
184		return -EINVAL;
185	}
186}
187
188static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
189	[TCA_TUNNEL_KEY_PARMS]	    = { .len = sizeof(struct tc_tunnel_key) },
190	[TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
191	[TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
192	[TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
193	[TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
194	[TCA_TUNNEL_KEY_ENC_KEY_ID]   = { .type = NLA_U32 },
195	[TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
196	[TCA_TUNNEL_KEY_NO_CSUM]      = { .type = NLA_U8 },
197	[TCA_TUNNEL_KEY_ENC_OPTS]     = { .type = NLA_NESTED },
198	[TCA_TUNNEL_KEY_ENC_TOS]      = { .type = NLA_U8 },
199	[TCA_TUNNEL_KEY_ENC_TTL]      = { .type = NLA_U8 },
200};
201
202static void tunnel_key_release_params(struct tcf_tunnel_key_params *p)
203{
204	if (!p)
205		return;
206	if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
207		dst_release(&p->tcft_enc_metadata->dst);
208
209	kfree_rcu(p, rcu);
210}
211
212static int tunnel_key_init(struct net *net, struct nlattr *nla,
213			   struct nlattr *est, struct tc_action **a,
214			   int ovr, int bind, bool rtnl_held,
215			   struct tcf_proto *tp,
216			   struct netlink_ext_ack *extack)
217{
218	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
219	struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
 
220	struct tcf_tunnel_key_params *params_new;
221	struct metadata_dst *metadata = NULL;
222	struct tcf_chain *goto_ch = NULL;
223	struct tc_tunnel_key *parm;
224	struct tcf_tunnel_key *t;
225	bool exists = false;
226	__be16 dst_port = 0;
227	__be64 key_id = 0;
228	int opts_len = 0;
229	__be16 flags = 0;
230	u8 tos, ttl;
231	int ret = 0;
232	u32 index;
233	int err;
234
235	if (!nla) {
236		NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed");
237		return -EINVAL;
238	}
239
240	err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla,
241					  tunnel_key_policy, extack);
242	if (err < 0) {
243		NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes");
244		return err;
245	}
246
247	if (!tb[TCA_TUNNEL_KEY_PARMS]) {
248		NL_SET_ERR_MSG(extack, "Missing tunnel key parameters");
249		return -EINVAL;
250	}
251
252	parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
253	index = parm->index;
254	err = tcf_idr_check_alloc(tn, &index, a, bind);
255	if (err < 0)
256		return err;
257	exists = err;
258	if (exists && bind)
259		return 0;
260
261	switch (parm->t_action) {
262	case TCA_TUNNEL_KEY_ACT_RELEASE:
263		break;
264	case TCA_TUNNEL_KEY_ACT_SET:
265		if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
266			__be32 key32;
267
268			key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]);
269			key_id = key32_to_tunnel_id(key32);
270			flags = TUNNEL_KEY;
271		}
272
273		flags |= TUNNEL_CSUM;
 
 
274		if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
275		    nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
276			flags &= ~TUNNEL_CSUM;
277
278		if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
279			dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
280
281		if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) {
282			opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS],
283							   extack);
284			if (opts_len < 0) {
285				ret = opts_len;
286				goto err_out;
287			}
288		}
289
290		tos = 0;
291		if (tb[TCA_TUNNEL_KEY_ENC_TOS])
292			tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]);
293		ttl = 0;
294		if (tb[TCA_TUNNEL_KEY_ENC_TTL])
295			ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]);
296
297		if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
298		    tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
299			__be32 saddr;
300			__be32 daddr;
301
302			saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
303			daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
304
305			metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl,
306						    dst_port, flags,
307						    key_id, opts_len);
308		} else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
309			   tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
310			struct in6_addr saddr;
311			struct in6_addr daddr;
312
313			saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
314			daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
315
316			metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port,
317						      0, flags,
318						      key_id, 0);
319		} else {
320			NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst");
321			ret = -EINVAL;
322			goto err_out;
323		}
324
325		if (!metadata) {
326			NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst");
327			ret = -ENOMEM;
328			goto err_out;
329		}
330
331#ifdef CONFIG_DST_CACHE
332		ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL);
333		if (ret)
334			goto release_tun_meta;
335#endif
336
337		if (opts_len) {
338			ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS],
339						  &metadata->u.tun_info,
340						  opts_len, extack);
341			if (ret < 0)
342				goto release_tun_meta;
343		}
344
345		metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
346		break;
347	default:
348		NL_SET_ERR_MSG(extack, "Unknown tunnel key action");
349		ret = -EINVAL;
350		goto err_out;
351	}
352
353	if (!exists) {
354		ret = tcf_idr_create(tn, index, est, a,
355				     &act_tunnel_key_ops, bind, true);
356		if (ret) {
357			NL_SET_ERR_MSG(extack, "Cannot create TC IDR");
358			goto release_tun_meta;
359		}
360
361		ret = ACT_P_CREATED;
362	} else if (!ovr) {
363		NL_SET_ERR_MSG(extack, "TC IDR already exists");
364		ret = -EEXIST;
365		goto release_tun_meta;
366	}
367
368	err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
369	if (err < 0) {
370		ret = err;
371		exists = true;
372		goto release_tun_meta;
373	}
374	t = to_tunnel_key(*a);
375
 
376	params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
377	if (unlikely(!params_new)) {
378		NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters");
379		ret = -ENOMEM;
380		exists = true;
381		goto put_chain;
382	}
 
 
 
 
383	params_new->tcft_action = parm->t_action;
384	params_new->tcft_enc_metadata = metadata;
385
386	spin_lock_bh(&t->tcf_lock);
387	goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
388	rcu_swap_protected(t->params, params_new,
389			   lockdep_is_held(&t->tcf_lock));
390	spin_unlock_bh(&t->tcf_lock);
391	tunnel_key_release_params(params_new);
392	if (goto_ch)
393		tcf_chain_put_by_act(goto_ch);
394
395	if (ret == ACT_P_CREATED)
396		tcf_idr_insert(tn, *a);
397
398	return ret;
399
400put_chain:
401	if (goto_ch)
402		tcf_chain_put_by_act(goto_ch);
403
404release_tun_meta:
405	if (metadata)
406		dst_release(&metadata->dst);
407
408err_out:
409	if (exists)
410		tcf_idr_release(*a, bind);
411	else
412		tcf_idr_cleanup(tn, index);
413	return ret;
414}
415
416static void tunnel_key_release(struct tc_action *a)
417{
418	struct tcf_tunnel_key *t = to_tunnel_key(a);
419	struct tcf_tunnel_key_params *params;
420
421	params = rcu_dereference_protected(t->params, 1);
422	tunnel_key_release_params(params);
423}
 
424
425static int tunnel_key_geneve_opts_dump(struct sk_buff *skb,
426				       const struct ip_tunnel_info *info)
427{
428	int len = info->options_len;
429	u8 *src = (u8 *)(info + 1);
430	struct nlattr *start;
431
432	start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE);
433	if (!start)
434		return -EMSGSIZE;
435
436	while (len > 0) {
437		struct geneve_opt *opt = (struct geneve_opt *)src;
438
439		if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS,
440				 opt->opt_class) ||
441		    nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE,
442			       opt->type) ||
443		    nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA,
444			    opt->length * 4, opt + 1)) {
445			nla_nest_cancel(skb, start);
446			return -EMSGSIZE;
447		}
448
449		len -= sizeof(struct geneve_opt) + opt->length * 4;
450		src += sizeof(struct geneve_opt) + opt->length * 4;
451	}
452
453	nla_nest_end(skb, start);
454	return 0;
455}
456
457static int tunnel_key_opts_dump(struct sk_buff *skb,
458				const struct ip_tunnel_info *info)
459{
460	struct nlattr *start;
461	int err = -EINVAL;
462
463	if (!info->options_len)
464		return 0;
465
466	start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS);
467	if (!start)
468		return -EMSGSIZE;
469
470	if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
471		err = tunnel_key_geneve_opts_dump(skb, info);
472		if (err)
473			goto err_out;
474	} else {
475err_out:
476		nla_nest_cancel(skb, start);
477		return err;
478	}
479
480	nla_nest_end(skb, start);
481	return 0;
482}
483
484static int tunnel_key_dump_addresses(struct sk_buff *skb,
485				     const struct ip_tunnel_info *info)
486{
487	unsigned short family = ip_tunnel_info_af(info);
488
489	if (family == AF_INET) {
490		__be32 saddr = info->key.u.ipv4.src;
491		__be32 daddr = info->key.u.ipv4.dst;
492
493		if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
494		    !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
495			return 0;
496	}
497
498	if (family == AF_INET6) {
499		const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
500		const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
501
502		if (!nla_put_in6_addr(skb,
503				      TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
504		    !nla_put_in6_addr(skb,
505				      TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
506			return 0;
507	}
508
509	return -EINVAL;
510}
511
512static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
513			   int bind, int ref)
514{
515	unsigned char *b = skb_tail_pointer(skb);
516	struct tcf_tunnel_key *t = to_tunnel_key(a);
517	struct tcf_tunnel_key_params *params;
518	struct tc_tunnel_key opt = {
519		.index    = t->tcf_index,
520		.refcnt   = refcount_read(&t->tcf_refcnt) - ref,
521		.bindcnt  = atomic_read(&t->tcf_bindcnt) - bind,
522	};
523	struct tcf_t tm;
524
525	spin_lock_bh(&t->tcf_lock);
526	params = rcu_dereference_protected(t->params,
527					   lockdep_is_held(&t->tcf_lock));
528	opt.action   = t->tcf_action;
529	opt.t_action = params->tcft_action;
 
530
531	if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
532		goto nla_put_failure;
533
534	if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
535		struct ip_tunnel_info *info =
536			&params->tcft_enc_metadata->u.tun_info;
537		struct ip_tunnel_key *key = &info->key;
538		__be32 key_id = tunnel_id_to_key32(key->tun_id);
539
540		if (((key->tun_flags & TUNNEL_KEY) &&
541		     nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) ||
542		    tunnel_key_dump_addresses(skb,
543					      &params->tcft_enc_metadata->u.tun_info) ||
544		    (key->tp_dst &&
545		      nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT,
546				   key->tp_dst)) ||
547		    nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
548			       !(key->tun_flags & TUNNEL_CSUM)) ||
549		    tunnel_key_opts_dump(skb, info))
550			goto nla_put_failure;
551
552		if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos))
553			goto nla_put_failure;
554
555		if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl))
556			goto nla_put_failure;
557	}
558
559	tcf_tm_dump(&tm, &t->tcf_tm);
560	if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
561			  &tm, TCA_TUNNEL_KEY_PAD))
562		goto nla_put_failure;
563	spin_unlock_bh(&t->tcf_lock);
564
565	return skb->len;
566
567nla_put_failure:
568	spin_unlock_bh(&t->tcf_lock);
569	nlmsg_trim(skb, b);
570	return -1;
571}
572
573static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
574			     struct netlink_callback *cb, int type,
575			     const struct tc_action_ops *ops,
576			     struct netlink_ext_ack *extack)
577{
578	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
579
580	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
581}
582
583static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index)
 
584{
585	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
586
587	return tcf_idr_search(tn, a, index);
588}
589
590static struct tc_action_ops act_tunnel_key_ops = {
591	.kind		=	"tunnel_key",
592	.id		=	TCA_ID_TUNNEL_KEY,
593	.owner		=	THIS_MODULE,
594	.act		=	tunnel_key_act,
595	.dump		=	tunnel_key_dump,
596	.init		=	tunnel_key_init,
597	.cleanup	=	tunnel_key_release,
598	.walk		=	tunnel_key_walker,
599	.lookup		=	tunnel_key_search,
600	.size		=	sizeof(struct tcf_tunnel_key),
601};
602
603static __net_init int tunnel_key_init_net(struct net *net)
604{
605	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
606
607	return tc_action_net_init(net, tn, &act_tunnel_key_ops);
608}
609
610static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
611{
612	tc_action_net_exit(net_list, tunnel_key_net_id);
613}
614
615static struct pernet_operations tunnel_key_net_ops = {
616	.init = tunnel_key_init_net,
617	.exit_batch = tunnel_key_exit_net,
618	.id   = &tunnel_key_net_id,
619	.size = sizeof(struct tc_action_net),
620};
621
622static int __init tunnel_key_init_module(void)
623{
624	return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
625}
626
627static void __exit tunnel_key_cleanup_module(void)
628{
629	tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
630}
631
632module_init(tunnel_key_init_module);
633module_exit(tunnel_key_cleanup_module);
634
635MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
636MODULE_DESCRIPTION("ip tunnel manipulation actions");
637MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  2 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
  3 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
  4 *
  5 * This program is free software; you can redistribute it and/or modify
  6 * it under the terms of the GNU General Public License as published by
  7 * the Free Software Foundation; either version 2 of the License, or
  8 * (at your option) any later version.
  9 */
 10
 11#include <linux/module.h>
 12#include <linux/init.h>
 13#include <linux/kernel.h>
 14#include <linux/skbuff.h>
 15#include <linux/rtnetlink.h>
 
 16#include <net/netlink.h>
 17#include <net/pkt_sched.h>
 18#include <net/dst.h>
 
 19
 20#include <linux/tc_act/tc_tunnel_key.h>
 21#include <net/tc_act/tc_tunnel_key.h>
 22
 23static unsigned int tunnel_key_net_id;
 24static struct tc_action_ops act_tunnel_key_ops;
 25
 26static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
 27			  struct tcf_result *res)
 28{
 29	struct tcf_tunnel_key *t = to_tunnel_key(a);
 30	struct tcf_tunnel_key_params *params;
 31	int action;
 32
 33	rcu_read_lock();
 34
 35	params = rcu_dereference(t->params);
 36
 37	tcf_lastuse_update(&t->tcf_tm);
 38	bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
 39	action = params->action;
 40
 41	switch (params->tcft_action) {
 42	case TCA_TUNNEL_KEY_ACT_RELEASE:
 43		skb_dst_drop(skb);
 44		break;
 45	case TCA_TUNNEL_KEY_ACT_SET:
 46		skb_dst_drop(skb);
 47		skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
 48		break;
 49	default:
 50		WARN_ONCE(1, "Bad tunnel_key action %d.\n",
 51			  params->tcft_action);
 52		break;
 53	}
 54
 55	rcu_read_unlock();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56
 57	return action;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 58}
 59
 60static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
 61	[TCA_TUNNEL_KEY_PARMS]	    = { .len = sizeof(struct tc_tunnel_key) },
 62	[TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
 63	[TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
 64	[TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
 65	[TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
 66	[TCA_TUNNEL_KEY_ENC_KEY_ID]   = { .type = NLA_U32 },
 67	[TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
 68	[TCA_TUNNEL_KEY_NO_CSUM]      = { .type = NLA_U8 },
 
 
 
 69};
 70
 
 
 
 
 
 
 
 
 
 
 71static int tunnel_key_init(struct net *net, struct nlattr *nla,
 72			   struct nlattr *est, struct tc_action **a,
 73			   int ovr, int bind, struct netlink_ext_ack *extack)
 
 
 74{
 75	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
 76	struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
 77	struct tcf_tunnel_key_params *params_old;
 78	struct tcf_tunnel_key_params *params_new;
 79	struct metadata_dst *metadata = NULL;
 
 80	struct tc_tunnel_key *parm;
 81	struct tcf_tunnel_key *t;
 82	bool exists = false;
 83	__be16 dst_port = 0;
 84	__be64 key_id;
 85	__be16 flags;
 
 
 86	int ret = 0;
 
 87	int err;
 88
 89	if (!nla)
 
 90		return -EINVAL;
 
 91
 92	err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
 93			       NULL);
 94	if (err < 0)
 
 95		return err;
 
 96
 97	if (!tb[TCA_TUNNEL_KEY_PARMS])
 
 98		return -EINVAL;
 
 99
100	parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
101	exists = tcf_idr_check(tn, parm->index, a, bind);
 
 
 
 
102	if (exists && bind)
103		return 0;
104
105	switch (parm->t_action) {
106	case TCA_TUNNEL_KEY_ACT_RELEASE:
107		break;
108	case TCA_TUNNEL_KEY_ACT_SET:
109		if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
110			ret = -EINVAL;
111			goto err_out;
 
 
 
112		}
113
114		key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
115
116		flags = TUNNEL_KEY | TUNNEL_CSUM;
117		if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
118		    nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
119			flags &= ~TUNNEL_CSUM;
120
121		if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
122			dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
123
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124		if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
125		    tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
126			__be32 saddr;
127			__be32 daddr;
128
129			saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
130			daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
131
132			metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
133						    dst_port, flags,
134						    key_id, 0);
135		} else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
136			   tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
137			struct in6_addr saddr;
138			struct in6_addr daddr;
139
140			saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
141			daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
142
143			metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
144						      0, flags,
145						      key_id, 0);
 
 
 
 
146		}
147
148		if (!metadata) {
149			ret = -EINVAL;
 
150			goto err_out;
151		}
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153		metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
154		break;
155	default:
 
156		ret = -EINVAL;
157		goto err_out;
158	}
159
160	if (!exists) {
161		ret = tcf_idr_create(tn, parm->index, est, a,
162				     &act_tunnel_key_ops, bind, true);
163		if (ret)
164			return ret;
 
 
165
166		ret = ACT_P_CREATED;
167	} else {
168		tcf_idr_release(*a, bind);
169		if (!ovr)
170			return -EEXIST;
171	}
172
 
 
 
 
 
 
173	t = to_tunnel_key(*a);
174
175	ASSERT_RTNL();
176	params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
177	if (unlikely(!params_new)) {
178		if (ret == ACT_P_CREATED)
179			tcf_idr_release(*a, bind);
180		return -ENOMEM;
 
181	}
182
183	params_old = rtnl_dereference(t->params);
184
185	params_new->action = parm->action;
186	params_new->tcft_action = parm->t_action;
187	params_new->tcft_enc_metadata = metadata;
188
189	rcu_assign_pointer(t->params, params_new);
190
191	if (params_old)
192		kfree_rcu(params_old, rcu);
 
 
 
 
193
194	if (ret == ACT_P_CREATED)
195		tcf_idr_insert(tn, *a);
196
197	return ret;
198
 
 
 
 
 
 
 
 
199err_out:
200	if (exists)
201		tcf_idr_release(*a, bind);
 
 
202	return ret;
203}
204
205static void tunnel_key_release(struct tc_action *a)
206{
207	struct tcf_tunnel_key *t = to_tunnel_key(a);
208	struct tcf_tunnel_key_params *params;
209
210	params = rcu_dereference_protected(t->params, 1);
211	if (params) {
212		if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
213			dst_release(&params->tcft_enc_metadata->dst);
214
215		kfree_rcu(params, rcu);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217}
218
219static int tunnel_key_dump_addresses(struct sk_buff *skb,
220				     const struct ip_tunnel_info *info)
221{
222	unsigned short family = ip_tunnel_info_af(info);
223
224	if (family == AF_INET) {
225		__be32 saddr = info->key.u.ipv4.src;
226		__be32 daddr = info->key.u.ipv4.dst;
227
228		if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
229		    !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
230			return 0;
231	}
232
233	if (family == AF_INET6) {
234		const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
235		const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
236
237		if (!nla_put_in6_addr(skb,
238				      TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
239		    !nla_put_in6_addr(skb,
240				      TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
241			return 0;
242	}
243
244	return -EINVAL;
245}
246
247static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
248			   int bind, int ref)
249{
250	unsigned char *b = skb_tail_pointer(skb);
251	struct tcf_tunnel_key *t = to_tunnel_key(a);
252	struct tcf_tunnel_key_params *params;
253	struct tc_tunnel_key opt = {
254		.index    = t->tcf_index,
255		.refcnt   = t->tcf_refcnt - ref,
256		.bindcnt  = t->tcf_bindcnt - bind,
257	};
258	struct tcf_t tm;
259
260	params = rtnl_dereference(t->params);
261
 
 
262	opt.t_action = params->tcft_action;
263	opt.action = params->action;
264
265	if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
266		goto nla_put_failure;
267
268	if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
269		struct ip_tunnel_key *key =
270			&params->tcft_enc_metadata->u.tun_info.key;
 
271		__be32 key_id = tunnel_id_to_key32(key->tun_id);
272
273		if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
 
274		    tunnel_key_dump_addresses(skb,
275					      &params->tcft_enc_metadata->u.tun_info) ||
276		    nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
 
 
277		    nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
278			       !(key->tun_flags & TUNNEL_CSUM)))
 
 
 
 
 
 
 
279			goto nla_put_failure;
280	}
281
282	tcf_tm_dump(&tm, &t->tcf_tm);
283	if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
284			  &tm, TCA_TUNNEL_KEY_PAD))
285		goto nla_put_failure;
 
286
287	return skb->len;
288
289nla_put_failure:
 
290	nlmsg_trim(skb, b);
291	return -1;
292}
293
294static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
295			     struct netlink_callback *cb, int type,
296			     const struct tc_action_ops *ops,
297			     struct netlink_ext_ack *extack)
298{
299	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
300
301	return tcf_generic_walker(tn, skb, cb, type, ops, extack);
302}
303
304static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
305			     struct netlink_ext_ack *extack)
306{
307	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
308
309	return tcf_idr_search(tn, a, index);
310}
311
312static struct tc_action_ops act_tunnel_key_ops = {
313	.kind		=	"tunnel_key",
314	.type		=	TCA_ACT_TUNNEL_KEY,
315	.owner		=	THIS_MODULE,
316	.act		=	tunnel_key_act,
317	.dump		=	tunnel_key_dump,
318	.init		=	tunnel_key_init,
319	.cleanup	=	tunnel_key_release,
320	.walk		=	tunnel_key_walker,
321	.lookup		=	tunnel_key_search,
322	.size		=	sizeof(struct tcf_tunnel_key),
323};
324
325static __net_init int tunnel_key_init_net(struct net *net)
326{
327	struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
328
329	return tc_action_net_init(tn, &act_tunnel_key_ops);
330}
331
332static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
333{
334	tc_action_net_exit(net_list, tunnel_key_net_id);
335}
336
337static struct pernet_operations tunnel_key_net_ops = {
338	.init = tunnel_key_init_net,
339	.exit_batch = tunnel_key_exit_net,
340	.id   = &tunnel_key_net_id,
341	.size = sizeof(struct tc_action_net),
342};
343
344static int __init tunnel_key_init_module(void)
345{
346	return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
347}
348
349static void __exit tunnel_key_cleanup_module(void)
350{
351	tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
352}
353
354module_init(tunnel_key_init_module);
355module_exit(tunnel_key_cleanup_module);
356
357MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
358MODULE_DESCRIPTION("ip tunnel manipulation actions");
359MODULE_LICENSE("GPL v2");