2 * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
3 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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.
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>
19 #include <net/dst_metadata.h>
21 #include <linux/tc_act/tc_tunnel_key.h>
22 #include <net/tc_act/tc_tunnel_key.h>
24 #define TUNNEL_KEY_TAB_MASK 15
26 static int tunnel_key_net_id
;
27 static struct tc_action_ops act_tunnel_key_ops
;
29 static int tunnel_key_act(struct sk_buff
*skb
, const struct tc_action
*a
,
30 struct tcf_result
*res
)
32 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
33 struct tcf_tunnel_key_params
*params
;
38 params
= rcu_dereference(t
->params
);
40 tcf_lastuse_update(&t
->tcf_tm
);
41 bstats_cpu_update(this_cpu_ptr(t
->common
.cpu_bstats
), skb
);
42 action
= params
->action
;
44 switch (params
->tcft_action
) {
45 case TCA_TUNNEL_KEY_ACT_RELEASE
:
48 case TCA_TUNNEL_KEY_ACT_SET
:
50 skb_dst_set(skb
, dst_clone(¶ms
->tcft_enc_metadata
->dst
));
53 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
63 static const struct nla_policy tunnel_key_policy
[TCA_TUNNEL_KEY_MAX
+ 1] = {
64 [TCA_TUNNEL_KEY_PARMS
] = { .len
= sizeof(struct tc_tunnel_key
) },
65 [TCA_TUNNEL_KEY_ENC_IPV4_SRC
] = { .type
= NLA_U32
},
66 [TCA_TUNNEL_KEY_ENC_IPV4_DST
] = { .type
= NLA_U32
},
67 [TCA_TUNNEL_KEY_ENC_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
68 [TCA_TUNNEL_KEY_ENC_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
69 [TCA_TUNNEL_KEY_ENC_KEY_ID
] = { .type
= NLA_U32
},
72 static int tunnel_key_init(struct net
*net
, struct nlattr
*nla
,
73 struct nlattr
*est
, struct tc_action
**a
,
76 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
77 struct nlattr
*tb
[TCA_TUNNEL_KEY_MAX
+ 1];
78 struct tcf_tunnel_key_params
*params_old
;
79 struct tcf_tunnel_key_params
*params_new
;
80 struct metadata_dst
*metadata
= NULL
;
81 struct tc_tunnel_key
*parm
;
82 struct tcf_tunnel_key
*t
;
91 err
= nla_parse_nested(tb
, TCA_TUNNEL_KEY_MAX
, nla
, tunnel_key_policy
);
95 if (!tb
[TCA_TUNNEL_KEY_PARMS
])
98 parm
= nla_data(tb
[TCA_TUNNEL_KEY_PARMS
]);
99 exists
= tcf_hash_check(tn
, parm
->index
, a
, bind
);
103 switch (parm
->t_action
) {
104 case TCA_TUNNEL_KEY_ACT_RELEASE
:
106 case TCA_TUNNEL_KEY_ACT_SET
:
107 if (!tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]) {
112 key_id
= key32_to_tunnel_id(nla_get_be32(tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]));
114 if (tb
[TCA_TUNNEL_KEY_ENC_IPV4_SRC
] &&
115 tb
[TCA_TUNNEL_KEY_ENC_IPV4_DST
]) {
119 saddr
= nla_get_in_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV4_SRC
]);
120 daddr
= nla_get_in_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV4_DST
]);
122 metadata
= __ip_tun_set_dst(saddr
, daddr
, 0, 0,
123 TUNNEL_KEY
, key_id
, 0);
124 } else if (tb
[TCA_TUNNEL_KEY_ENC_IPV6_SRC
] &&
125 tb
[TCA_TUNNEL_KEY_ENC_IPV6_DST
]) {
126 struct in6_addr saddr
;
127 struct in6_addr daddr
;
129 saddr
= nla_get_in6_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV6_SRC
]);
130 daddr
= nla_get_in6_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV6_DST
]);
132 metadata
= __ipv6_tun_set_dst(&saddr
, &daddr
, 0, 0, 0,
133 TUNNEL_KEY
, key_id
, 0);
141 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
;
148 ret
= tcf_hash_create(tn
, parm
->index
, est
, a
,
149 &act_tunnel_key_ops
, bind
, true);
155 tcf_hash_release(*a
, bind
);
160 t
= to_tunnel_key(*a
);
163 params_new
= kzalloc(sizeof(*params_new
), GFP_KERNEL
);
164 if (unlikely(!params_new
)) {
165 if (ret
== ACT_P_CREATED
)
166 tcf_hash_release(*a
, bind
);
170 params_old
= rtnl_dereference(t
->params
);
172 params_new
->action
= parm
->action
;
173 params_new
->tcft_action
= parm
->t_action
;
174 params_new
->tcft_enc_metadata
= metadata
;
176 rcu_assign_pointer(t
->params
, params_new
);
179 kfree_rcu(params_old
, rcu
);
181 if (ret
== ACT_P_CREATED
)
182 tcf_hash_insert(tn
, *a
);
188 tcf_hash_release(*a
, bind
);
192 static void tunnel_key_release(struct tc_action
*a
, int bind
)
194 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
195 struct tcf_tunnel_key_params
*params
;
197 params
= rcu_dereference_protected(t
->params
, 1);
199 if (params
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
)
200 dst_release(¶ms
->tcft_enc_metadata
->dst
);
202 kfree_rcu(params
, rcu
);
205 static int tunnel_key_dump_addresses(struct sk_buff
*skb
,
206 const struct ip_tunnel_info
*info
)
208 unsigned short family
= ip_tunnel_info_af(info
);
210 if (family
== AF_INET
) {
211 __be32 saddr
= info
->key
.u
.ipv4
.src
;
212 __be32 daddr
= info
->key
.u
.ipv4
.dst
;
214 if (!nla_put_in_addr(skb
, TCA_TUNNEL_KEY_ENC_IPV4_SRC
, saddr
) &&
215 !nla_put_in_addr(skb
, TCA_TUNNEL_KEY_ENC_IPV4_DST
, daddr
))
219 if (family
== AF_INET6
) {
220 const struct in6_addr
*saddr6
= &info
->key
.u
.ipv6
.src
;
221 const struct in6_addr
*daddr6
= &info
->key
.u
.ipv6
.dst
;
223 if (!nla_put_in6_addr(skb
,
224 TCA_TUNNEL_KEY_ENC_IPV6_SRC
, saddr6
) &&
225 !nla_put_in6_addr(skb
,
226 TCA_TUNNEL_KEY_ENC_IPV6_DST
, daddr6
))
233 static int tunnel_key_dump(struct sk_buff
*skb
, struct tc_action
*a
,
236 unsigned char *b
= skb_tail_pointer(skb
);
237 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
238 struct tcf_tunnel_key_params
*params
;
239 struct tc_tunnel_key opt
= {
240 .index
= t
->tcf_index
,
241 .refcnt
= t
->tcf_refcnt
- ref
,
242 .bindcnt
= t
->tcf_bindcnt
- bind
,
246 params
= rtnl_dereference(t
->params
);
248 opt
.t_action
= params
->tcft_action
;
249 opt
.action
= params
->action
;
251 if (nla_put(skb
, TCA_TUNNEL_KEY_PARMS
, sizeof(opt
), &opt
))
252 goto nla_put_failure
;
254 if (params
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
) {
255 struct ip_tunnel_key
*key
=
256 ¶ms
->tcft_enc_metadata
->u
.tun_info
.key
;
257 __be32 key_id
= tunnel_id_to_key32(key
->tun_id
);
259 if (nla_put_be32(skb
, TCA_TUNNEL_KEY_ENC_KEY_ID
, key_id
) ||
260 tunnel_key_dump_addresses(skb
,
261 ¶ms
->tcft_enc_metadata
->u
.tun_info
))
262 goto nla_put_failure
;
265 tcf_tm_dump(&tm
, &t
->tcf_tm
);
266 if (nla_put_64bit(skb
, TCA_TUNNEL_KEY_TM
, sizeof(tm
),
267 &tm
, TCA_TUNNEL_KEY_PAD
))
268 goto nla_put_failure
;
277 static int tunnel_key_walker(struct net
*net
, struct sk_buff
*skb
,
278 struct netlink_callback
*cb
, int type
,
279 const struct tc_action_ops
*ops
)
281 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
283 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
);
286 static int tunnel_key_search(struct net
*net
, struct tc_action
**a
, u32 index
)
288 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
290 return tcf_hash_search(tn
, a
, index
);
293 static struct tc_action_ops act_tunnel_key_ops
= {
294 .kind
= "tunnel_key",
295 .type
= TCA_ACT_TUNNEL_KEY
,
296 .owner
= THIS_MODULE
,
297 .act
= tunnel_key_act
,
298 .dump
= tunnel_key_dump
,
299 .init
= tunnel_key_init
,
300 .cleanup
= tunnel_key_release
,
301 .walk
= tunnel_key_walker
,
302 .lookup
= tunnel_key_search
,
303 .size
= sizeof(struct tcf_tunnel_key
),
306 static __net_init
int tunnel_key_init_net(struct net
*net
)
308 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
310 return tc_action_net_init(tn
, &act_tunnel_key_ops
, TUNNEL_KEY_TAB_MASK
);
313 static void __net_exit
tunnel_key_exit_net(struct net
*net
)
315 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
317 tc_action_net_exit(tn
);
320 static struct pernet_operations tunnel_key_net_ops
= {
321 .init
= tunnel_key_init_net
,
322 .exit
= tunnel_key_exit_net
,
323 .id
= &tunnel_key_net_id
,
324 .size
= sizeof(struct tc_action_net
),
327 static int __init
tunnel_key_init_module(void)
329 return tcf_register_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
332 static void __exit
tunnel_key_cleanup_module(void)
334 tcf_unregister_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
337 module_init(tunnel_key_init_module
);
338 module_exit(tunnel_key_cleanup_module
);
340 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
341 MODULE_DESCRIPTION("ip tunnel manipulation actions");
342 MODULE_LICENSE("GPL v2");