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> | |
0ed5269f | 16 | #include <net/geneve.h> |
d0f6dd8a AV |
17 | #include <net/netlink.h> |
18 | #include <net/pkt_sched.h> | |
19 | #include <net/dst.h> | |
d0f6dd8a AV |
20 | |
21 | #include <linux/tc_act/tc_tunnel_key.h> | |
22 | #include <net/tc_act/tc_tunnel_key.h> | |
23 | ||
c7d03a00 | 24 | static unsigned int tunnel_key_net_id; |
d0f6dd8a AV |
25 | static struct tc_action_ops act_tunnel_key_ops; |
26 | ||
27 | static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a, | |
28 | struct tcf_result *res) | |
29 | { | |
30 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
31 | struct tcf_tunnel_key_params *params; | |
32 | int action; | |
33 | ||
7fd4b288 | 34 | params = rcu_dereference_bh(t->params); |
d0f6dd8a AV |
35 | |
36 | tcf_lastuse_update(&t->tcf_tm); | |
37 | bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb); | |
38230a3e | 38 | action = READ_ONCE(t->tcf_action); |
d0f6dd8a AV |
39 | |
40 | switch (params->tcft_action) { | |
41 | case TCA_TUNNEL_KEY_ACT_RELEASE: | |
42 | skb_dst_drop(skb); | |
43 | break; | |
44 | case TCA_TUNNEL_KEY_ACT_SET: | |
45 | skb_dst_drop(skb); | |
46 | skb_dst_set(skb, dst_clone(¶ms->tcft_enc_metadata->dst)); | |
47 | break; | |
48 | default: | |
49 | WARN_ONCE(1, "Bad tunnel_key action %d.\n", | |
50 | params->tcft_action); | |
51 | break; | |
52 | } | |
53 | ||
d0f6dd8a AV |
54 | return action; |
55 | } | |
56 | ||
0ed5269f SH |
57 | static const struct nla_policy |
58 | enc_opts_policy[TCA_TUNNEL_KEY_ENC_OPTS_MAX + 1] = { | |
59 | [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, | |
60 | }; | |
61 | ||
62 | static const struct nla_policy | |
63 | geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = { | |
64 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
65 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
66 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, | |
67 | .len = 128 }, | |
68 | }; | |
69 | ||
70 | static int | |
71 | tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len, | |
72 | struct netlink_ext_ack *extack) | |
73 | { | |
74 | struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
75 | int err, data_len, opt_len; | |
76 | u8 *data; | |
77 | ||
78 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX, | |
79 | nla, geneve_opt_policy, extack); | |
80 | if (err < 0) | |
81 | return err; | |
82 | ||
83 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] || | |
84 | !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] || | |
85 | !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) { | |
86 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); | |
87 | return -EINVAL; | |
88 | } | |
89 | ||
90 | data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]); | |
91 | data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]); | |
92 | if (data_len < 4) { | |
93 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); | |
94 | return -ERANGE; | |
95 | } | |
96 | if (data_len % 4) { | |
97 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); | |
98 | return -ERANGE; | |
99 | } | |
100 | ||
101 | opt_len = sizeof(struct geneve_opt) + data_len; | |
102 | if (dst) { | |
103 | struct geneve_opt *opt = dst; | |
104 | ||
105 | WARN_ON(dst_len < opt_len); | |
106 | ||
107 | opt->opt_class = | |
108 | nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]); | |
109 | opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]); | |
110 | opt->length = data_len / 4; /* length is in units of 4 bytes */ | |
111 | opt->r1 = 0; | |
112 | opt->r2 = 0; | |
113 | opt->r3 = 0; | |
114 | ||
115 | memcpy(opt + 1, data, data_len); | |
116 | } | |
117 | ||
118 | return opt_len; | |
119 | } | |
120 | ||
121 | static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst, | |
122 | int dst_len, struct netlink_ext_ack *extack) | |
123 | { | |
124 | int err, rem, opt_len, len = nla_len(nla), opts_len = 0; | |
125 | const struct nlattr *attr, *head = nla_data(nla); | |
126 | ||
127 | err = nla_validate(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX, | |
128 | enc_opts_policy, extack); | |
129 | if (err) | |
130 | return err; | |
131 | ||
132 | nla_for_each_attr(attr, head, len, rem) { | |
133 | switch (nla_type(attr)) { | |
134 | case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE: | |
135 | opt_len = tunnel_key_copy_geneve_opt(attr, dst, | |
136 | dst_len, extack); | |
137 | if (opt_len < 0) | |
138 | return opt_len; | |
139 | opts_len += opt_len; | |
140 | if (dst) { | |
141 | dst_len -= opt_len; | |
142 | dst += opt_len; | |
143 | } | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
148 | if (!opts_len) { | |
149 | NL_SET_ERR_MSG(extack, "Empty list of tunnel options"); | |
150 | return -EINVAL; | |
151 | } | |
152 | ||
153 | if (rem > 0) { | |
154 | NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes"); | |
155 | return -EINVAL; | |
156 | } | |
157 | ||
158 | return opts_len; | |
159 | } | |
160 | ||
161 | static int tunnel_key_get_opts_len(struct nlattr *nla, | |
162 | struct netlink_ext_ack *extack) | |
163 | { | |
164 | return tunnel_key_copy_opts(nla, NULL, 0, extack); | |
165 | } | |
166 | ||
167 | static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info, | |
168 | int opts_len, struct netlink_ext_ack *extack) | |
169 | { | |
170 | info->options_len = opts_len; | |
171 | switch (nla_type(nla_data(nla))) { | |
172 | case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE: | |
173 | #if IS_ENABLED(CONFIG_INET) | |
174 | info->key.tun_flags |= TUNNEL_GENEVE_OPT; | |
175 | return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info), | |
176 | opts_len, extack); | |
177 | #else | |
178 | return -EAFNOSUPPORT; | |
179 | #endif | |
180 | default: | |
181 | NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type"); | |
182 | return -EINVAL; | |
183 | } | |
184 | } | |
185 | ||
d0f6dd8a AV |
186 | static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = { |
187 | [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) }, | |
188 | [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
189 | [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
190 | [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
191 | [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
192 | [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, | |
75bfbca0 | 193 | [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16}, |
86087e17 | 194 | [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 }, |
0ed5269f | 195 | [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
07a557f4 OG |
196 | [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 }, |
197 | [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 }, | |
d0f6dd8a AV |
198 | }; |
199 | ||
200 | static int tunnel_key_init(struct net *net, struct nlattr *nla, | |
201 | struct nlattr *est, struct tc_action **a, | |
789871bb VB |
202 | int ovr, int bind, bool rtnl_held, |
203 | struct netlink_ext_ack *extack) | |
d0f6dd8a AV |
204 | { |
205 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
206 | struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1]; | |
207 | struct tcf_tunnel_key_params *params_old; | |
208 | struct tcf_tunnel_key_params *params_new; | |
209 | struct metadata_dst *metadata = NULL; | |
210 | struct tc_tunnel_key *parm; | |
211 | struct tcf_tunnel_key *t; | |
212 | bool exists = false; | |
75bfbca0 | 213 | __be16 dst_port = 0; |
0ed5269f | 214 | int opts_len = 0; |
d0f6dd8a | 215 | __be64 key_id; |
86087e17 | 216 | __be16 flags; |
07a557f4 | 217 | u8 tos, ttl; |
d0f6dd8a AV |
218 | int ret = 0; |
219 | int err; | |
220 | ||
9d7298cd SH |
221 | if (!nla) { |
222 | NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed"); | |
d0f6dd8a | 223 | return -EINVAL; |
9d7298cd | 224 | } |
d0f6dd8a | 225 | |
fceb6435 | 226 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy, |
9d7298cd SH |
227 | extack); |
228 | if (err < 0) { | |
229 | NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes"); | |
d0f6dd8a | 230 | return err; |
9d7298cd | 231 | } |
d0f6dd8a | 232 | |
9d7298cd SH |
233 | if (!tb[TCA_TUNNEL_KEY_PARMS]) { |
234 | NL_SET_ERR_MSG(extack, "Missing tunnel key parameters"); | |
d0f6dd8a | 235 | return -EINVAL; |
9d7298cd | 236 | } |
d0f6dd8a AV |
237 | |
238 | parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]); | |
0190c1d4 VB |
239 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
240 | if (err < 0) | |
241 | return err; | |
242 | exists = err; | |
d0f6dd8a AV |
243 | if (exists && bind) |
244 | return 0; | |
245 | ||
246 | switch (parm->t_action) { | |
247 | case TCA_TUNNEL_KEY_ACT_RELEASE: | |
248 | break; | |
249 | case TCA_TUNNEL_KEY_ACT_SET: | |
250 | if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) { | |
9d7298cd | 251 | NL_SET_ERR_MSG(extack, "Missing tunnel key id"); |
d0f6dd8a AV |
252 | ret = -EINVAL; |
253 | goto err_out; | |
254 | } | |
255 | ||
256 | key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID])); | |
257 | ||
86087e17 JB |
258 | flags = TUNNEL_KEY | TUNNEL_CSUM; |
259 | if (tb[TCA_TUNNEL_KEY_NO_CSUM] && | |
260 | nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM])) | |
261 | flags &= ~TUNNEL_CSUM; | |
262 | ||
75bfbca0 HHZ |
263 | if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT]) |
264 | dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]); | |
265 | ||
0ed5269f SH |
266 | if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) { |
267 | opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS], | |
268 | extack); | |
269 | if (opts_len < 0) { | |
270 | ret = opts_len; | |
271 | goto err_out; | |
272 | } | |
273 | } | |
274 | ||
07a557f4 OG |
275 | tos = 0; |
276 | if (tb[TCA_TUNNEL_KEY_ENC_TOS]) | |
277 | tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]); | |
278 | ttl = 0; | |
279 | if (tb[TCA_TUNNEL_KEY_ENC_TTL]) | |
280 | ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]); | |
281 | ||
d0f6dd8a AV |
282 | if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] && |
283 | tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) { | |
284 | __be32 saddr; | |
285 | __be32 daddr; | |
286 | ||
287 | saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]); | |
288 | daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]); | |
289 | ||
07a557f4 | 290 | metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl, |
86087e17 | 291 | dst_port, flags, |
0ed5269f | 292 | key_id, opts_len); |
d0f6dd8a AV |
293 | } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] && |
294 | tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) { | |
295 | struct in6_addr saddr; | |
296 | struct in6_addr daddr; | |
297 | ||
298 | saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]); | |
299 | daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]); | |
300 | ||
07a557f4 | 301 | metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port, |
86087e17 | 302 | 0, flags, |
75bfbca0 | 303 | key_id, 0); |
a1165b59 | 304 | } else { |
9d7298cd | 305 | NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst"); |
a1165b59 SH |
306 | ret = -EINVAL; |
307 | goto err_out; | |
d0f6dd8a AV |
308 | } |
309 | ||
310 | if (!metadata) { | |
9d7298cd | 311 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst"); |
a1165b59 | 312 | ret = -ENOMEM; |
d0f6dd8a AV |
313 | goto err_out; |
314 | } | |
315 | ||
0ed5269f SH |
316 | if (opts_len) { |
317 | ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS], | |
318 | &metadata->u.tun_info, | |
319 | opts_len, extack); | |
320 | if (ret < 0) | |
321 | goto err_out; | |
322 | } | |
323 | ||
d0f6dd8a AV |
324 | metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX; |
325 | break; | |
326 | default: | |
9d7298cd | 327 | NL_SET_ERR_MSG(extack, "Unknown tunnel key action"); |
51d4740f | 328 | ret = -EINVAL; |
d0f6dd8a AV |
329 | goto err_out; |
330 | } | |
331 | ||
332 | if (!exists) { | |
65a206c0 CM |
333 | ret = tcf_idr_create(tn, parm->index, est, a, |
334 | &act_tunnel_key_ops, bind, true); | |
9d7298cd SH |
335 | if (ret) { |
336 | NL_SET_ERR_MSG(extack, "Cannot create TC IDR"); | |
0190c1d4 | 337 | goto err_out; |
9d7298cd | 338 | } |
d0f6dd8a AV |
339 | |
340 | ret = ACT_P_CREATED; | |
4e8ddd7f | 341 | } else if (!ovr) { |
65a206c0 | 342 | tcf_idr_release(*a, bind); |
4e8ddd7f VB |
343 | NL_SET_ERR_MSG(extack, "TC IDR already exists"); |
344 | return -EEXIST; | |
d0f6dd8a AV |
345 | } |
346 | ||
347 | t = to_tunnel_key(*a); | |
348 | ||
349 | ASSERT_RTNL(); | |
350 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); | |
351 | if (unlikely(!params_new)) { | |
4e8ddd7f | 352 | tcf_idr_release(*a, bind); |
9d7298cd | 353 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters"); |
d0f6dd8a AV |
354 | return -ENOMEM; |
355 | } | |
356 | ||
357 | params_old = rtnl_dereference(t->params); | |
358 | ||
38230a3e | 359 | t->tcf_action = parm->action; |
d0f6dd8a AV |
360 | params_new->tcft_action = parm->t_action; |
361 | params_new->tcft_enc_metadata = metadata; | |
362 | ||
363 | rcu_assign_pointer(t->params, params_new); | |
364 | ||
365 | if (params_old) | |
366 | kfree_rcu(params_old, rcu); | |
367 | ||
368 | if (ret == ACT_P_CREATED) | |
65a206c0 | 369 | tcf_idr_insert(tn, *a); |
d0f6dd8a AV |
370 | |
371 | return ret; | |
372 | ||
373 | err_out: | |
374 | if (exists) | |
65a206c0 | 375 | tcf_idr_release(*a, bind); |
0190c1d4 VB |
376 | else |
377 | tcf_idr_cleanup(tn, parm->index); | |
d0f6dd8a AV |
378 | return ret; |
379 | } | |
380 | ||
9a63b255 | 381 | static void tunnel_key_release(struct tc_action *a) |
d0f6dd8a AV |
382 | { |
383 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
384 | struct tcf_tunnel_key_params *params; | |
385 | ||
07c0f09e | 386 | params = rcu_dereference_protected(t->params, 1); |
abdadd3c DC |
387 | if (params) { |
388 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) | |
389 | dst_release(¶ms->tcft_enc_metadata->dst); | |
d0f6dd8a | 390 | |
abdadd3c DC |
391 | kfree_rcu(params, rcu); |
392 | } | |
d0f6dd8a AV |
393 | } |
394 | ||
0ed5269f SH |
395 | static int tunnel_key_geneve_opts_dump(struct sk_buff *skb, |
396 | const struct ip_tunnel_info *info) | |
397 | { | |
398 | int len = info->options_len; | |
399 | u8 *src = (u8 *)(info + 1); | |
400 | struct nlattr *start; | |
401 | ||
402 | start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE); | |
403 | if (!start) | |
404 | return -EMSGSIZE; | |
405 | ||
406 | while (len > 0) { | |
407 | struct geneve_opt *opt = (struct geneve_opt *)src; | |
408 | ||
409 | if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS, | |
410 | opt->opt_class) || | |
411 | nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE, | |
412 | opt->type) || | |
413 | nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA, | |
414 | opt->length * 4, opt + 1)) | |
415 | return -EMSGSIZE; | |
416 | ||
417 | len -= sizeof(struct geneve_opt) + opt->length * 4; | |
418 | src += sizeof(struct geneve_opt) + opt->length * 4; | |
419 | } | |
420 | ||
421 | nla_nest_end(skb, start); | |
422 | return 0; | |
423 | } | |
424 | ||
425 | static int tunnel_key_opts_dump(struct sk_buff *skb, | |
426 | const struct ip_tunnel_info *info) | |
427 | { | |
428 | struct nlattr *start; | |
429 | int err; | |
430 | ||
431 | if (!info->options_len) | |
432 | return 0; | |
433 | ||
434 | start = nla_nest_start(skb, TCA_TUNNEL_KEY_ENC_OPTS); | |
435 | if (!start) | |
436 | return -EMSGSIZE; | |
437 | ||
438 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) { | |
439 | err = tunnel_key_geneve_opts_dump(skb, info); | |
440 | if (err) | |
441 | return err; | |
442 | } else { | |
443 | return -EINVAL; | |
444 | } | |
445 | ||
446 | nla_nest_end(skb, start); | |
447 | return 0; | |
448 | } | |
449 | ||
d0f6dd8a AV |
450 | static int tunnel_key_dump_addresses(struct sk_buff *skb, |
451 | const struct ip_tunnel_info *info) | |
452 | { | |
453 | unsigned short family = ip_tunnel_info_af(info); | |
454 | ||
455 | if (family == AF_INET) { | |
456 | __be32 saddr = info->key.u.ipv4.src; | |
457 | __be32 daddr = info->key.u.ipv4.dst; | |
458 | ||
459 | if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) && | |
460 | !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr)) | |
461 | return 0; | |
462 | } | |
463 | ||
464 | if (family == AF_INET6) { | |
465 | const struct in6_addr *saddr6 = &info->key.u.ipv6.src; | |
466 | const struct in6_addr *daddr6 = &info->key.u.ipv6.dst; | |
467 | ||
468 | if (!nla_put_in6_addr(skb, | |
469 | TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) && | |
470 | !nla_put_in6_addr(skb, | |
471 | TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6)) | |
472 | return 0; | |
473 | } | |
474 | ||
475 | return -EINVAL; | |
476 | } | |
477 | ||
478 | static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a, | |
479 | int bind, int ref) | |
480 | { | |
481 | unsigned char *b = skb_tail_pointer(skb); | |
482 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
483 | struct tcf_tunnel_key_params *params; | |
484 | struct tc_tunnel_key opt = { | |
485 | .index = t->tcf_index, | |
036bb443 VB |
486 | .refcnt = refcount_read(&t->tcf_refcnt) - ref, |
487 | .bindcnt = atomic_read(&t->tcf_bindcnt) - bind, | |
38230a3e | 488 | .action = t->tcf_action, |
d0f6dd8a AV |
489 | }; |
490 | struct tcf_t tm; | |
d0f6dd8a | 491 | |
07c0f09e | 492 | params = rtnl_dereference(t->params); |
d0f6dd8a AV |
493 | |
494 | opt.t_action = params->tcft_action; | |
d0f6dd8a AV |
495 | |
496 | if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt)) | |
497 | goto nla_put_failure; | |
498 | ||
499 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) { | |
0ed5269f SH |
500 | struct ip_tunnel_info *info = |
501 | ¶ms->tcft_enc_metadata->u.tun_info; | |
502 | struct ip_tunnel_key *key = &info->key; | |
d0f6dd8a AV |
503 | __be32 key_id = tunnel_id_to_key32(key->tun_id); |
504 | ||
505 | if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) || | |
506 | tunnel_key_dump_addresses(skb, | |
75bfbca0 | 507 | ¶ms->tcft_enc_metadata->u.tun_info) || |
86087e17 JB |
508 | nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) || |
509 | nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM, | |
0ed5269f SH |
510 | !(key->tun_flags & TUNNEL_CSUM)) || |
511 | tunnel_key_opts_dump(skb, info)) | |
d0f6dd8a | 512 | goto nla_put_failure; |
07a557f4 OG |
513 | |
514 | if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos)) | |
515 | goto nla_put_failure; | |
516 | ||
517 | if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl)) | |
518 | goto nla_put_failure; | |
d0f6dd8a AV |
519 | } |
520 | ||
521 | tcf_tm_dump(&tm, &t->tcf_tm); | |
522 | if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm), | |
523 | &tm, TCA_TUNNEL_KEY_PAD)) | |
524 | goto nla_put_failure; | |
525 | ||
07c0f09e | 526 | return skb->len; |
d0f6dd8a AV |
527 | |
528 | nla_put_failure: | |
529 | nlmsg_trim(skb, b); | |
07c0f09e | 530 | return -1; |
d0f6dd8a AV |
531 | } |
532 | ||
533 | static int tunnel_key_walker(struct net *net, struct sk_buff *skb, | |
534 | struct netlink_callback *cb, int type, | |
41780105 AA |
535 | const struct tc_action_ops *ops, |
536 | struct netlink_ext_ack *extack) | |
d0f6dd8a AV |
537 | { |
538 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
539 | ||
b3620145 | 540 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
d0f6dd8a AV |
541 | } |
542 | ||
331a9295 AA |
543 | static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index, |
544 | struct netlink_ext_ack *extack) | |
d0f6dd8a AV |
545 | { |
546 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
547 | ||
65a206c0 | 548 | return tcf_idr_search(tn, a, index); |
d0f6dd8a AV |
549 | } |
550 | ||
b409074e VB |
551 | static int tunnel_key_delete(struct net *net, u32 index) |
552 | { | |
553 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
554 | ||
555 | return tcf_idr_delete_index(tn, index); | |
556 | } | |
557 | ||
d0f6dd8a AV |
558 | static struct tc_action_ops act_tunnel_key_ops = { |
559 | .kind = "tunnel_key", | |
560 | .type = TCA_ACT_TUNNEL_KEY, | |
561 | .owner = THIS_MODULE, | |
562 | .act = tunnel_key_act, | |
563 | .dump = tunnel_key_dump, | |
564 | .init = tunnel_key_init, | |
565 | .cleanup = tunnel_key_release, | |
566 | .walk = tunnel_key_walker, | |
567 | .lookup = tunnel_key_search, | |
b409074e | 568 | .delete = tunnel_key_delete, |
d0f6dd8a AV |
569 | .size = sizeof(struct tcf_tunnel_key), |
570 | }; | |
571 | ||
572 | static __net_init int tunnel_key_init_net(struct net *net) | |
573 | { | |
574 | struct tc_action_net *tn = net_generic(net, tunnel_key_net_id); | |
575 | ||
c7e460ce | 576 | return tc_action_net_init(tn, &act_tunnel_key_ops); |
d0f6dd8a AV |
577 | } |
578 | ||
039af9c6 | 579 | static void __net_exit tunnel_key_exit_net(struct list_head *net_list) |
d0f6dd8a | 580 | { |
039af9c6 | 581 | tc_action_net_exit(net_list, tunnel_key_net_id); |
d0f6dd8a AV |
582 | } |
583 | ||
584 | static struct pernet_operations tunnel_key_net_ops = { | |
585 | .init = tunnel_key_init_net, | |
039af9c6 | 586 | .exit_batch = tunnel_key_exit_net, |
d0f6dd8a AV |
587 | .id = &tunnel_key_net_id, |
588 | .size = sizeof(struct tc_action_net), | |
589 | }; | |
590 | ||
591 | static int __init tunnel_key_init_module(void) | |
592 | { | |
593 | return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
594 | } | |
595 | ||
596 | static void __exit tunnel_key_cleanup_module(void) | |
597 | { | |
598 | tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
599 | } | |
600 | ||
601 | module_init(tunnel_key_init_module); | |
602 | module_exit(tunnel_key_cleanup_module); | |
603 | ||
604 | MODULE_AUTHOR("Amir Vadai <amir@vadai.me>"); | |
605 | MODULE_DESCRIPTION("ip tunnel manipulation actions"); | |
606 | MODULE_LICENSE("GPL v2"); |