Commit | Line | Data |
---|---|---|
d0f6dd8a AV |
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> | |
d0f6dd8a AV |
19 | |
20 | #include <linux/tc_act/tc_tunnel_key.h> | |
21 | #include <net/tc_act/tc_tunnel_key.h> | |
22 | ||
c7d03a00 | 23 | static unsigned int tunnel_key_net_id; |
d0f6dd8a AV |
24 | static struct tc_action_ops act_tunnel_key_ops; |
25 | ||
26 | static 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(¶ms->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 | ||
60 | static 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 }, | |
75bfbca0 | 67 | [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16}, |
86087e17 | 68 | [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 }, |
d0f6dd8a AV |
69 | }; |
70 | ||
71 | static int tunnel_key_init(struct net *net, struct nlattr *nla, | |
72 | struct nlattr *est, struct tc_action **a, | |
589dad6d | 73 | int ovr, int bind, struct netlink_ext_ack *extack) |
d0f6dd8a AV |
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; | |
75bfbca0 | 83 | __be16 dst_port = 0; |
d0f6dd8a | 84 | __be64 key_id; |
86087e17 | 85 | __be16 flags; |
d0f6dd8a AV |
86 | int ret = 0; |
87 | int err; | |
88 | ||
9d7298cd SH |
89 | if (!nla) { |
90 | NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed"); | |
d0f6dd8a | 91 | return -EINVAL; |
9d7298cd | 92 | } |
d0f6dd8a | 93 | |
fceb6435 | 94 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy, |
9d7298cd SH |
95 | extack); |
96 | if (err < 0) { | |
97 | NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes"); | |
d0f6dd8a | 98 | return err; |
9d7298cd | 99 | } |
d0f6dd8a | 100 | |
9d7298cd SH |
101 | if (!tb[TCA_TUNNEL_KEY_PARMS]) { |
102 | NL_SET_ERR_MSG(extack, "Missing tunnel key parameters"); | |
d0f6dd8a | 103 | return -EINVAL; |
9d7298cd | 104 | } |
d0f6dd8a AV |
105 | |
106 | parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]); | |
65a206c0 | 107 | exists = tcf_idr_check(tn, parm->index, a, bind); |
d0f6dd8a AV |
108 | if (exists && bind) |
109 | return 0; | |
110 | ||
111 | switch (parm->t_action) { | |
112 | case TCA_TUNNEL_KEY_ACT_RELEASE: | |
113 | break; | |
114 | case TCA_TUNNEL_KEY_ACT_SET: | |
115 | if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) { | |
9d7298cd | 116 | NL_SET_ERR_MSG(extack, "Missing tunnel key id"); |
d0f6dd8a AV |
117 | ret = -EINVAL; |
118 | goto err_out; | |
119 | } | |
120 | ||
121 | key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID])); | |
122 | ||
86087e17 JB |
123 | flags = TUNNEL_KEY | TUNNEL_CSUM; |
124 | if (tb[TCA_TUNNEL_KEY_NO_CSUM] && | |
125 | nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM])) | |
126 | flags &= ~TUNNEL_CSUM; | |
127 | ||
75bfbca0 HHZ |
128 | if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT]) |
129 | dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]); | |
130 | ||
d0f6dd8a AV |
131 | if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] && |
132 | tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) { | |
133 | __be32 saddr; | |
134 | __be32 daddr; | |
135 | ||
136 | saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]); | |
137 | daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]); | |
138 | ||
139 | metadata = __ip_tun_set_dst(saddr, daddr, 0, 0, | |
86087e17 | 140 | dst_port, flags, |
75bfbca0 | 141 | key_id, 0); |
d0f6dd8a AV |
142 | } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] && |
143 | tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) { | |
144 | struct in6_addr saddr; | |
145 | struct in6_addr daddr; | |
146 | ||
147 | saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]); | |
148 | daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]); | |
149 | ||
dc594ecd | 150 | metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port, |
86087e17 | 151 | 0, flags, |
75bfbca0 | 152 | key_id, 0); |
a1165b59 | 153 | } else { |
9d7298cd | 154 | NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst"); |
a1165b59 SH |
155 | ret = -EINVAL; |
156 | goto err_out; | |
d0f6dd8a AV |
157 | } |
158 | ||
159 | if (!metadata) { | |
9d7298cd | 160 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst"); |
a1165b59 | 161 | ret = -ENOMEM; |
d0f6dd8a AV |
162 | goto err_out; |
163 | } | |
164 | ||
165 | metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX; | |
166 | break; | |
167 | default: | |
9d7298cd | 168 | NL_SET_ERR_MSG(extack, "Unknown tunnel key action"); |
51d4740f | 169 | ret = -EINVAL; |
d0f6dd8a AV |
170 | goto err_out; |
171 | } | |
172 | ||
173 | if (!exists) { | |
65a206c0 CM |
174 | ret = tcf_idr_create(tn, parm->index, est, a, |
175 | &act_tunnel_key_ops, bind, true); | |
9d7298cd SH |
176 | if (ret) { |
177 | NL_SET_ERR_MSG(extack, "Cannot create TC IDR"); | |
d0f6dd8a | 178 | return ret; |
9d7298cd | 179 | } |
d0f6dd8a AV |
180 | |
181 | ret = ACT_P_CREATED; | |
182 | } else { | |
65a206c0 | 183 | tcf_idr_release(*a, bind); |
9d7298cd SH |
184 | if (!ovr) { |
185 | NL_SET_ERR_MSG(extack, "TC IDR already exists"); | |
d0f6dd8a | 186 | return -EEXIST; |
9d7298cd | 187 | } |
d0f6dd8a AV |
188 | } |
189 | ||
190 | t = to_tunnel_key(*a); | |
191 | ||
192 | ASSERT_RTNL(); | |
193 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); | |
194 | if (unlikely(!params_new)) { | |
195 | if (ret == ACT_P_CREATED) | |
65a206c0 | 196 | tcf_idr_release(*a, bind); |
9d7298cd | 197 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters"); |
d0f6dd8a AV |
198 | return -ENOMEM; |
199 | } | |
200 | ||
201 | params_old = rtnl_dereference(t->params); | |
202 | ||
203 | params_new->action = parm->action; | |
204 | params_new->tcft_action = parm->t_action; | |
205 | params_new->tcft_enc_metadata = metadata; | |
206 | ||
207 | rcu_assign_pointer(t->params, params_new); | |
208 | ||
209 | if (params_old) | |
210 | kfree_rcu(params_old, rcu); | |
211 | ||
212 | if (ret == ACT_P_CREATED) | |
65a206c0 | 213 | tcf_idr_insert(tn, *a); |
d0f6dd8a AV |
214 | |
215 | return ret; | |
216 | ||
217 | err_out: | |
218 | if (exists) | |
65a206c0 | 219 | tcf_idr_release(*a, bind); |
d0f6dd8a AV |
220 | return ret; |
221 | } | |
222 | ||
9a63b255 | 223 | static void tunnel_key_release(struct tc_action *a) |
d0f6dd8a AV |
224 | { |
225 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
226 | struct tcf_tunnel_key_params *params; | |
227 | ||
07c0f09e | 228 | params = rcu_dereference_protected(t->params, 1); |
abdadd3c DC |
229 | if (params) { |
230 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) | |
231 | dst_release(¶ms->tcft_enc_metadata->dst); | |
d0f6dd8a | 232 | |
abdadd3c DC |
233 | kfree_rcu(params, rcu); |
234 | } | |
d0f6dd8a AV |
235 | } |
236 | ||
237 | static int tunnel_key_dump_addresses(struct sk_buff *skb, | |
238 | const struct ip_tunnel_info *info) | |
239 | { | |
240 | unsigned short family = ip_tunnel_info_af(info); | |
241 | ||
242 | if (family == AF_INET) { | |
243 | __be32 saddr = info->key.u.ipv4.src; | |
244 | __be32 daddr = info->key.u.ipv4.dst; | |
245 | ||
246 | if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) && | |
247 | !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr)) | |
248 | return 0; | |
249 | } | |
250 | ||
251 | if (family == AF_INET6) { | |
252 | const struct in6_addr *saddr6 = &info->key.u.ipv6.src; | |
253 | const struct in6_addr *daddr6 = &info->key.u.ipv6.dst; | |
254 | ||
255 | if (!nla_put_in6_addr(skb, | |
256 | TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) && | |
257 | !nla_put_in6_addr(skb, | |
258 | TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6)) | |
259 | return 0; | |
260 | } | |
261 | ||
262 | return -EINVAL; | |
263 | } | |
264 | ||
265 | static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a, | |
266 | int bind, int ref) | |
267 | { | |
268 | unsigned char *b = skb_tail_pointer(skb); | |
269 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
270 | struct tcf_tunnel_key_params *params; | |
271 | struct tc_tunnel_key opt = { | |
272 | .index = t->tcf_index, | |
273 | .refcnt = t->tcf_refcnt - ref, | |
274 | .bindcnt = t->tcf_bindcnt - bind, | |
275 | }; | |
276 | struct tcf_t tm; | |
d0f6dd8a | 277 | |
07c0f09e | 278 | params = rtnl_dereference(t->params); |
d0f6dd8a AV |
279 | |
280 | opt.t_action = params->tcft_action; | |
281 | opt.action = params->action; | |
282 | ||
283 | if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt)) | |
284 | goto nla_put_failure; | |
285 | ||
286 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) { | |
287 | struct ip_tunnel_key *key = | |
288 | ¶ms->tcft_enc_metadata->u.tun_info.key; | |
289 | __be32 key_id = tunnel_id_to_key32(key->tun_id); | |
290 | ||
291 | if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) || | |
292 | tunnel_key_dump_addresses(skb, | |
75bfbca0 | 293 | ¶ms->tcft_enc_metadata->u.tun_info) || |
86087e17 JB |
294 | nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) || |
295 | nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM, | |
296 | !(key->tun_flags & TUNNEL_CSUM))) | |
d0f6dd8a AV |
297 | goto nla_put_failure; |
298 | } | |
299 | ||
300 | tcf_tm_dump(&tm, &t->tcf_tm); | |
301 | if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm), | |
302 | &tm, TCA_TUNNEL_KEY_PAD)) | |
303 | goto nla_put_failure; | |
304 | ||
07c0f09e | 305 | return skb->len; |
d0f6dd8a AV |
306 | |
307 | nla_put_failure: | |
308 | nlmsg_trim(skb, b); | |
07c0f09e | 309 | return -1; |
d0f6dd8a AV |
310 | } |
311 | ||
312 | static int tunnel_key_walker(struct net *net, struct sk_buff *skb, | |
313 | struct netlink_callback *cb, int type, | |
41780105 AA |
314 | const struct tc_action_ops *ops, |
315 | struct netlink_ext_ack *extack) | |
d0f6dd8a AV |
316 | { |
317 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
318 | ||
b3620145 | 319 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
d0f6dd8a AV |
320 | } |
321 | ||
331a9295 AA |
322 | static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index, |
323 | struct netlink_ext_ack *extack) | |
d0f6dd8a AV |
324 | { |
325 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
326 | ||
65a206c0 | 327 | return tcf_idr_search(tn, a, index); |
d0f6dd8a AV |
328 | } |
329 | ||
330 | static struct tc_action_ops act_tunnel_key_ops = { | |
331 | .kind = "tunnel_key", | |
332 | .type = TCA_ACT_TUNNEL_KEY, | |
333 | .owner = THIS_MODULE, | |
334 | .act = tunnel_key_act, | |
335 | .dump = tunnel_key_dump, | |
336 | .init = tunnel_key_init, | |
337 | .cleanup = tunnel_key_release, | |
338 | .walk = tunnel_key_walker, | |
339 | .lookup = tunnel_key_search, | |
340 | .size = sizeof(struct tcf_tunnel_key), | |
341 | }; | |
342 | ||
343 | static __net_init int tunnel_key_init_net(struct net *net) | |
344 | { | |
345 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
346 | ||
c7e460ce | 347 | return tc_action_net_init(tn, &act_tunnel_key_ops); |
d0f6dd8a AV |
348 | } |
349 | ||
039af9c6 | 350 | static void __net_exit tunnel_key_exit_net(struct list_head *net_list) |
d0f6dd8a | 351 | { |
039af9c6 | 352 | tc_action_net_exit(net_list, tunnel_key_net_id); |
d0f6dd8a AV |
353 | } |
354 | ||
355 | static struct pernet_operations tunnel_key_net_ops = { | |
356 | .init = tunnel_key_init_net, | |
039af9c6 | 357 | .exit_batch = tunnel_key_exit_net, |
d0f6dd8a AV |
358 | .id = &tunnel_key_net_id, |
359 | .size = sizeof(struct tc_action_net), | |
360 | }; | |
361 | ||
362 | static int __init tunnel_key_init_module(void) | |
363 | { | |
364 | return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
365 | } | |
366 | ||
367 | static void __exit tunnel_key_cleanup_module(void) | |
368 | { | |
369 | tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
370 | } | |
371 | ||
372 | module_init(tunnel_key_init_module); | |
373 | module_exit(tunnel_key_cleanup_module); | |
374 | ||
375 | MODULE_AUTHOR("Amir Vadai <amir@vadai.me>"); | |
376 | MODULE_DESCRIPTION("ip tunnel manipulation actions"); | |
377 | MODULE_LICENSE("GPL v2"); |