Commit | Line | Data |
---|---|---|
c9422999 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
0e6fbc5b PS |
2 | /* |
3 | * Copyright (c) 2013 Nicira, Inc. | |
0e6fbc5b PS |
4 | */ |
5 | ||
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | ||
8 | #include <linux/types.h> | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/skbuff.h> | |
11 | #include <linux/netdevice.h> | |
12 | #include <linux/in.h> | |
13 | #include <linux/if_arp.h> | |
0e6fbc5b PS |
14 | #include <linux/init.h> |
15 | #include <linux/in6.h> | |
16 | #include <linux/inetdevice.h> | |
17 | #include <linux/netfilter_ipv4.h> | |
18 | #include <linux/etherdevice.h> | |
19 | #include <linux/if_ether.h> | |
20 | #include <linux/if_vlan.h> | |
e7030878 | 21 | #include <linux/static_key.h> |
0e6fbc5b PS |
22 | |
23 | #include <net/ip.h> | |
24 | #include <net/icmp.h> | |
25 | #include <net/protocol.h> | |
26 | #include <net/ip_tunnels.h> | |
058214a4 | 27 | #include <net/ip6_tunnel.h> |
0e6fbc5b PS |
28 | #include <net/arp.h> |
29 | #include <net/checksum.h> | |
30 | #include <net/dsfield.h> | |
31 | #include <net/inet_ecn.h> | |
32 | #include <net/xfrm.h> | |
33 | #include <net/net_namespace.h> | |
34 | #include <net/netns/generic.h> | |
35 | #include <net/rtnetlink.h> | |
63d008a4 | 36 | #include <net/dst_metadata.h> |
4ece4778 | 37 | #include <net/geneve.h> |
edf31cbb | 38 | #include <net/vxlan.h> |
b0a21810 | 39 | #include <net/erspan.h> |
0e6fbc5b | 40 | |
55c2bc14 TH |
41 | const struct ip_tunnel_encap_ops __rcu * |
42 | iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly; | |
43 | EXPORT_SYMBOL(iptun_encaps); | |
44 | ||
058214a4 TH |
45 | const struct ip6_tnl_encap_ops __rcu * |
46 | ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly; | |
47 | EXPORT_SYMBOL(ip6tun_encaps); | |
48 | ||
039f5062 PS |
49 | void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb, |
50 | __be32 src, __be32 dst, __u8 proto, | |
51 | __u8 tos, __u8 ttl, __be16 df, bool xnet) | |
0e6fbc5b | 52 | { |
bc22a0e2 | 53 | int pkt_len = skb->len - skb_inner_network_offset(skb); |
f859b0f6 | 54 | struct net *net = dev_net(rt->dst.dev); |
039f5062 | 55 | struct net_device *dev = skb->dev; |
0e6fbc5b PS |
56 | struct iphdr *iph; |
57 | int err; | |
58 | ||
963a88b3 ND |
59 | skb_scrub_packet(skb, xnet); |
60 | ||
bf8d85d4 | 61 | skb_clear_hash_if_not_l4(skb); |
0e6fbc5b PS |
62 | skb_dst_set(skb, &rt->dst); |
63 | memset(IPCB(skb), 0, sizeof(*IPCB(skb))); | |
64 | ||
65 | /* Push down and install the IP header. */ | |
78a3694d | 66 | skb_push(skb, sizeof(struct iphdr)); |
0e6fbc5b PS |
67 | skb_reset_network_header(skb); |
68 | ||
69 | iph = ip_hdr(skb); | |
70 | ||
71 | iph->version = 4; | |
72 | iph->ihl = sizeof(struct iphdr) >> 2; | |
16f7eb2b | 73 | iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df; |
0e6fbc5b PS |
74 | iph->protocol = proto; |
75 | iph->tos = tos; | |
76 | iph->daddr = dst; | |
77 | iph->saddr = src; | |
78 | iph->ttl = ttl; | |
f859b0f6 | 79 | __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1); |
0e6fbc5b | 80 | |
33224b16 | 81 | err = ip_local_out(net, sk, skb); |
5684abf7 XL |
82 | |
83 | if (dev) { | |
84 | if (unlikely(net_xmit_eval(err))) | |
85 | pkt_len = 0; | |
86 | iptunnel_xmit_stats(dev, pkt_len); | |
87 | } | |
0e6fbc5b PS |
88 | } |
89 | EXPORT_SYMBOL_GPL(iptunnel_xmit); | |
3d7b46cd | 90 | |
a6d5bbf3 JB |
91 | int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len, |
92 | __be16 inner_proto, bool raw_proto, bool xnet) | |
3d7b46cd PS |
93 | { |
94 | if (unlikely(!pskb_may_pull(skb, hdr_len))) | |
95 | return -ENOMEM; | |
96 | ||
97 | skb_pull_rcsum(skb, hdr_len); | |
98 | ||
a6d5bbf3 | 99 | if (!raw_proto && inner_proto == htons(ETH_P_TEB)) { |
1245dfc8 | 100 | struct ethhdr *eh; |
3d7b46cd PS |
101 | |
102 | if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) | |
103 | return -ENOMEM; | |
104 | ||
1245dfc8 | 105 | eh = (struct ethhdr *)skb->data; |
d181ddca | 106 | if (likely(eth_proto_is_802_3(eh->h_proto))) |
3d7b46cd PS |
107 | skb->protocol = eh->h_proto; |
108 | else | |
109 | skb->protocol = htons(ETH_P_802_2); | |
110 | ||
111 | } else { | |
112 | skb->protocol = inner_proto; | |
113 | } | |
114 | ||
7539fadc | 115 | skb_clear_hash_if_not_l4(skb); |
3e2ed0c2 | 116 | __vlan_hwaccel_clear_tag(skb); |
3d7b46cd | 117 | skb_set_queue_mapping(skb, 0); |
7f290c94 | 118 | skb_scrub_packet(skb, xnet); |
a09a4c8d JG |
119 | |
120 | return iptunnel_pull_offloads(skb); | |
3d7b46cd | 121 | } |
a6d5bbf3 | 122 | EXPORT_SYMBOL_GPL(__iptunnel_pull_header); |
2d26f0a3 | 123 | |
63d008a4 JB |
124 | struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md, |
125 | gfp_t flags) | |
126 | { | |
127 | struct metadata_dst *res; | |
128 | struct ip_tunnel_info *dst, *src; | |
129 | ||
3fcece12 JK |
130 | if (!md || md->type != METADATA_IP_TUNNEL || |
131 | md->u.tun_info.mode & IP_TUNNEL_INFO_TX) | |
63d008a4 JB |
132 | return NULL; |
133 | ||
f52f11ec XL |
134 | src = &md->u.tun_info; |
135 | res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags); | |
63d008a4 JB |
136 | if (!res) |
137 | return NULL; | |
138 | ||
139 | dst = &res->u.tun_info; | |
63d008a4 JB |
140 | dst->key.tun_id = src->key.tun_id; |
141 | if (src->mode & IP_TUNNEL_INFO_IPV6) | |
142 | memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src, | |
143 | sizeof(struct in6_addr)); | |
144 | else | |
145 | dst->key.u.ipv4.dst = src->key.u.ipv4.src; | |
7bdca378 | 146 | dst->key.tun_flags = src->key.tun_flags; |
63d008a4 | 147 | dst->mode = src->mode | IP_TUNNEL_INFO_TX; |
f52f11ec XL |
148 | ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src), |
149 | src->options_len, 0); | |
63d008a4 JB |
150 | |
151 | return res; | |
152 | } | |
153 | EXPORT_SYMBOL_GPL(iptunnel_metadata_reply); | |
154 | ||
aed069df AD |
155 | int iptunnel_handle_offloads(struct sk_buff *skb, |
156 | int gso_type_mask) | |
2d26f0a3 ED |
157 | { |
158 | int err; | |
159 | ||
160 | if (likely(!skb->encapsulation)) { | |
161 | skb_reset_inner_headers(skb); | |
162 | skb->encapsulation = 1; | |
163 | } | |
164 | ||
165 | if (skb_is_gso(skb)) { | |
9580bf2e | 166 | err = skb_header_unclone(skb, GFP_ATOMIC); |
2d26f0a3 | 167 | if (unlikely(err)) |
aed069df | 168 | return err; |
2d26f0a3 | 169 | skb_shinfo(skb)->gso_type |= gso_type_mask; |
aed069df | 170 | return 0; |
2d26f0a3 ED |
171 | } |
172 | ||
6fa79666 | 173 | if (skb->ip_summed != CHECKSUM_PARTIAL) { |
2d26f0a3 | 174 | skb->ip_summed = CHECKSUM_NONE; |
6fa79666 EC |
175 | /* We clear encapsulation here to prevent badly-written |
176 | * drivers potentially deciding to offload an inner checksum | |
177 | * if we set CHECKSUM_PARTIAL on the outer header. | |
178 | * This should go away when the drivers are all fixed. | |
179 | */ | |
179bc67f EC |
180 | skb->encapsulation = 0; |
181 | } | |
2d26f0a3 | 182 | |
aed069df | 183 | return 0; |
2d26f0a3 ED |
184 | } |
185 | EXPORT_SYMBOL_GPL(iptunnel_handle_offloads); | |
ebe44f35 DM |
186 | |
187 | /* Often modified stats are per cpu, other are shared (netdev->stats) */ | |
bc1f4470 | 188 | void ip_tunnel_get_stats64(struct net_device *dev, |
189 | struct rtnl_link_stats64 *tot) | |
ebe44f35 DM |
190 | { |
191 | int i; | |
192 | ||
c24a5964 AD |
193 | netdev_stats_to_stats64(tot, &dev->stats); |
194 | ||
ebe44f35 DM |
195 | for_each_possible_cpu(i) { |
196 | const struct pcpu_sw_netstats *tstats = | |
197 | per_cpu_ptr(dev->tstats, i); | |
198 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; | |
199 | unsigned int start; | |
200 | ||
201 | do { | |
57a7744e | 202 | start = u64_stats_fetch_begin_irq(&tstats->syncp); |
ebe44f35 DM |
203 | rx_packets = tstats->rx_packets; |
204 | tx_packets = tstats->tx_packets; | |
205 | rx_bytes = tstats->rx_bytes; | |
206 | tx_bytes = tstats->tx_bytes; | |
57a7744e | 207 | } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); |
ebe44f35 DM |
208 | |
209 | tot->rx_packets += rx_packets; | |
210 | tot->tx_packets += tx_packets; | |
211 | tot->rx_bytes += rx_bytes; | |
212 | tot->tx_bytes += tx_bytes; | |
213 | } | |
ebe44f35 DM |
214 | } |
215 | EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64); | |
3093fbe7 | 216 | |
a1c234f9 JB |
217 | static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { |
218 | [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, | |
219 | [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, | |
220 | [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, | |
221 | [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, | |
222 | [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, | |
a1c234f9 | 223 | [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, |
4ece4778 | 224 | [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED }, |
3093fbe7 TG |
225 | }; |
226 | ||
4ece4778 XL |
227 | static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = { |
228 | [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED }, | |
edf31cbb | 229 | [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED }, |
b0a21810 | 230 | [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED }, |
4ece4778 XL |
231 | }; |
232 | ||
233 | static const struct nla_policy | |
234 | geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = { | |
235 | [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
236 | [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
237 | [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 }, | |
238 | }; | |
239 | ||
edf31cbb XL |
240 | static const struct nla_policy |
241 | vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = { | |
242 | [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 }, | |
243 | }; | |
244 | ||
b0a21810 XL |
245 | static const struct nla_policy |
246 | erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = { | |
247 | [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 }, | |
248 | [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, | |
249 | [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, | |
250 | [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, | |
251 | }; | |
252 | ||
4ece4778 XL |
253 | static int ip_tun_parse_opts_geneve(struct nlattr *attr, |
254 | struct ip_tunnel_info *info, | |
255 | struct netlink_ext_ack *extack) | |
256 | { | |
257 | struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1]; | |
258 | int data_len, err; | |
259 | ||
260 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, | |
261 | attr, geneve_opt_policy, extack); | |
262 | if (err) | |
263 | return err; | |
264 | ||
265 | if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] || | |
266 | !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] || | |
267 | !tb[LWTUNNEL_IP_OPT_GENEVE_DATA]) | |
268 | return -EINVAL; | |
269 | ||
270 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA]; | |
271 | data_len = nla_len(attr); | |
272 | if (data_len % 4) | |
273 | return -EINVAL; | |
274 | ||
275 | if (info) { | |
276 | struct geneve_opt *opt = ip_tunnel_info_opts(info); | |
277 | ||
278 | memcpy(opt->opt_data, nla_data(attr), data_len); | |
279 | opt->length = data_len / 4; | |
280 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS]; | |
281 | opt->opt_class = nla_get_be16(attr); | |
282 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE]; | |
283 | opt->type = nla_get_u8(attr); | |
284 | info->key.tun_flags |= TUNNEL_GENEVE_OPT; | |
285 | } | |
286 | ||
287 | return sizeof(struct geneve_opt) + data_len; | |
288 | } | |
289 | ||
edf31cbb XL |
290 | static int ip_tun_parse_opts_vxlan(struct nlattr *attr, |
291 | struct ip_tunnel_info *info, | |
292 | struct netlink_ext_ack *extack) | |
293 | { | |
294 | struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1]; | |
295 | int err; | |
296 | ||
297 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, | |
298 | attr, vxlan_opt_policy, extack); | |
299 | if (err) | |
300 | return err; | |
301 | ||
302 | if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP]) | |
303 | return -EINVAL; | |
304 | ||
305 | if (info) { | |
306 | struct vxlan_metadata *md = ip_tunnel_info_opts(info); | |
307 | ||
308 | attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP]; | |
309 | md->gbp = nla_get_u32(attr); | |
310 | info->key.tun_flags |= TUNNEL_VXLAN_OPT; | |
311 | } | |
312 | ||
313 | return sizeof(struct vxlan_metadata); | |
314 | } | |
315 | ||
b0a21810 XL |
316 | static int ip_tun_parse_opts_erspan(struct nlattr *attr, |
317 | struct ip_tunnel_info *info, | |
318 | struct netlink_ext_ack *extack) | |
319 | { | |
320 | struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1]; | |
321 | int err; | |
322 | ||
323 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, | |
324 | attr, erspan_opt_policy, extack); | |
325 | if (err) | |
326 | return err; | |
327 | ||
328 | if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER]) | |
329 | return -EINVAL; | |
330 | ||
331 | if (info) { | |
332 | struct erspan_metadata *md = ip_tunnel_info_opts(info); | |
333 | ||
334 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_VER]; | |
335 | md->version = nla_get_u8(attr); | |
336 | ||
337 | if (md->version == 1 && tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]) { | |
338 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]; | |
339 | md->u.index = nla_get_be32(attr); | |
340 | } else if (md->version == 2 && tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] && | |
341 | tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]) { | |
342 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR]; | |
343 | md->u.md2.dir = nla_get_u8(attr); | |
344 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]; | |
345 | set_hwid(&md->u.md2, nla_get_u8(attr)); | |
346 | } else { | |
347 | return -EINVAL; | |
348 | } | |
349 | ||
350 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; | |
351 | } | |
352 | ||
353 | return sizeof(struct erspan_metadata); | |
354 | } | |
355 | ||
4ece4778 XL |
356 | static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info, |
357 | struct netlink_ext_ack *extack) | |
358 | { | |
359 | struct nlattr *tb[LWTUNNEL_IP_OPTS_MAX + 1]; | |
360 | int err; | |
361 | ||
362 | if (!attr) | |
363 | return 0; | |
364 | ||
365 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_OPTS_MAX, attr, | |
366 | ip_opts_policy, extack); | |
367 | if (err) | |
368 | return err; | |
369 | ||
370 | if (tb[LWTUNNEL_IP_OPTS_GENEVE]) | |
371 | err = ip_tun_parse_opts_geneve(tb[LWTUNNEL_IP_OPTS_GENEVE], | |
372 | info, extack); | |
edf31cbb XL |
373 | else if (tb[LWTUNNEL_IP_OPTS_VXLAN]) |
374 | err = ip_tun_parse_opts_vxlan(tb[LWTUNNEL_IP_OPTS_VXLAN], | |
375 | info, extack); | |
b0a21810 XL |
376 | else if (tb[LWTUNNEL_IP_OPTS_ERSPAN]) |
377 | err = ip_tun_parse_opts_erspan(tb[LWTUNNEL_IP_OPTS_ERSPAN], | |
378 | info, extack); | |
4ece4778 XL |
379 | else |
380 | err = -EINVAL; | |
381 | ||
382 | return err; | |
383 | } | |
384 | ||
385 | static int ip_tun_get_optlen(struct nlattr *attr, | |
386 | struct netlink_ext_ack *extack) | |
387 | { | |
388 | return ip_tun_parse_opts(attr, NULL, extack); | |
389 | } | |
390 | ||
391 | static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info, | |
392 | struct netlink_ext_ack *extack) | |
393 | { | |
394 | return ip_tun_parse_opts(attr, info, extack); | |
395 | } | |
396 | ||
30357d7d | 397 | static int ip_tun_build_state(struct nlattr *attr, |
127eb7cd | 398 | unsigned int family, const void *cfg, |
9ae28727 DA |
399 | struct lwtunnel_state **ts, |
400 | struct netlink_ext_ack *extack) | |
3093fbe7 | 401 | { |
a1c234f9 | 402 | struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; |
4ece4778 XL |
403 | struct lwtunnel_state *new_state; |
404 | struct ip_tunnel_info *tun_info; | |
405 | int err, opt_len; | |
3093fbe7 | 406 | |
8cb08174 JB |
407 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr, |
408 | ip_tun_policy, extack); | |
3093fbe7 TG |
409 | if (err < 0) |
410 | return err; | |
411 | ||
4ece4778 XL |
412 | opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack); |
413 | if (opt_len < 0) | |
414 | return opt_len; | |
415 | ||
416 | new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); | |
3093fbe7 TG |
417 | if (!new_state) |
418 | return -ENOMEM; | |
419 | ||
420 | new_state->type = LWTUNNEL_ENCAP_IP; | |
421 | ||
422 | tun_info = lwt_tun_info(new_state); | |
423 | ||
4ece4778 XL |
424 | err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack); |
425 | if (err < 0) { | |
426 | lwtstate_free(new_state); | |
427 | return err; | |
428 | } | |
429 | ||
3d25eabb | 430 | #ifdef CONFIG_DST_CACHE |
431 | err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL); | |
432 | if (err) { | |
433 | lwtstate_free(new_state); | |
434 | return err; | |
435 | } | |
436 | #endif | |
437 | ||
a1c234f9 | 438 | if (tb[LWTUNNEL_IP_ID]) |
30d3d83a | 439 | tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]); |
3093fbe7 | 440 | |
a1c234f9 | 441 | if (tb[LWTUNNEL_IP_DST]) |
7822ce73 | 442 | tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]); |
3093fbe7 | 443 | |
a1c234f9 | 444 | if (tb[LWTUNNEL_IP_SRC]) |
7822ce73 | 445 | tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]); |
3093fbe7 | 446 | |
a1c234f9 | 447 | if (tb[LWTUNNEL_IP_TTL]) |
7c383fb2 | 448 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); |
3093fbe7 | 449 | |
a1c234f9 | 450 | if (tb[LWTUNNEL_IP_TOS]) |
7c383fb2 | 451 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); |
3093fbe7 | 452 | |
a1c234f9 | 453 | if (tb[LWTUNNEL_IP_FLAGS]) |
4ece4778 | 454 | tun_info->key.tun_flags |= nla_get_be16(tb[LWTUNNEL_IP_FLAGS]); |
3093fbe7 TG |
455 | |
456 | tun_info->mode = IP_TUNNEL_INFO_TX; | |
4ece4778 | 457 | tun_info->options_len = opt_len; |
3093fbe7 TG |
458 | |
459 | *ts = new_state; | |
460 | ||
461 | return 0; | |
462 | } | |
463 | ||
3d25eabb | 464 | static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate) |
465 | { | |
466 | #ifdef CONFIG_DST_CACHE | |
467 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
468 | ||
469 | dst_cache_destroy(&tun_info->dst_cache); | |
470 | #endif | |
471 | } | |
472 | ||
4ece4778 XL |
473 | static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb, |
474 | struct ip_tunnel_info *tun_info) | |
475 | { | |
476 | struct geneve_opt *opt; | |
477 | struct nlattr *nest; | |
478 | ||
479 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE); | |
480 | if (!nest) | |
481 | return -ENOMEM; | |
482 | ||
483 | opt = ip_tunnel_info_opts(tun_info); | |
484 | if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS, opt->opt_class) || | |
485 | nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) || | |
486 | nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4, | |
487 | opt->opt_data)) { | |
488 | nla_nest_cancel(skb, nest); | |
489 | return -ENOMEM; | |
490 | } | |
491 | ||
492 | nla_nest_end(skb, nest); | |
493 | return 0; | |
494 | } | |
495 | ||
edf31cbb XL |
496 | static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb, |
497 | struct ip_tunnel_info *tun_info) | |
498 | { | |
499 | struct vxlan_metadata *md; | |
500 | struct nlattr *nest; | |
501 | ||
502 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN); | |
503 | if (!nest) | |
504 | return -ENOMEM; | |
505 | ||
506 | md = ip_tunnel_info_opts(tun_info); | |
507 | if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) { | |
508 | nla_nest_cancel(skb, nest); | |
509 | return -ENOMEM; | |
510 | } | |
511 | ||
512 | nla_nest_end(skb, nest); | |
513 | return 0; | |
514 | } | |
515 | ||
b0a21810 XL |
516 | static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb, |
517 | struct ip_tunnel_info *tun_info) | |
518 | { | |
519 | struct erspan_metadata *md; | |
520 | struct nlattr *nest; | |
521 | ||
522 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN); | |
523 | if (!nest) | |
524 | return -ENOMEM; | |
525 | ||
526 | md = ip_tunnel_info_opts(tun_info); | |
527 | if (nla_put_u32(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version)) | |
528 | goto err; | |
529 | ||
530 | if (md->version == 1 && | |
531 | nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index)) | |
532 | goto err; | |
533 | ||
534 | if (md->version == 2 && | |
535 | (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) || | |
536 | nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID, | |
537 | get_hwid(&md->u.md2)))) | |
538 | goto err; | |
539 | ||
540 | nla_nest_end(skb, nest); | |
541 | return 0; | |
542 | err: | |
543 | nla_nest_cancel(skb, nest); | |
544 | return -ENOMEM; | |
545 | } | |
546 | ||
4ece4778 XL |
547 | static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type, |
548 | struct ip_tunnel_info *tun_info) | |
549 | { | |
550 | struct nlattr *nest; | |
551 | int err = 0; | |
552 | ||
edf31cbb | 553 | if (!(tun_info->key.tun_flags & |
b0a21810 | 554 | (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT | TUNNEL_ERSPAN_OPT))) |
4ece4778 XL |
555 | return 0; |
556 | ||
557 | nest = nla_nest_start_noflag(skb, type); | |
558 | if (!nest) | |
559 | return -ENOMEM; | |
560 | ||
561 | if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT) | |
562 | err = ip_tun_fill_encap_opts_geneve(skb, tun_info); | |
edf31cbb XL |
563 | else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT) |
564 | err = ip_tun_fill_encap_opts_vxlan(skb, tun_info); | |
b0a21810 XL |
565 | else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT) |
566 | err = ip_tun_fill_encap_opts_erspan(skb, tun_info); | |
4ece4778 XL |
567 | |
568 | if (err) { | |
569 | nla_nest_cancel(skb, nest); | |
570 | return err; | |
571 | } | |
572 | ||
573 | nla_nest_end(skb, nest); | |
574 | return 0; | |
575 | } | |
576 | ||
3093fbe7 TG |
577 | static int ip_tun_fill_encap_info(struct sk_buff *skb, |
578 | struct lwtunnel_state *lwtstate) | |
579 | { | |
580 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
581 | ||
b46f6ded ND |
582 | if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id, |
583 | LWTUNNEL_IP_PAD) || | |
7822ce73 HY |
584 | nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || |
585 | nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || | |
7c383fb2 JB |
586 | nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || |
587 | nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || | |
4ece4778 XL |
588 | nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) || |
589 | ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info)) | |
3093fbe7 TG |
590 | return -ENOMEM; |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
4ece4778 XL |
595 | static int ip_tun_opts_nlsize(struct ip_tunnel_info *info) |
596 | { | |
597 | int opt_len; | |
598 | ||
edf31cbb | 599 | if (!(info->key.tun_flags & |
b0a21810 | 600 | (TUNNEL_GENEVE_OPT | TUNNEL_VXLAN_OPT | TUNNEL_ERSPAN_OPT))) |
4ece4778 XL |
601 | return 0; |
602 | ||
603 | opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */ | |
604 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) { | |
605 | struct geneve_opt *opt = ip_tunnel_info_opts(info); | |
606 | ||
607 | opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_GENEVE */ | |
608 | + nla_total_size(2) /* OPT_GENEVE_CLASS */ | |
609 | + nla_total_size(1) /* OPT_GENEVE_TYPE */ | |
610 | + nla_total_size(opt->length * 4); | |
611 | /* OPT_GENEVE_DATA */ | |
edf31cbb XL |
612 | } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) { |
613 | opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */ | |
614 | + nla_total_size(4); /* OPT_VXLAN_GBP */ | |
b0a21810 XL |
615 | } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) { |
616 | opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */ | |
617 | + nla_total_size(1) /* OPT_ERSPAN_VER */ | |
618 | + nla_total_size(4); /* OPT_ERSPAN_INDEX/DIR/HWID */ | |
4ece4778 XL |
619 | } |
620 | ||
621 | return opt_len; | |
622 | } | |
623 | ||
3093fbe7 TG |
624 | static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) |
625 | { | |
b46f6ded | 626 | return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */ |
a1c234f9 JB |
627 | + nla_total_size(4) /* LWTUNNEL_IP_DST */ |
628 | + nla_total_size(4) /* LWTUNNEL_IP_SRC */ | |
629 | + nla_total_size(1) /* LWTUNNEL_IP_TOS */ | |
630 | + nla_total_size(1) /* LWTUNNEL_IP_TTL */ | |
4ece4778 XL |
631 | + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */ |
632 | + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); | |
633 | /* LWTUNNEL_IP_OPTS */ | |
3093fbe7 TG |
634 | } |
635 | ||
2d798499 JB |
636 | static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) |
637 | { | |
0eb8eb2f XL |
638 | struct ip_tunnel_info *info_a = lwt_tun_info(a); |
639 | struct ip_tunnel_info *info_b = lwt_tun_info(b); | |
640 | ||
641 | return memcmp(info_a, info_b, sizeof(info_a->key)) || | |
642 | info_a->mode != info_b->mode || | |
643 | info_a->options_len != info_b->options_len || | |
644 | memcmp(ip_tunnel_info_opts(info_a), | |
645 | ip_tunnel_info_opts(info_b), info_a->options_len); | |
2d798499 JB |
646 | } |
647 | ||
3093fbe7 TG |
648 | static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { |
649 | .build_state = ip_tun_build_state, | |
3d25eabb | 650 | .destroy_state = ip_tun_destroy_state, |
3093fbe7 TG |
651 | .fill_encap = ip_tun_fill_encap_info, |
652 | .get_encap_size = ip_tun_encap_nlsize, | |
2d798499 | 653 | .cmp_encap = ip_tun_cmp_encap, |
88ff7334 | 654 | .owner = THIS_MODULE, |
3093fbe7 TG |
655 | }; |
656 | ||
32a2b002 JB |
657 | static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { |
658 | [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, | |
659 | [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, | |
660 | [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, | |
661 | [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, | |
662 | [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, | |
32a2b002 JB |
663 | [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, |
664 | }; | |
665 | ||
30357d7d | 666 | static int ip6_tun_build_state(struct nlattr *attr, |
127eb7cd | 667 | unsigned int family, const void *cfg, |
9ae28727 DA |
668 | struct lwtunnel_state **ts, |
669 | struct netlink_ext_ack *extack) | |
32a2b002 | 670 | { |
32a2b002 | 671 | struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; |
4ece4778 XL |
672 | struct lwtunnel_state *new_state; |
673 | struct ip_tunnel_info *tun_info; | |
674 | int err, opt_len; | |
32a2b002 | 675 | |
8cb08174 JB |
676 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr, |
677 | ip6_tun_policy, extack); | |
32a2b002 JB |
678 | if (err < 0) |
679 | return err; | |
680 | ||
4ece4778 XL |
681 | opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack); |
682 | if (opt_len < 0) | |
683 | return opt_len; | |
684 | ||
685 | new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); | |
32a2b002 JB |
686 | if (!new_state) |
687 | return -ENOMEM; | |
688 | ||
689 | new_state->type = LWTUNNEL_ENCAP_IP6; | |
690 | ||
691 | tun_info = lwt_tun_info(new_state); | |
692 | ||
4ece4778 XL |
693 | err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack); |
694 | if (err < 0) { | |
695 | lwtstate_free(new_state); | |
696 | return err; | |
697 | } | |
698 | ||
32a2b002 | 699 | if (tb[LWTUNNEL_IP6_ID]) |
30d3d83a | 700 | tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]); |
32a2b002 JB |
701 | |
702 | if (tb[LWTUNNEL_IP6_DST]) | |
703 | tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); | |
704 | ||
705 | if (tb[LWTUNNEL_IP6_SRC]) | |
706 | tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); | |
707 | ||
708 | if (tb[LWTUNNEL_IP6_HOPLIMIT]) | |
709 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); | |
710 | ||
711 | if (tb[LWTUNNEL_IP6_TC]) | |
712 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); | |
713 | ||
32a2b002 | 714 | if (tb[LWTUNNEL_IP6_FLAGS]) |
4ece4778 | 715 | tun_info->key.tun_flags |= nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]); |
32a2b002 | 716 | |
7f9562a1 | 717 | tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6; |
4ece4778 | 718 | tun_info->options_len = opt_len; |
32a2b002 JB |
719 | |
720 | *ts = new_state; | |
721 | ||
722 | return 0; | |
723 | } | |
724 | ||
725 | static int ip6_tun_fill_encap_info(struct sk_buff *skb, | |
726 | struct lwtunnel_state *lwtstate) | |
727 | { | |
728 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
729 | ||
b46f6ded ND |
730 | if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id, |
731 | LWTUNNEL_IP6_PAD) || | |
32a2b002 JB |
732 | nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || |
733 | nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || | |
995096a0 QA |
734 | nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) || |
735 | nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) || | |
4ece4778 XL |
736 | nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) || |
737 | ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info)) | |
32a2b002 JB |
738 | return -ENOMEM; |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
743 | static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) | |
744 | { | |
b46f6ded | 745 | return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */ |
32a2b002 JB |
746 | + nla_total_size(16) /* LWTUNNEL_IP6_DST */ |
747 | + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ | |
748 | + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ | |
749 | + nla_total_size(1) /* LWTUNNEL_IP6_TC */ | |
4ece4778 XL |
750 | + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */ |
751 | + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); | |
752 | /* LWTUNNEL_IP6_OPTS */ | |
32a2b002 JB |
753 | } |
754 | ||
755 | static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { | |
756 | .build_state = ip6_tun_build_state, | |
757 | .fill_encap = ip6_tun_fill_encap_info, | |
758 | .get_encap_size = ip6_tun_encap_nlsize, | |
759 | .cmp_encap = ip_tun_cmp_encap, | |
88ff7334 | 760 | .owner = THIS_MODULE, |
32a2b002 JB |
761 | }; |
762 | ||
045a0fa0 | 763 | void __init ip_tunnel_core_init(void) |
3093fbe7 | 764 | { |
fca5fdf6 DB |
765 | /* If you land here, make sure whether increasing ip_tunnel_info's |
766 | * options_len is a reasonable choice with its usage in front ends | |
767 | * (f.e., it's part of flow keys, etc). | |
768 | */ | |
769 | BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255); | |
770 | ||
3093fbe7 | 771 | lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); |
32a2b002 | 772 | lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); |
3093fbe7 | 773 | } |
e7030878 | 774 | |
5263a98f | 775 | DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt); |
e7030878 TG |
776 | EXPORT_SYMBOL(ip_tunnel_metadata_cnt); |
777 | ||
778 | void ip_tunnel_need_metadata(void) | |
779 | { | |
5263a98f | 780 | static_branch_inc(&ip_tunnel_metadata_cnt); |
e7030878 TG |
781 | } |
782 | EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); | |
783 | ||
784 | void ip_tunnel_unneed_metadata(void) | |
785 | { | |
5263a98f | 786 | static_branch_dec(&ip_tunnel_metadata_cnt); |
e7030878 TG |
787 | } |
788 | EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); |