1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
4 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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>
16 #include <net/pkt_cls.h>
18 #include <linux/tc_act/tc_tunnel_key.h>
19 #include <net/tc_act/tc_tunnel_key.h>
21 static unsigned int tunnel_key_net_id
;
22 static struct tc_action_ops act_tunnel_key_ops
;
24 static int tunnel_key_act(struct sk_buff
*skb
, const struct tc_action
*a
,
25 struct tcf_result
*res
)
27 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
28 struct tcf_tunnel_key_params
*params
;
31 params
= rcu_dereference_bh(t
->params
);
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
);
37 switch (params
->tcft_action
) {
38 case TCA_TUNNEL_KEY_ACT_RELEASE
:
41 case TCA_TUNNEL_KEY_ACT_SET
:
43 skb_dst_set(skb
, dst_clone(¶ms
->tcft_enc_metadata
->dst
));
46 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
54 static const struct nla_policy
55 enc_opts_policy
[TCA_TUNNEL_KEY_ENC_OPTS_MAX
+ 1] = {
56 [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE
] = { .type
= NLA_NESTED
},
59 static const struct nla_policy
60 geneve_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
,
68 tunnel_key_copy_geneve_opt(const struct nlattr
*nla
, void *dst
, int dst_len
,
69 struct netlink_ext_ack
*extack
)
71 struct nlattr
*tb
[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX
+ 1];
72 int err
, data_len
, opt_len
;
75 err
= nla_parse_nested_deprecated(tb
,
76 TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX
,
77 nla
, geneve_opt_policy
, extack
);
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");
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
]);
91 NL_SET_ERR_MSG(extack
, "Tunnel key geneve option data is less than 4 bytes long");
95 NL_SET_ERR_MSG(extack
, "Tunnel key geneve option data is not a multiple of 4 bytes long");
99 opt_len
= sizeof(struct geneve_opt
) + data_len
;
101 struct geneve_opt
*opt
= dst
;
103 WARN_ON(dst_len
< opt_len
);
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 */
113 memcpy(opt
+ 1, data
, data_len
);
119 static int tunnel_key_copy_opts(const struct nlattr
*nla
, u8
*dst
,
120 int dst_len
, struct netlink_ext_ack
*extack
)
122 int err
, rem
, opt_len
, len
= nla_len(nla
), opts_len
= 0;
123 const struct nlattr
*attr
, *head
= nla_data(nla
);
125 err
= nla_validate_deprecated(head
, len
, TCA_TUNNEL_KEY_ENC_OPTS_MAX
,
126 enc_opts_policy
, extack
);
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
,
138 if (opts_len
> IP_TUNNEL_OPTS_MAX
) {
139 NL_SET_ERR_MSG(extack
, "Tunnel options exceeds max size");
151 NL_SET_ERR_MSG(extack
, "Empty list of tunnel options");
156 NL_SET_ERR_MSG(extack
, "Trailing data after parsing tunnel key options attributes");
163 static int tunnel_key_get_opts_len(struct nlattr
*nla
,
164 struct netlink_ext_ack
*extack
)
166 return tunnel_key_copy_opts(nla
, NULL
, 0, extack
);
169 static int tunnel_key_opts_set(struct nlattr
*nla
, struct ip_tunnel_info
*info
,
170 int opts_len
, struct netlink_ext_ack
*extack
)
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
),
180 return -EAFNOSUPPORT
;
183 NL_SET_ERR_MSG(extack
, "Cannot set tunnel options for unknown tunnel type");
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
},
202 static void tunnel_key_release_params(struct tcf_tunnel_key_params
*p
)
206 if (p
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
)
207 dst_release(&p
->tcft_enc_metadata
->dst
);
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
)
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
;
236 NL_SET_ERR_MSG(extack
, "Tunnel requires attributes to be passed");
240 err
= nla_parse_nested_deprecated(tb
, TCA_TUNNEL_KEY_MAX
, nla
,
241 tunnel_key_policy
, extack
);
243 NL_SET_ERR_MSG(extack
, "Failed to parse nested tunnel key attributes");
247 if (!tb
[TCA_TUNNEL_KEY_PARMS
]) {
248 NL_SET_ERR_MSG(extack
, "Missing tunnel key parameters");
252 parm
= nla_data(tb
[TCA_TUNNEL_KEY_PARMS
]);
254 err
= tcf_idr_check_alloc(tn
, &index
, a
, bind
);
261 switch (parm
->t_action
) {
262 case TCA_TUNNEL_KEY_ACT_RELEASE
:
264 case TCA_TUNNEL_KEY_ACT_SET
:
265 if (tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]) {
268 key32
= nla_get_be32(tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]);
269 key_id
= key32_to_tunnel_id(key32
);
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
;
278 if (tb
[TCA_TUNNEL_KEY_ENC_DST_PORT
])
279 dst_port
= nla_get_be16(tb
[TCA_TUNNEL_KEY_ENC_DST_PORT
]);
281 if (tb
[TCA_TUNNEL_KEY_ENC_OPTS
]) {
282 opts_len
= tunnel_key_get_opts_len(tb
[TCA_TUNNEL_KEY_ENC_OPTS
],
291 if (tb
[TCA_TUNNEL_KEY_ENC_TOS
])
292 tos
= nla_get_u8(tb
[TCA_TUNNEL_KEY_ENC_TOS
]);
294 if (tb
[TCA_TUNNEL_KEY_ENC_TTL
])
295 ttl
= nla_get_u8(tb
[TCA_TUNNEL_KEY_ENC_TTL
]);
297 if (tb
[TCA_TUNNEL_KEY_ENC_IPV4_SRC
] &&
298 tb
[TCA_TUNNEL_KEY_ENC_IPV4_DST
]) {
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
]);
305 metadata
= __ip_tun_set_dst(saddr
, daddr
, tos
, ttl
,
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
;
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
]);
316 metadata
= __ipv6_tun_set_dst(&saddr
, &daddr
, tos
, ttl
, dst_port
,
320 NL_SET_ERR_MSG(extack
, "Missing either ipv4 or ipv6 src and dst");
326 NL_SET_ERR_MSG(extack
, "Cannot allocate tunnel metadata dst");
331 #ifdef CONFIG_DST_CACHE
332 ret
= dst_cache_init(&metadata
->u
.tun_info
.dst_cache
, GFP_KERNEL
);
334 goto release_tun_meta
;
338 ret
= tunnel_key_opts_set(tb
[TCA_TUNNEL_KEY_ENC_OPTS
],
339 &metadata
->u
.tun_info
,
342 goto release_tun_meta
;
345 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
;
348 NL_SET_ERR_MSG(extack
, "Unknown tunnel key action");
354 ret
= tcf_idr_create(tn
, index
, est
, a
,
355 &act_tunnel_key_ops
, bind
, true);
357 NL_SET_ERR_MSG(extack
, "Cannot create TC IDR");
358 goto release_tun_meta
;
363 NL_SET_ERR_MSG(extack
, "TC IDR already exists");
365 goto release_tun_meta
;
368 err
= tcf_action_check_ctrlact(parm
->action
, tp
, &goto_ch
, extack
);
372 goto release_tun_meta
;
374 t
= to_tunnel_key(*a
);
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");
383 params_new
->tcft_action
= parm
->t_action
;
384 params_new
->tcft_enc_metadata
= metadata
;
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
);
393 tcf_chain_put_by_act(goto_ch
);
395 if (ret
== ACT_P_CREATED
)
396 tcf_idr_insert(tn
, *a
);
402 tcf_chain_put_by_act(goto_ch
);
406 dst_release(&metadata
->dst
);
410 tcf_idr_release(*a
, bind
);
412 tcf_idr_cleanup(tn
, index
);
416 static void tunnel_key_release(struct tc_action
*a
)
418 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
419 struct tcf_tunnel_key_params
*params
;
421 params
= rcu_dereference_protected(t
->params
, 1);
422 tunnel_key_release_params(params
);
425 static int tunnel_key_geneve_opts_dump(struct sk_buff
*skb
,
426 const struct ip_tunnel_info
*info
)
428 int len
= info
->options_len
;
429 u8
*src
= (u8
*)(info
+ 1);
430 struct nlattr
*start
;
432 start
= nla_nest_start_noflag(skb
, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE
);
437 struct geneve_opt
*opt
= (struct geneve_opt
*)src
;
439 if (nla_put_be16(skb
, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS
,
441 nla_put_u8(skb
, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE
,
443 nla_put(skb
, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA
,
444 opt
->length
* 4, opt
+ 1)) {
445 nla_nest_cancel(skb
, start
);
449 len
-= sizeof(struct geneve_opt
) + opt
->length
* 4;
450 src
+= sizeof(struct geneve_opt
) + opt
->length
* 4;
453 nla_nest_end(skb
, start
);
457 static int tunnel_key_opts_dump(struct sk_buff
*skb
,
458 const struct ip_tunnel_info
*info
)
460 struct nlattr
*start
;
463 if (!info
->options_len
)
466 start
= nla_nest_start_noflag(skb
, TCA_TUNNEL_KEY_ENC_OPTS
);
470 if (info
->key
.tun_flags
& TUNNEL_GENEVE_OPT
) {
471 err
= tunnel_key_geneve_opts_dump(skb
, info
);
476 nla_nest_cancel(skb
, start
);
480 nla_nest_end(skb
, start
);
484 static int tunnel_key_dump_addresses(struct sk_buff
*skb
,
485 const struct ip_tunnel_info
*info
)
487 unsigned short family
= ip_tunnel_info_af(info
);
489 if (family
== AF_INET
) {
490 __be32 saddr
= info
->key
.u
.ipv4
.src
;
491 __be32 daddr
= info
->key
.u
.ipv4
.dst
;
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
))
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
;
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
))
512 static int tunnel_key_dump(struct sk_buff
*skb
, struct tc_action
*a
,
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
,
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
;
531 if (nla_put(skb
, TCA_TUNNEL_KEY_PARMS
, sizeof(opt
), &opt
))
532 goto nla_put_failure
;
534 if (params
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
) {
535 struct ip_tunnel_info
*info
=
536 ¶ms
->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
);
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 ¶ms
->tcft_enc_metadata
->u
.tun_info
) ||
545 nla_put_be16(skb
, TCA_TUNNEL_KEY_ENC_DST_PORT
,
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
;
552 if (key
->tos
&& nla_put_u8(skb
, TCA_TUNNEL_KEY_ENC_TOS
, key
->tos
))
553 goto nla_put_failure
;
555 if (key
->ttl
&& nla_put_u8(skb
, TCA_TUNNEL_KEY_ENC_TTL
, key
->ttl
))
556 goto nla_put_failure
;
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
);
568 spin_unlock_bh(&t
->tcf_lock
);
573 static 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
)
578 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
580 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
, extack
);
583 static int tunnel_key_search(struct net
*net
, struct tc_action
**a
, u32 index
)
585 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
587 return tcf_idr_search(tn
, a
, index
);
590 static 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
),
603 static __net_init
int tunnel_key_init_net(struct net
*net
)
605 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
607 return tc_action_net_init(net
, tn
, &act_tunnel_key_ops
);
610 static void __net_exit
tunnel_key_exit_net(struct list_head
*net_list
)
612 tc_action_net_exit(net_list
, tunnel_key_net_id
);
615 static 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
),
622 static int __init
tunnel_key_init_module(void)
624 return tcf_register_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
627 static void __exit
tunnel_key_cleanup_module(void)
629 tcf_unregister_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
632 module_init(tunnel_key_init_module
);
633 module_exit(tunnel_key_cleanup_module
);
635 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
636 MODULE_DESCRIPTION("ip tunnel manipulation actions");
637 MODULE_LICENSE("GPL v2");