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