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