Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
d0f6dd8a AV |
2 | /* |
3 | * Copyright (c) 2016, Amir Vadai <amir@vadai.me> | |
4 | * Copyright (c) 2016, Mellanox Technologies. All rights reserved. | |
d0f6dd8a AV |
5 | */ |
6 | ||
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> | |
0ed5269f | 12 | #include <net/geneve.h> |
fca3f91c | 13 | #include <net/vxlan.h> |
e20d4ff2 | 14 | #include <net/erspan.h> |
d0f6dd8a AV |
15 | #include <net/netlink.h> |
16 | #include <net/pkt_sched.h> | |
17 | #include <net/dst.h> | |
e5fdabac | 18 | #include <net/pkt_cls.h> |
871cf386 | 19 | #include <net/tc_wrapper.h> |
d0f6dd8a AV |
20 | |
21 | #include <linux/tc_act/tc_tunnel_key.h> | |
22 | #include <net/tc_act/tc_tunnel_key.h> | |
23 | ||
d0f6dd8a AV |
24 | static struct tc_action_ops act_tunnel_key_ops; |
25 | ||
871cf386 PT |
26 | TC_INDIRECT_SCOPE int tunnel_key_act(struct sk_buff *skb, |
27 | const struct tc_action *a, | |
28 | struct tcf_result *res) | |
d0f6dd8a AV |
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); | |
5e1ad95b | 37 | tcf_action_update_bstats(&t->common, 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] = { | |
fca3f91c XL |
59 | [TCA_TUNNEL_KEY_ENC_OPTS_UNSPEC] = { |
60 | .strict_start_type = TCA_TUNNEL_KEY_ENC_OPTS_VXLAN }, | |
0ed5269f | 61 | [TCA_TUNNEL_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, |
fca3f91c | 62 | [TCA_TUNNEL_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED }, |
e20d4ff2 | 63 | [TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED }, |
0ed5269f SH |
64 | }; |
65 | ||
66 | static const struct nla_policy | |
67 | geneve_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1] = { | |
68 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
69 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
70 | [TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, | |
71 | .len = 128 }, | |
72 | }; | |
73 | ||
fca3f91c XL |
74 | static const struct nla_policy |
75 | vxlan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1] = { | |
76 | [TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 }, | |
77 | }; | |
78 | ||
e20d4ff2 XL |
79 | static const struct nla_policy |
80 | erspan_opt_policy[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1] = { | |
81 | [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 }, | |
82 | [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, | |
83 | [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, | |
84 | [TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, | |
85 | }; | |
86 | ||
0ed5269f SH |
87 | static int |
88 | tunnel_key_copy_geneve_opt(const struct nlattr *nla, void *dst, int dst_len, | |
89 | struct netlink_ext_ack *extack) | |
90 | { | |
91 | struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
92 | int err, data_len, opt_len; | |
93 | u8 *data; | |
94 | ||
8cb08174 JB |
95 | err = nla_parse_nested_deprecated(tb, |
96 | TCA_TUNNEL_KEY_ENC_OPT_GENEVE_MAX, | |
97 | nla, geneve_opt_policy, extack); | |
0ed5269f SH |
98 | if (err < 0) |
99 | return err; | |
100 | ||
101 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS] || | |
102 | !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE] || | |
103 | !tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]) { | |
104 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); | |
105 | return -EINVAL; | |
106 | } | |
107 | ||
108 | data = nla_data(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]); | |
109 | data_len = nla_len(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA]); | |
110 | if (data_len < 4) { | |
111 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); | |
112 | return -ERANGE; | |
113 | } | |
114 | if (data_len % 4) { | |
115 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); | |
116 | return -ERANGE; | |
117 | } | |
118 | ||
119 | opt_len = sizeof(struct geneve_opt) + data_len; | |
120 | if (dst) { | |
121 | struct geneve_opt *opt = dst; | |
122 | ||
123 | WARN_ON(dst_len < opt_len); | |
124 | ||
125 | opt->opt_class = | |
126 | nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS]); | |
127 | opt->type = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE]); | |
128 | opt->length = data_len / 4; /* length is in units of 4 bytes */ | |
129 | opt->r1 = 0; | |
130 | opt->r2 = 0; | |
131 | opt->r3 = 0; | |
132 | ||
133 | memcpy(opt + 1, data, data_len); | |
134 | } | |
135 | ||
136 | return opt_len; | |
137 | } | |
138 | ||
fca3f91c XL |
139 | static int |
140 | tunnel_key_copy_vxlan_opt(const struct nlattr *nla, void *dst, int dst_len, | |
141 | struct netlink_ext_ack *extack) | |
142 | { | |
143 | struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX + 1]; | |
144 | int err; | |
145 | ||
146 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_MAX, nla, | |
147 | vxlan_opt_policy, extack); | |
148 | if (err < 0) | |
149 | return err; | |
150 | ||
151 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]) { | |
152 | NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp"); | |
153 | return -EINVAL; | |
154 | } | |
155 | ||
156 | if (dst) { | |
157 | struct vxlan_metadata *md = dst; | |
158 | ||
159 | md->gbp = nla_get_u32(tb[TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP]); | |
13e6ce98 | 160 | md->gbp &= VXLAN_GBP_MASK; |
fca3f91c XL |
161 | } |
162 | ||
163 | return sizeof(struct vxlan_metadata); | |
164 | } | |
165 | ||
e20d4ff2 XL |
166 | static int |
167 | tunnel_key_copy_erspan_opt(const struct nlattr *nla, void *dst, int dst_len, | |
168 | struct netlink_ext_ack *extack) | |
169 | { | |
170 | struct nlattr *tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX + 1]; | |
171 | int err; | |
172 | u8 ver; | |
173 | ||
174 | err = nla_parse_nested(tb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_MAX, nla, | |
175 | erspan_opt_policy, extack); | |
176 | if (err < 0) | |
177 | return err; | |
178 | ||
179 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]) { | |
180 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver"); | |
181 | return -EINVAL; | |
182 | } | |
183 | ||
184 | ver = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER]); | |
185 | if (ver == 1) { | |
186 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX]) { | |
187 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index"); | |
188 | return -EINVAL; | |
189 | } | |
190 | } else if (ver == 2) { | |
191 | if (!tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR] || | |
192 | !tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID]) { | |
193 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid"); | |
194 | return -EINVAL; | |
195 | } | |
196 | } else { | |
197 | NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect"); | |
198 | return -EINVAL; | |
199 | } | |
200 | ||
201 | if (dst) { | |
202 | struct erspan_metadata *md = dst; | |
203 | ||
204 | md->version = ver; | |
205 | if (ver == 1) { | |
206 | nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX]; | |
207 | md->u.index = nla_get_be32(nla); | |
208 | } else { | |
209 | nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR]; | |
210 | md->u.md2.dir = nla_get_u8(nla); | |
211 | nla = tb[TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID]; | |
212 | set_hwid(&md->u.md2, nla_get_u8(nla)); | |
213 | } | |
214 | } | |
215 | ||
216 | return sizeof(struct erspan_metadata); | |
217 | } | |
218 | ||
0ed5269f SH |
219 | static int tunnel_key_copy_opts(const struct nlattr *nla, u8 *dst, |
220 | int dst_len, struct netlink_ext_ack *extack) | |
221 | { | |
fca3f91c | 222 | int err, rem, opt_len, len = nla_len(nla), opts_len = 0, type = 0; |
0ed5269f SH |
223 | const struct nlattr *attr, *head = nla_data(nla); |
224 | ||
8cb08174 JB |
225 | err = nla_validate_deprecated(head, len, TCA_TUNNEL_KEY_ENC_OPTS_MAX, |
226 | enc_opts_policy, extack); | |
0ed5269f SH |
227 | if (err) |
228 | return err; | |
229 | ||
230 | nla_for_each_attr(attr, head, len, rem) { | |
231 | switch (nla_type(attr)) { | |
232 | case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE: | |
fca3f91c XL |
233 | if (type && type != TUNNEL_GENEVE_OPT) { |
234 | NL_SET_ERR_MSG(extack, "Duplicate type for geneve options"); | |
235 | return -EINVAL; | |
236 | } | |
0ed5269f SH |
237 | opt_len = tunnel_key_copy_geneve_opt(attr, dst, |
238 | dst_len, extack); | |
239 | if (opt_len < 0) | |
240 | return opt_len; | |
241 | opts_len += opt_len; | |
4f0e97d0 XL |
242 | if (opts_len > IP_TUNNEL_OPTS_MAX) { |
243 | NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); | |
244 | return -EINVAL; | |
245 | } | |
0ed5269f SH |
246 | if (dst) { |
247 | dst_len -= opt_len; | |
248 | dst += opt_len; | |
249 | } | |
fca3f91c XL |
250 | type = TUNNEL_GENEVE_OPT; |
251 | break; | |
252 | case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN: | |
253 | if (type) { | |
254 | NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options"); | |
255 | return -EINVAL; | |
256 | } | |
257 | opt_len = tunnel_key_copy_vxlan_opt(attr, dst, | |
258 | dst_len, extack); | |
259 | if (opt_len < 0) | |
260 | return opt_len; | |
261 | opts_len += opt_len; | |
262 | type = TUNNEL_VXLAN_OPT; | |
0ed5269f | 263 | break; |
e20d4ff2 XL |
264 | case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN: |
265 | if (type) { | |
266 | NL_SET_ERR_MSG(extack, "Duplicate type for erspan options"); | |
267 | return -EINVAL; | |
268 | } | |
269 | opt_len = tunnel_key_copy_erspan_opt(attr, dst, | |
270 | dst_len, extack); | |
271 | if (opt_len < 0) | |
272 | return opt_len; | |
273 | opts_len += opt_len; | |
274 | type = TUNNEL_ERSPAN_OPT; | |
275 | break; | |
0ed5269f SH |
276 | } |
277 | } | |
278 | ||
279 | if (!opts_len) { | |
280 | NL_SET_ERR_MSG(extack, "Empty list of tunnel options"); | |
281 | return -EINVAL; | |
282 | } | |
283 | ||
284 | if (rem > 0) { | |
285 | NL_SET_ERR_MSG(extack, "Trailing data after parsing tunnel key options attributes"); | |
286 | return -EINVAL; | |
287 | } | |
288 | ||
289 | return opts_len; | |
290 | } | |
291 | ||
292 | static int tunnel_key_get_opts_len(struct nlattr *nla, | |
293 | struct netlink_ext_ack *extack) | |
294 | { | |
295 | return tunnel_key_copy_opts(nla, NULL, 0, extack); | |
296 | } | |
297 | ||
298 | static int tunnel_key_opts_set(struct nlattr *nla, struct ip_tunnel_info *info, | |
299 | int opts_len, struct netlink_ext_ack *extack) | |
300 | { | |
301 | info->options_len = opts_len; | |
302 | switch (nla_type(nla_data(nla))) { | |
303 | case TCA_TUNNEL_KEY_ENC_OPTS_GENEVE: | |
304 | #if IS_ENABLED(CONFIG_INET) | |
305 | info->key.tun_flags |= TUNNEL_GENEVE_OPT; | |
306 | return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info), | |
307 | opts_len, extack); | |
308 | #else | |
309 | return -EAFNOSUPPORT; | |
fca3f91c XL |
310 | #endif |
311 | case TCA_TUNNEL_KEY_ENC_OPTS_VXLAN: | |
312 | #if IS_ENABLED(CONFIG_INET) | |
313 | info->key.tun_flags |= TUNNEL_VXLAN_OPT; | |
314 | return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info), | |
315 | opts_len, extack); | |
316 | #else | |
317 | return -EAFNOSUPPORT; | |
e20d4ff2 XL |
318 | #endif |
319 | case TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN: | |
320 | #if IS_ENABLED(CONFIG_INET) | |
321 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; | |
322 | return tunnel_key_copy_opts(nla, ip_tunnel_info_opts(info), | |
323 | opts_len, extack); | |
324 | #else | |
325 | return -EAFNOSUPPORT; | |
0ed5269f SH |
326 | #endif |
327 | default: | |
328 | NL_SET_ERR_MSG(extack, "Cannot set tunnel options for unknown tunnel type"); | |
329 | return -EINVAL; | |
330 | } | |
331 | } | |
332 | ||
d0f6dd8a AV |
333 | static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = { |
334 | [TCA_TUNNEL_KEY_PARMS] = { .len = sizeof(struct tc_tunnel_key) }, | |
335 | [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
336 | [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
337 | [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
338 | [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
339 | [TCA_TUNNEL_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, | |
75bfbca0 | 340 | [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16}, |
86087e17 | 341 | [TCA_TUNNEL_KEY_NO_CSUM] = { .type = NLA_U8 }, |
0ed5269f | 342 | [TCA_TUNNEL_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
07a557f4 OG |
343 | [TCA_TUNNEL_KEY_ENC_TOS] = { .type = NLA_U8 }, |
344 | [TCA_TUNNEL_KEY_ENC_TTL] = { .type = NLA_U8 }, | |
d0f6dd8a AV |
345 | }; |
346 | ||
9174c3df DC |
347 | static void tunnel_key_release_params(struct tcf_tunnel_key_params *p) |
348 | { | |
349 | if (!p) | |
350 | return; | |
4177c5d9 | 351 | if (p->tcft_action == TCA_TUNNEL_KEY_ACT_SET) |
9174c3df | 352 | dst_release(&p->tcft_enc_metadata->dst); |
4177c5d9 | 353 | |
9174c3df DC |
354 | kfree_rcu(p, rcu); |
355 | } | |
356 | ||
d0f6dd8a AV |
357 | static int tunnel_key_init(struct net *net, struct nlattr *nla, |
358 | struct nlattr *est, struct tc_action **a, | |
abbb0d33 | 359 | struct tcf_proto *tp, u32 act_flags, |
789871bb | 360 | struct netlink_ext_ack *extack) |
d0f6dd8a | 361 | { |
acd0a7ab | 362 | struct tc_action_net *tn = net_generic(net, act_tunnel_key_ops.net_id); |
695176bf | 363 | bool bind = act_flags & TCA_ACT_FLAGS_BIND; |
d0f6dd8a | 364 | struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1]; |
d0f6dd8a AV |
365 | struct tcf_tunnel_key_params *params_new; |
366 | struct metadata_dst *metadata = NULL; | |
e5fdabac | 367 | struct tcf_chain *goto_ch = NULL; |
d0f6dd8a AV |
368 | struct tc_tunnel_key *parm; |
369 | struct tcf_tunnel_key *t; | |
370 | bool exists = false; | |
75bfbca0 | 371 | __be16 dst_port = 0; |
80ef0f22 | 372 | __be64 key_id = 0; |
0ed5269f | 373 | int opts_len = 0; |
80ef0f22 | 374 | __be16 flags = 0; |
07a557f4 | 375 | u8 tos, ttl; |
d0f6dd8a | 376 | int ret = 0; |
7be8ef2c | 377 | u32 index; |
d0f6dd8a AV |
378 | int err; |
379 | ||
9d7298cd SH |
380 | if (!nla) { |
381 | NL_SET_ERR_MSG(extack, "Tunnel requires attributes to be passed"); | |
d0f6dd8a | 382 | return -EINVAL; |
9d7298cd | 383 | } |
d0f6dd8a | 384 | |
8cb08174 JB |
385 | err = nla_parse_nested_deprecated(tb, TCA_TUNNEL_KEY_MAX, nla, |
386 | tunnel_key_policy, extack); | |
9d7298cd SH |
387 | if (err < 0) { |
388 | NL_SET_ERR_MSG(extack, "Failed to parse nested tunnel key attributes"); | |
d0f6dd8a | 389 | return err; |
9d7298cd | 390 | } |
d0f6dd8a | 391 | |
9d7298cd SH |
392 | if (!tb[TCA_TUNNEL_KEY_PARMS]) { |
393 | NL_SET_ERR_MSG(extack, "Missing tunnel key parameters"); | |
d0f6dd8a | 394 | return -EINVAL; |
9d7298cd | 395 | } |
d0f6dd8a AV |
396 | |
397 | parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]); | |
7be8ef2c DL |
398 | index = parm->index; |
399 | err = tcf_idr_check_alloc(tn, &index, a, bind); | |
0190c1d4 VB |
400 | if (err < 0) |
401 | return err; | |
402 | exists = err; | |
d0f6dd8a AV |
403 | if (exists && bind) |
404 | return 0; | |
405 | ||
406 | switch (parm->t_action) { | |
407 | case TCA_TUNNEL_KEY_ACT_RELEASE: | |
408 | break; | |
409 | case TCA_TUNNEL_KEY_ACT_SET: | |
80ef0f22 AN |
410 | if (tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) { |
411 | __be32 key32; | |
d0f6dd8a | 412 | |
80ef0f22 AN |
413 | key32 = nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]); |
414 | key_id = key32_to_tunnel_id(key32); | |
415 | flags = TUNNEL_KEY; | |
416 | } | |
d0f6dd8a | 417 | |
80ef0f22 | 418 | flags |= TUNNEL_CSUM; |
86087e17 JB |
419 | if (tb[TCA_TUNNEL_KEY_NO_CSUM] && |
420 | nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM])) | |
421 | flags &= ~TUNNEL_CSUM; | |
422 | ||
75bfbca0 HHZ |
423 | if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT]) |
424 | dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]); | |
425 | ||
0ed5269f SH |
426 | if (tb[TCA_TUNNEL_KEY_ENC_OPTS]) { |
427 | opts_len = tunnel_key_get_opts_len(tb[TCA_TUNNEL_KEY_ENC_OPTS], | |
428 | extack); | |
429 | if (opts_len < 0) { | |
430 | ret = opts_len; | |
431 | goto err_out; | |
432 | } | |
433 | } | |
434 | ||
07a557f4 OG |
435 | tos = 0; |
436 | if (tb[TCA_TUNNEL_KEY_ENC_TOS]) | |
437 | tos = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TOS]); | |
438 | ttl = 0; | |
439 | if (tb[TCA_TUNNEL_KEY_ENC_TTL]) | |
440 | ttl = nla_get_u8(tb[TCA_TUNNEL_KEY_ENC_TTL]); | |
441 | ||
d0f6dd8a AV |
442 | if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] && |
443 | tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) { | |
444 | __be32 saddr; | |
445 | __be32 daddr; | |
446 | ||
447 | saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]); | |
448 | daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]); | |
449 | ||
07a557f4 | 450 | metadata = __ip_tun_set_dst(saddr, daddr, tos, ttl, |
86087e17 | 451 | dst_port, flags, |
0ed5269f | 452 | key_id, opts_len); |
d0f6dd8a AV |
453 | } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] && |
454 | tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) { | |
455 | struct in6_addr saddr; | |
456 | struct in6_addr daddr; | |
457 | ||
458 | saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]); | |
459 | daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]); | |
460 | ||
07a557f4 | 461 | metadata = __ipv6_tun_set_dst(&saddr, &daddr, tos, ttl, dst_port, |
86087e17 | 462 | 0, flags, |
a7a12b5a | 463 | key_id, opts_len); |
a1165b59 | 464 | } else { |
9d7298cd | 465 | NL_SET_ERR_MSG(extack, "Missing either ipv4 or ipv6 src and dst"); |
a1165b59 SH |
466 | ret = -EINVAL; |
467 | goto err_out; | |
d0f6dd8a AV |
468 | } |
469 | ||
470 | if (!metadata) { | |
9d7298cd | 471 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel metadata dst"); |
a1165b59 | 472 | ret = -ENOMEM; |
d0f6dd8a AV |
473 | goto err_out; |
474 | } | |
475 | ||
41411e2f | 476 | #ifdef CONFIG_DST_CACHE |
477 | ret = dst_cache_init(&metadata->u.tun_info.dst_cache, GFP_KERNEL); | |
478 | if (ret) | |
479 | goto release_tun_meta; | |
480 | #endif | |
481 | ||
0ed5269f SH |
482 | if (opts_len) { |
483 | ret = tunnel_key_opts_set(tb[TCA_TUNNEL_KEY_ENC_OPTS], | |
484 | &metadata->u.tun_info, | |
485 | opts_len, extack); | |
486 | if (ret < 0) | |
4177c5d9 | 487 | goto release_tun_meta; |
0ed5269f SH |
488 | } |
489 | ||
d0f6dd8a AV |
490 | metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX; |
491 | break; | |
492 | default: | |
9d7298cd | 493 | NL_SET_ERR_MSG(extack, "Unknown tunnel key action"); |
51d4740f | 494 | ret = -EINVAL; |
d0f6dd8a AV |
495 | goto err_out; |
496 | } | |
497 | ||
498 | if (!exists) { | |
e3822678 VB |
499 | ret = tcf_idr_create_from_flags(tn, index, est, a, |
500 | &act_tunnel_key_ops, bind, | |
501 | act_flags); | |
9d7298cd SH |
502 | if (ret) { |
503 | NL_SET_ERR_MSG(extack, "Cannot create TC IDR"); | |
4177c5d9 | 504 | goto release_tun_meta; |
9d7298cd | 505 | } |
d0f6dd8a AV |
506 | |
507 | ret = ACT_P_CREATED; | |
695176bf | 508 | } else if (!(act_flags & TCA_ACT_FLAGS_REPLACE)) { |
4e8ddd7f | 509 | NL_SET_ERR_MSG(extack, "TC IDR already exists"); |
ee28bb56 | 510 | ret = -EEXIST; |
4177c5d9 | 511 | goto release_tun_meta; |
d0f6dd8a AV |
512 | } |
513 | ||
e5fdabac DC |
514 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
515 | if (err < 0) { | |
516 | ret = err; | |
517 | exists = true; | |
518 | goto release_tun_meta; | |
519 | } | |
d0f6dd8a AV |
520 | t = to_tunnel_key(*a); |
521 | ||
d0f6dd8a AV |
522 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
523 | if (unlikely(!params_new)) { | |
9d7298cd | 524 | NL_SET_ERR_MSG(extack, "Cannot allocate tunnel key parameters"); |
ee28bb56 DC |
525 | ret = -ENOMEM; |
526 | exists = true; | |
e5fdabac | 527 | goto put_chain; |
d0f6dd8a | 528 | } |
d0f6dd8a AV |
529 | params_new->tcft_action = parm->t_action; |
530 | params_new->tcft_enc_metadata = metadata; | |
531 | ||
653cd284 | 532 | spin_lock_bh(&t->tcf_lock); |
e5fdabac | 533 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
445d3749 PM |
534 | params_new = rcu_replace_pointer(t->params, params_new, |
535 | lockdep_is_held(&t->tcf_lock)); | |
653cd284 | 536 | spin_unlock_bh(&t->tcf_lock); |
9174c3df | 537 | tunnel_key_release_params(params_new); |
e5fdabac DC |
538 | if (goto_ch) |
539 | tcf_chain_put_by_act(goto_ch); | |
d0f6dd8a | 540 | |
d0f6dd8a AV |
541 | return ret; |
542 | ||
e5fdabac DC |
543 | put_chain: |
544 | if (goto_ch) | |
545 | tcf_chain_put_by_act(goto_ch); | |
546 | ||
ee28bb56 | 547 | release_tun_meta: |
a3df633a VB |
548 | if (metadata) |
549 | dst_release(&metadata->dst); | |
ee28bb56 | 550 | |
d0f6dd8a AV |
551 | err_out: |
552 | if (exists) | |
65a206c0 | 553 | tcf_idr_release(*a, bind); |
0190c1d4 | 554 | else |
7be8ef2c | 555 | tcf_idr_cleanup(tn, index); |
d0f6dd8a AV |
556 | return ret; |
557 | } | |
558 | ||
9a63b255 | 559 | static void tunnel_key_release(struct tc_action *a) |
d0f6dd8a AV |
560 | { |
561 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
562 | struct tcf_tunnel_key_params *params; | |
563 | ||
07c0f09e | 564 | params = rcu_dereference_protected(t->params, 1); |
9174c3df | 565 | tunnel_key_release_params(params); |
d0f6dd8a AV |
566 | } |
567 | ||
0ed5269f SH |
568 | static int tunnel_key_geneve_opts_dump(struct sk_buff *skb, |
569 | const struct ip_tunnel_info *info) | |
570 | { | |
571 | int len = info->options_len; | |
572 | u8 *src = (u8 *)(info + 1); | |
573 | struct nlattr *start; | |
574 | ||
ae0be8de | 575 | start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_GENEVE); |
0ed5269f SH |
576 | if (!start) |
577 | return -EMSGSIZE; | |
578 | ||
579 | while (len > 0) { | |
580 | struct geneve_opt *opt = (struct geneve_opt *)src; | |
581 | ||
582 | if (nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_CLASS, | |
583 | opt->opt_class) || | |
584 | nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_TYPE, | |
585 | opt->type) || | |
586 | nla_put(skb, TCA_TUNNEL_KEY_ENC_OPT_GENEVE_DATA, | |
a162c351 CW |
587 | opt->length * 4, opt + 1)) { |
588 | nla_nest_cancel(skb, start); | |
0ed5269f | 589 | return -EMSGSIZE; |
a162c351 | 590 | } |
0ed5269f SH |
591 | |
592 | len -= sizeof(struct geneve_opt) + opt->length * 4; | |
593 | src += sizeof(struct geneve_opt) + opt->length * 4; | |
594 | } | |
595 | ||
596 | nla_nest_end(skb, start); | |
597 | return 0; | |
598 | } | |
599 | ||
fca3f91c XL |
600 | static int tunnel_key_vxlan_opts_dump(struct sk_buff *skb, |
601 | const struct ip_tunnel_info *info) | |
602 | { | |
603 | struct vxlan_metadata *md = (struct vxlan_metadata *)(info + 1); | |
604 | struct nlattr *start; | |
605 | ||
606 | start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_VXLAN); | |
607 | if (!start) | |
608 | return -EMSGSIZE; | |
609 | ||
610 | if (nla_put_u32(skb, TCA_TUNNEL_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) { | |
611 | nla_nest_cancel(skb, start); | |
612 | return -EMSGSIZE; | |
613 | } | |
614 | ||
615 | nla_nest_end(skb, start); | |
616 | return 0; | |
617 | } | |
618 | ||
e20d4ff2 XL |
619 | static int tunnel_key_erspan_opts_dump(struct sk_buff *skb, |
620 | const struct ip_tunnel_info *info) | |
621 | { | |
622 | struct erspan_metadata *md = (struct erspan_metadata *)(info + 1); | |
623 | struct nlattr *start; | |
624 | ||
625 | start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS_ERSPAN); | |
626 | if (!start) | |
627 | return -EMSGSIZE; | |
628 | ||
629 | if (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_VER, md->version)) | |
630 | goto err; | |
631 | ||
632 | if (md->version == 1 && | |
633 | nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index)) | |
634 | goto err; | |
635 | ||
636 | if (md->version == 2 && | |
637 | (nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_DIR, | |
638 | md->u.md2.dir) || | |
639 | nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_OPT_ERSPAN_HWID, | |
640 | get_hwid(&md->u.md2)))) | |
641 | goto err; | |
642 | ||
643 | nla_nest_end(skb, start); | |
644 | return 0; | |
645 | err: | |
646 | nla_nest_cancel(skb, start); | |
647 | return -EMSGSIZE; | |
648 | } | |
649 | ||
0ed5269f SH |
650 | static int tunnel_key_opts_dump(struct sk_buff *skb, |
651 | const struct ip_tunnel_info *info) | |
652 | { | |
653 | struct nlattr *start; | |
a162c351 | 654 | int err = -EINVAL; |
0ed5269f SH |
655 | |
656 | if (!info->options_len) | |
657 | return 0; | |
658 | ||
ae0be8de | 659 | start = nla_nest_start_noflag(skb, TCA_TUNNEL_KEY_ENC_OPTS); |
0ed5269f SH |
660 | if (!start) |
661 | return -EMSGSIZE; | |
662 | ||
663 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) { | |
664 | err = tunnel_key_geneve_opts_dump(skb, info); | |
665 | if (err) | |
a162c351 | 666 | goto err_out; |
fca3f91c XL |
667 | } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) { |
668 | err = tunnel_key_vxlan_opts_dump(skb, info); | |
669 | if (err) | |
670 | goto err_out; | |
e20d4ff2 XL |
671 | } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) { |
672 | err = tunnel_key_erspan_opts_dump(skb, info); | |
673 | if (err) | |
674 | goto err_out; | |
0ed5269f | 675 | } else { |
a162c351 CW |
676 | err_out: |
677 | nla_nest_cancel(skb, start); | |
678 | return err; | |
0ed5269f SH |
679 | } |
680 | ||
681 | nla_nest_end(skb, start); | |
682 | return 0; | |
683 | } | |
684 | ||
d0f6dd8a AV |
685 | static int tunnel_key_dump_addresses(struct sk_buff *skb, |
686 | const struct ip_tunnel_info *info) | |
687 | { | |
688 | unsigned short family = ip_tunnel_info_af(info); | |
689 | ||
690 | if (family == AF_INET) { | |
691 | __be32 saddr = info->key.u.ipv4.src; | |
692 | __be32 daddr = info->key.u.ipv4.dst; | |
693 | ||
694 | if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) && | |
695 | !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr)) | |
696 | return 0; | |
697 | } | |
698 | ||
699 | if (family == AF_INET6) { | |
700 | const struct in6_addr *saddr6 = &info->key.u.ipv6.src; | |
701 | const struct in6_addr *daddr6 = &info->key.u.ipv6.dst; | |
702 | ||
703 | if (!nla_put_in6_addr(skb, | |
704 | TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) && | |
705 | !nla_put_in6_addr(skb, | |
706 | TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6)) | |
707 | return 0; | |
708 | } | |
709 | ||
710 | return -EINVAL; | |
711 | } | |
712 | ||
713 | static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a, | |
714 | int bind, int ref) | |
715 | { | |
716 | unsigned char *b = skb_tail_pointer(skb); | |
717 | struct tcf_tunnel_key *t = to_tunnel_key(a); | |
718 | struct tcf_tunnel_key_params *params; | |
719 | struct tc_tunnel_key opt = { | |
720 | .index = t->tcf_index, | |
036bb443 VB |
721 | .refcnt = refcount_read(&t->tcf_refcnt) - ref, |
722 | .bindcnt = atomic_read(&t->tcf_bindcnt) - bind, | |
d0f6dd8a AV |
723 | }; |
724 | struct tcf_t tm; | |
d0f6dd8a | 725 | |
653cd284 | 726 | spin_lock_bh(&t->tcf_lock); |
729e0126 VB |
727 | params = rcu_dereference_protected(t->params, |
728 | lockdep_is_held(&t->tcf_lock)); | |
729 | opt.action = t->tcf_action; | |
d0f6dd8a | 730 | opt.t_action = params->tcft_action; |
d0f6dd8a AV |
731 | |
732 | if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt)) | |
733 | goto nla_put_failure; | |
734 | ||
735 | if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) { | |
0ed5269f SH |
736 | struct ip_tunnel_info *info = |
737 | ¶ms->tcft_enc_metadata->u.tun_info; | |
738 | struct ip_tunnel_key *key = &info->key; | |
d0f6dd8a AV |
739 | __be32 key_id = tunnel_id_to_key32(key->tun_id); |
740 | ||
80ef0f22 AN |
741 | if (((key->tun_flags & TUNNEL_KEY) && |
742 | nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id)) || | |
d0f6dd8a | 743 | tunnel_key_dump_addresses(skb, |
75bfbca0 | 744 | ¶ms->tcft_enc_metadata->u.tun_info) || |
1c25324c AN |
745 | (key->tp_dst && |
746 | nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, | |
747 | key->tp_dst)) || | |
86087e17 | 748 | nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM, |
0ed5269f SH |
749 | !(key->tun_flags & TUNNEL_CSUM)) || |
750 | tunnel_key_opts_dump(skb, info)) | |
d0f6dd8a | 751 | goto nla_put_failure; |
07a557f4 OG |
752 | |
753 | if (key->tos && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TOS, key->tos)) | |
754 | goto nla_put_failure; | |
755 | ||
756 | if (key->ttl && nla_put_u8(skb, TCA_TUNNEL_KEY_ENC_TTL, key->ttl)) | |
757 | goto nla_put_failure; | |
d0f6dd8a AV |
758 | } |
759 | ||
760 | tcf_tm_dump(&tm, &t->tcf_tm); | |
761 | if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm), | |
762 | &tm, TCA_TUNNEL_KEY_PAD)) | |
763 | goto nla_put_failure; | |
653cd284 | 764 | spin_unlock_bh(&t->tcf_lock); |
d0f6dd8a | 765 | |
07c0f09e | 766 | return skb->len; |
d0f6dd8a AV |
767 | |
768 | nla_put_failure: | |
653cd284 | 769 | spin_unlock_bh(&t->tcf_lock); |
d0f6dd8a | 770 | nlmsg_trim(skb, b); |
07c0f09e | 771 | return -1; |
d0f6dd8a AV |
772 | } |
773 | ||
c54e1d92 BZ |
774 | static void tcf_tunnel_encap_put_tunnel(void *priv) |
775 | { | |
776 | struct ip_tunnel_info *tunnel = priv; | |
777 | ||
778 | kfree(tunnel); | |
779 | } | |
780 | ||
781 | static int tcf_tunnel_encap_get_tunnel(struct flow_action_entry *entry, | |
782 | const struct tc_action *act) | |
783 | { | |
784 | entry->tunnel = tcf_tunnel_info_copy(act); | |
785 | if (!entry->tunnel) | |
786 | return -ENOMEM; | |
787 | entry->destructor = tcf_tunnel_encap_put_tunnel; | |
788 | entry->destructor_priv = entry->tunnel; | |
789 | return 0; | |
790 | } | |
791 | ||
792 | static int tcf_tunnel_key_offload_act_setup(struct tc_action *act, | |
793 | void *entry_data, | |
794 | u32 *index_inc, | |
c2ccf84e IS |
795 | bool bind, |
796 | struct netlink_ext_ack *extack) | |
c54e1d92 BZ |
797 | { |
798 | int err; | |
799 | ||
800 | if (bind) { | |
801 | struct flow_action_entry *entry = entry_data; | |
802 | ||
803 | if (is_tcf_tunnel_set(act)) { | |
804 | entry->id = FLOW_ACTION_TUNNEL_ENCAP; | |
805 | err = tcf_tunnel_encap_get_tunnel(entry, act); | |
806 | if (err) | |
807 | return err; | |
808 | } else if (is_tcf_tunnel_release(act)) { | |
809 | entry->id = FLOW_ACTION_TUNNEL_DECAP; | |
810 | } else { | |
ee367d44 | 811 | NL_SET_ERR_MSG_MOD(extack, "Unsupported tunnel key mode offload"); |
c54e1d92 BZ |
812 | return -EOPNOTSUPP; |
813 | } | |
814 | *index_inc = 1; | |
815 | } else { | |
8cbfe939 BZ |
816 | struct flow_offload_action *fl_action = entry_data; |
817 | ||
818 | if (is_tcf_tunnel_set(act)) | |
819 | fl_action->id = FLOW_ACTION_TUNNEL_ENCAP; | |
820 | else if (is_tcf_tunnel_release(act)) | |
821 | fl_action->id = FLOW_ACTION_TUNNEL_DECAP; | |
822 | else | |
823 | return -EOPNOTSUPP; | |
c54e1d92 BZ |
824 | } |
825 | ||
826 | return 0; | |
827 | } | |
828 | ||
d0f6dd8a AV |
829 | static struct tc_action_ops act_tunnel_key_ops = { |
830 | .kind = "tunnel_key", | |
eddd2cf1 | 831 | .id = TCA_ID_TUNNEL_KEY, |
d0f6dd8a AV |
832 | .owner = THIS_MODULE, |
833 | .act = tunnel_key_act, | |
834 | .dump = tunnel_key_dump, | |
835 | .init = tunnel_key_init, | |
836 | .cleanup = tunnel_key_release, | |
c54e1d92 | 837 | .offload_act_setup = tcf_tunnel_key_offload_act_setup, |
d0f6dd8a AV |
838 | .size = sizeof(struct tcf_tunnel_key), |
839 | }; | |
840 | ||
841 | static __net_init int tunnel_key_init_net(struct net *net) | |
842 | { | |
acd0a7ab | 843 | struct tc_action_net *tn = net_generic(net, act_tunnel_key_ops.net_id); |
d0f6dd8a | 844 | |
981471bd | 845 | return tc_action_net_init(net, tn, &act_tunnel_key_ops); |
d0f6dd8a AV |
846 | } |
847 | ||
039af9c6 | 848 | static void __net_exit tunnel_key_exit_net(struct list_head *net_list) |
d0f6dd8a | 849 | { |
acd0a7ab | 850 | tc_action_net_exit(net_list, act_tunnel_key_ops.net_id); |
d0f6dd8a AV |
851 | } |
852 | ||
853 | static struct pernet_operations tunnel_key_net_ops = { | |
854 | .init = tunnel_key_init_net, | |
039af9c6 | 855 | .exit_batch = tunnel_key_exit_net, |
acd0a7ab | 856 | .id = &act_tunnel_key_ops.net_id, |
d0f6dd8a AV |
857 | .size = sizeof(struct tc_action_net), |
858 | }; | |
859 | ||
860 | static int __init tunnel_key_init_module(void) | |
861 | { | |
862 | return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
863 | } | |
864 | ||
865 | static void __exit tunnel_key_cleanup_module(void) | |
866 | { | |
867 | tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops); | |
868 | } | |
869 | ||
870 | module_init(tunnel_key_init_module); | |
871 | module_exit(tunnel_key_cleanup_module); | |
872 | ||
873 | MODULE_AUTHOR("Amir Vadai <amir@vadai.me>"); | |
874 | MODULE_DESCRIPTION("ip tunnel manipulation actions"); | |
875 | MODULE_LICENSE("GPL v2"); |