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