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>
20 #include <linux/tc_act/tc_tunnel_key.h>
21 #include <net/tc_act/tc_tunnel_key.h>
23 #define TUNNEL_KEY_TAB_MASK 15
25 static unsigned int tunnel_key_net_id
;
26 static struct tc_action_ops act_tunnel_key_ops
;
28 static int tunnel_key_act(struct sk_buff
*skb
, const struct tc_action
*a
,
29 struct tcf_result
*res
)
31 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
32 struct tcf_tunnel_key_params
*params
;
37 params
= rcu_dereference(t
->params
);
39 tcf_lastuse_update(&t
->tcf_tm
);
40 bstats_cpu_update(this_cpu_ptr(t
->common
.cpu_bstats
), skb
);
41 action
= params
->action
;
43 switch (params
->tcft_action
) {
44 case TCA_TUNNEL_KEY_ACT_RELEASE
:
47 case TCA_TUNNEL_KEY_ACT_SET
:
49 skb_dst_set(skb
, dst_clone(¶ms
->tcft_enc_metadata
->dst
));
52 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
62 static const struct nla_policy tunnel_key_policy
[TCA_TUNNEL_KEY_MAX
+ 1] = {
63 [TCA_TUNNEL_KEY_PARMS
] = { .len
= sizeof(struct tc_tunnel_key
) },
64 [TCA_TUNNEL_KEY_ENC_IPV4_SRC
] = { .type
= NLA_U32
},
65 [TCA_TUNNEL_KEY_ENC_IPV4_DST
] = { .type
= NLA_U32
},
66 [TCA_TUNNEL_KEY_ENC_IPV6_SRC
] = { .len
= sizeof(struct in6_addr
) },
67 [TCA_TUNNEL_KEY_ENC_IPV6_DST
] = { .len
= sizeof(struct in6_addr
) },
68 [TCA_TUNNEL_KEY_ENC_KEY_ID
] = { .type
= NLA_U32
},
69 [TCA_TUNNEL_KEY_ENC_DST_PORT
] = {.type
= NLA_U16
},
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
;
92 err
= nla_parse_nested(tb
, TCA_TUNNEL_KEY_MAX
, nla
, tunnel_key_policy
);
96 if (!tb
[TCA_TUNNEL_KEY_PARMS
])
99 parm
= nla_data(tb
[TCA_TUNNEL_KEY_PARMS
]);
100 exists
= tcf_hash_check(tn
, parm
->index
, a
, bind
);
104 switch (parm
->t_action
) {
105 case TCA_TUNNEL_KEY_ACT_RELEASE
:
107 case TCA_TUNNEL_KEY_ACT_SET
:
108 if (!tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]) {
113 key_id
= key32_to_tunnel_id(nla_get_be32(tb
[TCA_TUNNEL_KEY_ENC_KEY_ID
]));
115 if (tb
[TCA_TUNNEL_KEY_ENC_DST_PORT
])
116 dst_port
= nla_get_be16(tb
[TCA_TUNNEL_KEY_ENC_DST_PORT
]);
118 if (tb
[TCA_TUNNEL_KEY_ENC_IPV4_SRC
] &&
119 tb
[TCA_TUNNEL_KEY_ENC_IPV4_DST
]) {
123 saddr
= nla_get_in_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV4_SRC
]);
124 daddr
= nla_get_in_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV4_DST
]);
126 metadata
= __ip_tun_set_dst(saddr
, daddr
, 0, 0,
127 dst_port
, TUNNEL_KEY
,
129 } else if (tb
[TCA_TUNNEL_KEY_ENC_IPV6_SRC
] &&
130 tb
[TCA_TUNNEL_KEY_ENC_IPV6_DST
]) {
131 struct in6_addr saddr
;
132 struct in6_addr daddr
;
134 saddr
= nla_get_in6_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV6_SRC
]);
135 daddr
= nla_get_in6_addr(tb
[TCA_TUNNEL_KEY_ENC_IPV6_DST
]);
137 metadata
= __ipv6_tun_set_dst(&saddr
, &daddr
, 0, 0, dst_port
,
147 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
;
154 ret
= tcf_hash_create(tn
, parm
->index
, est
, a
,
155 &act_tunnel_key_ops
, bind
, true);
161 tcf_hash_release(*a
, bind
);
166 t
= to_tunnel_key(*a
);
169 params_new
= kzalloc(sizeof(*params_new
), GFP_KERNEL
);
170 if (unlikely(!params_new
)) {
171 if (ret
== ACT_P_CREATED
)
172 tcf_hash_release(*a
, bind
);
176 params_old
= rtnl_dereference(t
->params
);
178 params_new
->action
= parm
->action
;
179 params_new
->tcft_action
= parm
->t_action
;
180 params_new
->tcft_enc_metadata
= metadata
;
182 rcu_assign_pointer(t
->params
, params_new
);
185 kfree_rcu(params_old
, rcu
);
187 if (ret
== ACT_P_CREATED
)
188 tcf_hash_insert(tn
, *a
);
194 tcf_hash_release(*a
, bind
);
198 static void tunnel_key_release(struct tc_action
*a
, int bind
)
200 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
201 struct tcf_tunnel_key_params
*params
;
203 params
= rcu_dereference_protected(t
->params
, 1);
205 if (params
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
)
206 dst_release(¶ms
->tcft_enc_metadata
->dst
);
208 kfree_rcu(params
, rcu
);
211 static int tunnel_key_dump_addresses(struct sk_buff
*skb
,
212 const struct ip_tunnel_info
*info
)
214 unsigned short family
= ip_tunnel_info_af(info
);
216 if (family
== AF_INET
) {
217 __be32 saddr
= info
->key
.u
.ipv4
.src
;
218 __be32 daddr
= info
->key
.u
.ipv4
.dst
;
220 if (!nla_put_in_addr(skb
, TCA_TUNNEL_KEY_ENC_IPV4_SRC
, saddr
) &&
221 !nla_put_in_addr(skb
, TCA_TUNNEL_KEY_ENC_IPV4_DST
, daddr
))
225 if (family
== AF_INET6
) {
226 const struct in6_addr
*saddr6
= &info
->key
.u
.ipv6
.src
;
227 const struct in6_addr
*daddr6
= &info
->key
.u
.ipv6
.dst
;
229 if (!nla_put_in6_addr(skb
,
230 TCA_TUNNEL_KEY_ENC_IPV6_SRC
, saddr6
) &&
231 !nla_put_in6_addr(skb
,
232 TCA_TUNNEL_KEY_ENC_IPV6_DST
, daddr6
))
239 static int tunnel_key_dump(struct sk_buff
*skb
, struct tc_action
*a
,
242 unsigned char *b
= skb_tail_pointer(skb
);
243 struct tcf_tunnel_key
*t
= to_tunnel_key(a
);
244 struct tcf_tunnel_key_params
*params
;
245 struct tc_tunnel_key opt
= {
246 .index
= t
->tcf_index
,
247 .refcnt
= t
->tcf_refcnt
- ref
,
248 .bindcnt
= t
->tcf_bindcnt
- bind
,
252 params
= rtnl_dereference(t
->params
);
254 opt
.t_action
= params
->tcft_action
;
255 opt
.action
= params
->action
;
257 if (nla_put(skb
, TCA_TUNNEL_KEY_PARMS
, sizeof(opt
), &opt
))
258 goto nla_put_failure
;
260 if (params
->tcft_action
== TCA_TUNNEL_KEY_ACT_SET
) {
261 struct ip_tunnel_key
*key
=
262 ¶ms
->tcft_enc_metadata
->u
.tun_info
.key
;
263 __be32 key_id
= tunnel_id_to_key32(key
->tun_id
);
265 if (nla_put_be32(skb
, TCA_TUNNEL_KEY_ENC_KEY_ID
, key_id
) ||
266 tunnel_key_dump_addresses(skb
,
267 ¶ms
->tcft_enc_metadata
->u
.tun_info
) ||
268 nla_put_be16(skb
, TCA_TUNNEL_KEY_ENC_DST_PORT
, key
->tp_dst
))
269 goto nla_put_failure
;
272 tcf_tm_dump(&tm
, &t
->tcf_tm
);
273 if (nla_put_64bit(skb
, TCA_TUNNEL_KEY_TM
, sizeof(tm
),
274 &tm
, TCA_TUNNEL_KEY_PAD
))
275 goto nla_put_failure
;
284 static int tunnel_key_walker(struct net
*net
, struct sk_buff
*skb
,
285 struct netlink_callback
*cb
, int type
,
286 const struct tc_action_ops
*ops
)
288 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
290 return tcf_generic_walker(tn
, skb
, cb
, type
, ops
);
293 static int tunnel_key_search(struct net
*net
, struct tc_action
**a
, u32 index
)
295 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
297 return tcf_hash_search(tn
, a
, index
);
300 static struct tc_action_ops act_tunnel_key_ops
= {
301 .kind
= "tunnel_key",
302 .type
= TCA_ACT_TUNNEL_KEY
,
303 .owner
= THIS_MODULE
,
304 .act
= tunnel_key_act
,
305 .dump
= tunnel_key_dump
,
306 .init
= tunnel_key_init
,
307 .cleanup
= tunnel_key_release
,
308 .walk
= tunnel_key_walker
,
309 .lookup
= tunnel_key_search
,
310 .size
= sizeof(struct tcf_tunnel_key
),
313 static __net_init
int tunnel_key_init_net(struct net
*net
)
315 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
317 return tc_action_net_init(tn
, &act_tunnel_key_ops
, TUNNEL_KEY_TAB_MASK
);
320 static void __net_exit
tunnel_key_exit_net(struct net
*net
)
322 struct tc_action_net
*tn
= net_generic(net
, tunnel_key_net_id
);
324 tc_action_net_exit(tn
);
327 static struct pernet_operations tunnel_key_net_ops
= {
328 .init
= tunnel_key_init_net
,
329 .exit
= tunnel_key_exit_net
,
330 .id
= &tunnel_key_net_id
,
331 .size
= sizeof(struct tc_action_net
),
334 static int __init
tunnel_key_init_module(void)
336 return tcf_register_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
339 static void __exit
tunnel_key_cleanup_module(void)
341 tcf_unregister_action(&act_tunnel_key_ops
, &tunnel_key_net_ops
);
344 module_init(tunnel_key_init_module
);
345 module_exit(tunnel_key_cleanup_module
);
347 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
348 MODULE_DESCRIPTION("ip tunnel manipulation actions");
349 MODULE_LICENSE("GPL v2");