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 | 217 | static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { |
7b6a70f7 | 218 | [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS }, |
a1c234f9 JB |
219 | [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, |
220 | [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, | |
221 | [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, | |
222 | [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, | |
223 | [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, | |
a1c234f9 | 224 | [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, |
4ece4778 | 225 | [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED }, |
3093fbe7 TG |
226 | }; |
227 | ||
4ece4778 XL |
228 | static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = { |
229 | [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED }, | |
edf31cbb | 230 | [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED }, |
b0a21810 | 231 | [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED }, |
4ece4778 XL |
232 | }; |
233 | ||
234 | static const struct nla_policy | |
235 | geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = { | |
236 | [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
237 | [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
238 | [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 }, | |
239 | }; | |
240 | ||
edf31cbb XL |
241 | static const struct nla_policy |
242 | vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = { | |
243 | [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 }, | |
244 | }; | |
245 | ||
b0a21810 XL |
246 | static const struct nla_policy |
247 | erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = { | |
248 | [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 }, | |
249 | [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, | |
250 | [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, | |
251 | [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, | |
252 | }; | |
253 | ||
4ece4778 | 254 | static int ip_tun_parse_opts_geneve(struct nlattr *attr, |
2f1d370b | 255 | struct ip_tunnel_info *info, int opts_len, |
4ece4778 XL |
256 | struct netlink_ext_ack *extack) |
257 | { | |
258 | struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1]; | |
259 | int data_len, err; | |
260 | ||
ed02551f XL |
261 | err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr, |
262 | geneve_opt_policy, extack); | |
4ece4778 XL |
263 | if (err) |
264 | return err; | |
265 | ||
266 | if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] || | |
267 | !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] || | |
268 | !tb[LWTUNNEL_IP_OPT_GENEVE_DATA]) | |
269 | return -EINVAL; | |
270 | ||
271 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA]; | |
272 | data_len = nla_len(attr); | |
273 | if (data_len % 4) | |
274 | return -EINVAL; | |
275 | ||
276 | if (info) { | |
2f1d370b | 277 | struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len; |
4ece4778 XL |
278 | |
279 | memcpy(opt->opt_data, nla_data(attr), data_len); | |
280 | opt->length = data_len / 4; | |
281 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS]; | |
282 | opt->opt_class = nla_get_be16(attr); | |
283 | attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE]; | |
284 | opt->type = nla_get_u8(attr); | |
285 | info->key.tun_flags |= TUNNEL_GENEVE_OPT; | |
286 | } | |
287 | ||
288 | return sizeof(struct geneve_opt) + data_len; | |
289 | } | |
290 | ||
edf31cbb | 291 | static int ip_tun_parse_opts_vxlan(struct nlattr *attr, |
2f1d370b | 292 | struct ip_tunnel_info *info, int opts_len, |
edf31cbb XL |
293 | struct netlink_ext_ack *extack) |
294 | { | |
295 | struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1]; | |
296 | int err; | |
297 | ||
ed02551f XL |
298 | err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr, |
299 | vxlan_opt_policy, extack); | |
edf31cbb XL |
300 | if (err) |
301 | return err; | |
302 | ||
303 | if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP]) | |
304 | return -EINVAL; | |
305 | ||
306 | if (info) { | |
2f1d370b XL |
307 | struct vxlan_metadata *md = |
308 | ip_tunnel_info_opts(info) + opts_len; | |
edf31cbb XL |
309 | |
310 | attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP]; | |
311 | md->gbp = nla_get_u32(attr); | |
312 | info->key.tun_flags |= TUNNEL_VXLAN_OPT; | |
313 | } | |
314 | ||
315 | return sizeof(struct vxlan_metadata); | |
316 | } | |
317 | ||
b0a21810 | 318 | static int ip_tun_parse_opts_erspan(struct nlattr *attr, |
2f1d370b | 319 | struct ip_tunnel_info *info, int opts_len, |
b0a21810 XL |
320 | struct netlink_ext_ack *extack) |
321 | { | |
322 | struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1]; | |
323 | int err; | |
324 | ||
ed02551f XL |
325 | err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr, |
326 | erspan_opt_policy, extack); | |
b0a21810 XL |
327 | if (err) |
328 | return err; | |
329 | ||
330 | if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER]) | |
331 | return -EINVAL; | |
332 | ||
333 | if (info) { | |
2f1d370b XL |
334 | struct erspan_metadata *md = |
335 | ip_tunnel_info_opts(info) + opts_len; | |
b0a21810 XL |
336 | |
337 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_VER]; | |
338 | md->version = nla_get_u8(attr); | |
339 | ||
340 | if (md->version == 1 && tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]) { | |
341 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX]; | |
342 | md->u.index = nla_get_be32(attr); | |
343 | } else if (md->version == 2 && tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] && | |
344 | tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]) { | |
345 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR]; | |
346 | md->u.md2.dir = nla_get_u8(attr); | |
347 | attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID]; | |
348 | set_hwid(&md->u.md2, nla_get_u8(attr)); | |
349 | } else { | |
350 | return -EINVAL; | |
351 | } | |
352 | ||
353 | info->key.tun_flags |= TUNNEL_ERSPAN_OPT; | |
354 | } | |
355 | ||
356 | return sizeof(struct erspan_metadata); | |
357 | } | |
358 | ||
4ece4778 XL |
359 | static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info, |
360 | struct netlink_ext_ack *extack) | |
361 | { | |
2f1d370b XL |
362 | int err, rem, opt_len, opts_len = 0, type = 0; |
363 | struct nlattr *nla; | |
4ece4778 XL |
364 | |
365 | if (!attr) | |
366 | return 0; | |
367 | ||
2f1d370b XL |
368 | err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX, |
369 | ip_opts_policy, extack); | |
4ece4778 XL |
370 | if (err) |
371 | return err; | |
372 | ||
2f1d370b XL |
373 | nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) { |
374 | switch (nla_type(nla)) { | |
375 | case LWTUNNEL_IP_OPTS_GENEVE: | |
376 | if (type && type != TUNNEL_GENEVE_OPT) | |
377 | return -EINVAL; | |
378 | opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len, | |
379 | extack); | |
380 | if (opt_len < 0) | |
381 | return opt_len; | |
382 | opts_len += opt_len; | |
383 | if (opts_len > IP_TUNNEL_OPTS_MAX) | |
384 | return -EINVAL; | |
385 | type = TUNNEL_GENEVE_OPT; | |
386 | break; | |
387 | case LWTUNNEL_IP_OPTS_VXLAN: | |
388 | if (type) | |
389 | return -EINVAL; | |
390 | opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len, | |
391 | extack); | |
392 | if (opt_len < 0) | |
393 | return opt_len; | |
394 | opts_len += opt_len; | |
395 | type = TUNNEL_VXLAN_OPT; | |
396 | break; | |
397 | case LWTUNNEL_IP_OPTS_ERSPAN: | |
398 | if (type) | |
399 | return -EINVAL; | |
400 | opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len, | |
401 | extack); | |
402 | if (opt_len < 0) | |
403 | return opt_len; | |
404 | opts_len += opt_len; | |
405 | type = TUNNEL_ERSPAN_OPT; | |
406 | break; | |
407 | default: | |
408 | return -EINVAL; | |
409 | } | |
410 | } | |
4ece4778 | 411 | |
2f1d370b | 412 | return opts_len; |
4ece4778 XL |
413 | } |
414 | ||
415 | static int ip_tun_get_optlen(struct nlattr *attr, | |
416 | struct netlink_ext_ack *extack) | |
417 | { | |
418 | return ip_tun_parse_opts(attr, NULL, extack); | |
419 | } | |
420 | ||
421 | static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info, | |
422 | struct netlink_ext_ack *extack) | |
423 | { | |
424 | return ip_tun_parse_opts(attr, info, extack); | |
425 | } | |
426 | ||
30357d7d | 427 | static int ip_tun_build_state(struct nlattr *attr, |
127eb7cd | 428 | unsigned int family, const void *cfg, |
9ae28727 DA |
429 | struct lwtunnel_state **ts, |
430 | struct netlink_ext_ack *extack) | |
3093fbe7 | 431 | { |
a1c234f9 | 432 | struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; |
4ece4778 XL |
433 | struct lwtunnel_state *new_state; |
434 | struct ip_tunnel_info *tun_info; | |
435 | int err, opt_len; | |
3093fbe7 | 436 | |
8cb08174 JB |
437 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr, |
438 | ip_tun_policy, extack); | |
3093fbe7 TG |
439 | if (err < 0) |
440 | return err; | |
441 | ||
4ece4778 XL |
442 | opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack); |
443 | if (opt_len < 0) | |
444 | return opt_len; | |
445 | ||
446 | new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); | |
3093fbe7 TG |
447 | if (!new_state) |
448 | return -ENOMEM; | |
449 | ||
450 | new_state->type = LWTUNNEL_ENCAP_IP; | |
451 | ||
452 | tun_info = lwt_tun_info(new_state); | |
453 | ||
4ece4778 XL |
454 | err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack); |
455 | if (err < 0) { | |
456 | lwtstate_free(new_state); | |
457 | return err; | |
458 | } | |
459 | ||
3d25eabb | 460 | #ifdef CONFIG_DST_CACHE |
461 | err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL); | |
462 | if (err) { | |
463 | lwtstate_free(new_state); | |
464 | return err; | |
465 | } | |
466 | #endif | |
467 | ||
a1c234f9 | 468 | if (tb[LWTUNNEL_IP_ID]) |
30d3d83a | 469 | tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]); |
3093fbe7 | 470 | |
a1c234f9 | 471 | if (tb[LWTUNNEL_IP_DST]) |
7822ce73 | 472 | tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]); |
3093fbe7 | 473 | |
a1c234f9 | 474 | if (tb[LWTUNNEL_IP_SRC]) |
7822ce73 | 475 | tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]); |
3093fbe7 | 476 | |
a1c234f9 | 477 | if (tb[LWTUNNEL_IP_TTL]) |
7c383fb2 | 478 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); |
3093fbe7 | 479 | |
a1c234f9 | 480 | if (tb[LWTUNNEL_IP_TOS]) |
7c383fb2 | 481 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); |
3093fbe7 | 482 | |
a1c234f9 | 483 | if (tb[LWTUNNEL_IP_FLAGS]) |
0c06d166 XL |
484 | tun_info->key.tun_flags |= |
485 | (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) & | |
486 | ~TUNNEL_OPTIONS_PRESENT); | |
3093fbe7 TG |
487 | |
488 | tun_info->mode = IP_TUNNEL_INFO_TX; | |
4ece4778 | 489 | tun_info->options_len = opt_len; |
3093fbe7 TG |
490 | |
491 | *ts = new_state; | |
492 | ||
493 | return 0; | |
494 | } | |
495 | ||
3d25eabb | 496 | static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate) |
497 | { | |
498 | #ifdef CONFIG_DST_CACHE | |
499 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
500 | ||
501 | dst_cache_destroy(&tun_info->dst_cache); | |
502 | #endif | |
503 | } | |
504 | ||
4ece4778 XL |
505 | static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb, |
506 | struct ip_tunnel_info *tun_info) | |
507 | { | |
508 | struct geneve_opt *opt; | |
509 | struct nlattr *nest; | |
2f1d370b | 510 | int offset = 0; |
4ece4778 XL |
511 | |
512 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE); | |
513 | if (!nest) | |
514 | return -ENOMEM; | |
515 | ||
2f1d370b XL |
516 | while (tun_info->options_len > offset) { |
517 | opt = ip_tunnel_info_opts(tun_info) + offset; | |
518 | if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS, | |
519 | opt->opt_class) || | |
520 | nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) || | |
521 | nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4, | |
522 | opt->opt_data)) { | |
523 | nla_nest_cancel(skb, nest); | |
524 | return -ENOMEM; | |
525 | } | |
526 | offset += sizeof(*opt) + opt->length * 4; | |
4ece4778 XL |
527 | } |
528 | ||
529 | nla_nest_end(skb, nest); | |
530 | return 0; | |
531 | } | |
532 | ||
edf31cbb XL |
533 | static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb, |
534 | struct ip_tunnel_info *tun_info) | |
535 | { | |
536 | struct vxlan_metadata *md; | |
537 | struct nlattr *nest; | |
538 | ||
539 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN); | |
540 | if (!nest) | |
541 | return -ENOMEM; | |
542 | ||
543 | md = ip_tunnel_info_opts(tun_info); | |
544 | if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) { | |
545 | nla_nest_cancel(skb, nest); | |
546 | return -ENOMEM; | |
547 | } | |
548 | ||
549 | nla_nest_end(skb, nest); | |
550 | return 0; | |
551 | } | |
552 | ||
b0a21810 XL |
553 | static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb, |
554 | struct ip_tunnel_info *tun_info) | |
555 | { | |
556 | struct erspan_metadata *md; | |
557 | struct nlattr *nest; | |
558 | ||
559 | nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN); | |
560 | if (!nest) | |
561 | return -ENOMEM; | |
562 | ||
563 | md = ip_tunnel_info_opts(tun_info); | |
3132174b | 564 | if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version)) |
b0a21810 XL |
565 | goto err; |
566 | ||
567 | if (md->version == 1 && | |
568 | nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index)) | |
569 | goto err; | |
570 | ||
571 | if (md->version == 2 && | |
572 | (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) || | |
573 | nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID, | |
574 | get_hwid(&md->u.md2)))) | |
575 | goto err; | |
576 | ||
577 | nla_nest_end(skb, nest); | |
578 | return 0; | |
579 | err: | |
580 | nla_nest_cancel(skb, nest); | |
581 | return -ENOMEM; | |
582 | } | |
583 | ||
4ece4778 XL |
584 | static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type, |
585 | struct ip_tunnel_info *tun_info) | |
586 | { | |
587 | struct nlattr *nest; | |
588 | int err = 0; | |
589 | ||
0c06d166 | 590 | if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT)) |
4ece4778 XL |
591 | return 0; |
592 | ||
593 | nest = nla_nest_start_noflag(skb, type); | |
594 | if (!nest) | |
595 | return -ENOMEM; | |
596 | ||
597 | if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT) | |
598 | err = ip_tun_fill_encap_opts_geneve(skb, tun_info); | |
edf31cbb XL |
599 | else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT) |
600 | err = ip_tun_fill_encap_opts_vxlan(skb, tun_info); | |
b0a21810 XL |
601 | else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT) |
602 | err = ip_tun_fill_encap_opts_erspan(skb, tun_info); | |
4ece4778 XL |
603 | |
604 | if (err) { | |
605 | nla_nest_cancel(skb, nest); | |
606 | return err; | |
607 | } | |
608 | ||
609 | nla_nest_end(skb, nest); | |
610 | return 0; | |
611 | } | |
612 | ||
3093fbe7 TG |
613 | static int ip_tun_fill_encap_info(struct sk_buff *skb, |
614 | struct lwtunnel_state *lwtstate) | |
615 | { | |
616 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
617 | ||
b46f6ded ND |
618 | if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id, |
619 | LWTUNNEL_IP_PAD) || | |
7822ce73 HY |
620 | nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || |
621 | nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || | |
7c383fb2 JB |
622 | nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || |
623 | nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || | |
4ece4778 XL |
624 | nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) || |
625 | ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info)) | |
3093fbe7 TG |
626 | return -ENOMEM; |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
4ece4778 XL |
631 | static int ip_tun_opts_nlsize(struct ip_tunnel_info *info) |
632 | { | |
633 | int opt_len; | |
634 | ||
0c06d166 | 635 | if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT)) |
4ece4778 XL |
636 | return 0; |
637 | ||
638 | opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */ | |
639 | if (info->key.tun_flags & TUNNEL_GENEVE_OPT) { | |
2f1d370b XL |
640 | struct geneve_opt *opt; |
641 | int offset = 0; | |
642 | ||
643 | opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */ | |
644 | while (info->options_len > offset) { | |
645 | opt = ip_tunnel_info_opts(info) + offset; | |
646 | opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */ | |
647 | + nla_total_size(1) /* OPT_GENEVE_TYPE */ | |
648 | + nla_total_size(opt->length * 4); | |
649 | /* OPT_GENEVE_DATA */ | |
650 | offset += sizeof(*opt) + opt->length * 4; | |
651 | } | |
edf31cbb XL |
652 | } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) { |
653 | opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */ | |
654 | + nla_total_size(4); /* OPT_VXLAN_GBP */ | |
b0a21810 | 655 | } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) { |
58e8494e XL |
656 | struct erspan_metadata *md = ip_tunnel_info_opts(info); |
657 | ||
b0a21810 XL |
658 | opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */ |
659 | + nla_total_size(1) /* OPT_ERSPAN_VER */ | |
58e8494e XL |
660 | + (md->version == 1 ? nla_total_size(4) |
661 | /* OPT_ERSPAN_INDEX (v1) */ | |
662 | : nla_total_size(1) + | |
663 | nla_total_size(1)); | |
664 | /* OPT_ERSPAN_DIR + HWID (v2) */ | |
4ece4778 XL |
665 | } |
666 | ||
667 | return opt_len; | |
668 | } | |
669 | ||
3093fbe7 TG |
670 | static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) |
671 | { | |
b46f6ded | 672 | return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */ |
a1c234f9 JB |
673 | + nla_total_size(4) /* LWTUNNEL_IP_DST */ |
674 | + nla_total_size(4) /* LWTUNNEL_IP_SRC */ | |
675 | + nla_total_size(1) /* LWTUNNEL_IP_TOS */ | |
676 | + nla_total_size(1) /* LWTUNNEL_IP_TTL */ | |
4ece4778 XL |
677 | + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */ |
678 | + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); | |
679 | /* LWTUNNEL_IP_OPTS */ | |
3093fbe7 TG |
680 | } |
681 | ||
2d798499 JB |
682 | static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) |
683 | { | |
0eb8eb2f XL |
684 | struct ip_tunnel_info *info_a = lwt_tun_info(a); |
685 | struct ip_tunnel_info *info_b = lwt_tun_info(b); | |
686 | ||
687 | return memcmp(info_a, info_b, sizeof(info_a->key)) || | |
688 | info_a->mode != info_b->mode || | |
689 | info_a->options_len != info_b->options_len || | |
690 | memcmp(ip_tunnel_info_opts(info_a), | |
691 | ip_tunnel_info_opts(info_b), info_a->options_len); | |
2d798499 JB |
692 | } |
693 | ||
3093fbe7 TG |
694 | static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { |
695 | .build_state = ip_tun_build_state, | |
3d25eabb | 696 | .destroy_state = ip_tun_destroy_state, |
3093fbe7 TG |
697 | .fill_encap = ip_tun_fill_encap_info, |
698 | .get_encap_size = ip_tun_encap_nlsize, | |
2d798499 | 699 | .cmp_encap = ip_tun_cmp_encap, |
88ff7334 | 700 | .owner = THIS_MODULE, |
3093fbe7 TG |
701 | }; |
702 | ||
32a2b002 | 703 | static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { |
7b6a70f7 | 704 | [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS }, |
32a2b002 JB |
705 | [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, |
706 | [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, | |
707 | [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, | |
708 | [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, | |
709 | [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, | |
32a2b002 | 710 | [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, |
7b6a70f7 | 711 | [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED }, |
32a2b002 JB |
712 | }; |
713 | ||
30357d7d | 714 | static int ip6_tun_build_state(struct nlattr *attr, |
127eb7cd | 715 | unsigned int family, const void *cfg, |
9ae28727 DA |
716 | struct lwtunnel_state **ts, |
717 | struct netlink_ext_ack *extack) | |
32a2b002 | 718 | { |
32a2b002 | 719 | struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; |
4ece4778 XL |
720 | struct lwtunnel_state *new_state; |
721 | struct ip_tunnel_info *tun_info; | |
722 | int err, opt_len; | |
32a2b002 | 723 | |
8cb08174 JB |
724 | err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr, |
725 | ip6_tun_policy, extack); | |
32a2b002 JB |
726 | if (err < 0) |
727 | return err; | |
728 | ||
4ece4778 XL |
729 | opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack); |
730 | if (opt_len < 0) | |
731 | return opt_len; | |
732 | ||
733 | new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len); | |
32a2b002 JB |
734 | if (!new_state) |
735 | return -ENOMEM; | |
736 | ||
737 | new_state->type = LWTUNNEL_ENCAP_IP6; | |
738 | ||
739 | tun_info = lwt_tun_info(new_state); | |
740 | ||
4ece4778 XL |
741 | err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack); |
742 | if (err < 0) { | |
743 | lwtstate_free(new_state); | |
744 | return err; | |
745 | } | |
746 | ||
32a2b002 | 747 | if (tb[LWTUNNEL_IP6_ID]) |
30d3d83a | 748 | tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]); |
32a2b002 JB |
749 | |
750 | if (tb[LWTUNNEL_IP6_DST]) | |
751 | tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); | |
752 | ||
753 | if (tb[LWTUNNEL_IP6_SRC]) | |
754 | tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); | |
755 | ||
756 | if (tb[LWTUNNEL_IP6_HOPLIMIT]) | |
757 | tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); | |
758 | ||
759 | if (tb[LWTUNNEL_IP6_TC]) | |
760 | tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); | |
761 | ||
32a2b002 | 762 | if (tb[LWTUNNEL_IP6_FLAGS]) |
0c06d166 XL |
763 | tun_info->key.tun_flags |= |
764 | (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) & | |
765 | ~TUNNEL_OPTIONS_PRESENT); | |
32a2b002 | 766 | |
7f9562a1 | 767 | tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6; |
4ece4778 | 768 | tun_info->options_len = opt_len; |
32a2b002 JB |
769 | |
770 | *ts = new_state; | |
771 | ||
772 | return 0; | |
773 | } | |
774 | ||
775 | static int ip6_tun_fill_encap_info(struct sk_buff *skb, | |
776 | struct lwtunnel_state *lwtstate) | |
777 | { | |
778 | struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); | |
779 | ||
b46f6ded ND |
780 | if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id, |
781 | LWTUNNEL_IP6_PAD) || | |
32a2b002 JB |
782 | nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || |
783 | nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || | |
995096a0 QA |
784 | nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) || |
785 | nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) || | |
4ece4778 XL |
786 | nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) || |
787 | ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info)) | |
32a2b002 JB |
788 | return -ENOMEM; |
789 | ||
790 | return 0; | |
791 | } | |
792 | ||
793 | static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) | |
794 | { | |
b46f6ded | 795 | return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */ |
32a2b002 JB |
796 | + nla_total_size(16) /* LWTUNNEL_IP6_DST */ |
797 | + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ | |
798 | + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ | |
799 | + nla_total_size(1) /* LWTUNNEL_IP6_TC */ | |
4ece4778 XL |
800 | + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */ |
801 | + ip_tun_opts_nlsize(lwt_tun_info(lwtstate)); | |
802 | /* LWTUNNEL_IP6_OPTS */ | |
32a2b002 JB |
803 | } |
804 | ||
805 | static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { | |
806 | .build_state = ip6_tun_build_state, | |
807 | .fill_encap = ip6_tun_fill_encap_info, | |
808 | .get_encap_size = ip6_tun_encap_nlsize, | |
809 | .cmp_encap = ip_tun_cmp_encap, | |
88ff7334 | 810 | .owner = THIS_MODULE, |
32a2b002 JB |
811 | }; |
812 | ||
045a0fa0 | 813 | void __init ip_tunnel_core_init(void) |
3093fbe7 | 814 | { |
fca5fdf6 DB |
815 | /* If you land here, make sure whether increasing ip_tunnel_info's |
816 | * options_len is a reasonable choice with its usage in front ends | |
817 | * (f.e., it's part of flow keys, etc). | |
818 | */ | |
819 | BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255); | |
820 | ||
3093fbe7 | 821 | lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); |
32a2b002 | 822 | lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); |
3093fbe7 | 823 | } |
e7030878 | 824 | |
5263a98f | 825 | DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt); |
e7030878 TG |
826 | EXPORT_SYMBOL(ip_tunnel_metadata_cnt); |
827 | ||
828 | void ip_tunnel_need_metadata(void) | |
829 | { | |
5263a98f | 830 | static_branch_inc(&ip_tunnel_metadata_cnt); |
e7030878 TG |
831 | } |
832 | EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); | |
833 | ||
834 | void ip_tunnel_unneed_metadata(void) | |
835 | { | |
5263a98f | 836 | static_branch_dec(&ip_tunnel_metadata_cnt); |
e7030878 TG |
837 | } |
838 | EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); |