1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 Nicira, Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.h>
13 #include <linux/if_arp.h>
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>
21 #include <linux/static_key.h>
25 #include <net/protocol.h>
26 #include <net/ip_tunnels.h>
27 #include <net/ip6_tunnel.h>
28 #include <net/ip6_checksum.h>
30 #include <net/checksum.h>
31 #include <net/dsfield.h>
32 #include <net/inet_ecn.h>
34 #include <net/net_namespace.h>
35 #include <net/netns/generic.h>
36 #include <net/rtnetlink.h>
37 #include <net/dst_metadata.h>
38 #include <net/geneve.h>
39 #include <net/vxlan.h>
40 #include <net/erspan.h>
42 const struct ip_tunnel_encap_ops __rcu *
43 iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
44 EXPORT_SYMBOL(iptun_encaps);
46 const struct ip6_tnl_encap_ops __rcu *
47 ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
48 EXPORT_SYMBOL(ip6tun_encaps);
50 void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
51 __be32 src, __be32 dst, __u8 proto,
52 __u8 tos, __u8 ttl, __be16 df, bool xnet)
54 int pkt_len = skb->len - skb_inner_network_offset(skb);
55 struct net *net = dev_net(rt->dst.dev);
56 struct net_device *dev = skb->dev;
60 skb_scrub_packet(skb, xnet);
62 skb_clear_hash_if_not_l4(skb);
63 skb_dst_set(skb, &rt->dst);
64 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
66 /* Push down and install the IP header. */
67 skb_push(skb, sizeof(struct iphdr));
68 skb_reset_network_header(skb);
73 iph->ihl = sizeof(struct iphdr) >> 2;
74 iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
75 iph->protocol = proto;
80 __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
82 err = ip_local_out(net, sk, skb);
85 if (unlikely(net_xmit_eval(err)))
87 iptunnel_xmit_stats(dev, pkt_len);
90 EXPORT_SYMBOL_GPL(iptunnel_xmit);
92 int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
93 __be16 inner_proto, bool raw_proto, bool xnet)
95 if (unlikely(!pskb_may_pull(skb, hdr_len)))
98 skb_pull_rcsum(skb, hdr_len);
100 if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
103 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
106 eh = (struct ethhdr *)skb->data;
107 if (likely(eth_proto_is_802_3(eh->h_proto)))
108 skb->protocol = eh->h_proto;
110 skb->protocol = htons(ETH_P_802_2);
113 skb->protocol = inner_proto;
116 skb_clear_hash_if_not_l4(skb);
117 __vlan_hwaccel_clear_tag(skb);
118 skb_set_queue_mapping(skb, 0);
119 skb_scrub_packet(skb, xnet);
121 return iptunnel_pull_offloads(skb);
123 EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
125 struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
128 struct metadata_dst *res;
129 struct ip_tunnel_info *dst, *src;
131 if (!md || md->type != METADATA_IP_TUNNEL ||
132 md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
135 src = &md->u.tun_info;
136 res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
140 dst = &res->u.tun_info;
141 dst->key.tun_id = src->key.tun_id;
142 if (src->mode & IP_TUNNEL_INFO_IPV6)
143 memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
144 sizeof(struct in6_addr));
146 dst->key.u.ipv4.dst = src->key.u.ipv4.src;
147 dst->key.tun_flags = src->key.tun_flags;
148 dst->mode = src->mode | IP_TUNNEL_INFO_TX;
149 ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
150 src->options_len, 0);
154 EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
156 int iptunnel_handle_offloads(struct sk_buff *skb,
161 if (likely(!skb->encapsulation)) {
162 skb_reset_inner_headers(skb);
163 skb->encapsulation = 1;
166 if (skb_is_gso(skb)) {
167 err = skb_header_unclone(skb, GFP_ATOMIC);
170 skb_shinfo(skb)->gso_type |= gso_type_mask;
174 if (skb->ip_summed != CHECKSUM_PARTIAL) {
175 skb->ip_summed = CHECKSUM_NONE;
176 /* We clear encapsulation here to prevent badly-written
177 * drivers potentially deciding to offload an inner checksum
178 * if we set CHECKSUM_PARTIAL on the outer header.
179 * This should go away when the drivers are all fixed.
181 skb->encapsulation = 0;
186 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
189 * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD
190 * @skb: Original packet with L2 header
191 * @mtu: MTU value for ICMP error
193 * Return: length on success, negative error code if message couldn't be built.
195 static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
197 const struct iphdr *iph = ip_hdr(skb);
198 struct icmphdr *icmph;
203 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
206 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
207 pskb_pull(skb, ETH_HLEN);
208 skb_reset_network_header(skb);
210 err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
214 len = skb->len + sizeof(*icmph);
215 err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
219 icmph = skb_push(skb, sizeof(*icmph));
220 *icmph = (struct icmphdr) {
221 .type = ICMP_DEST_UNREACH,
222 .code = ICMP_FRAG_NEEDED,
224 .un.frag.__unused = 0,
225 .un.frag.mtu = ntohs(mtu),
227 icmph->checksum = csum_fold(skb_checksum(skb, 0, len, 0));
228 skb_reset_transport_header(skb);
230 niph = skb_push(skb, sizeof(*niph));
231 *niph = (struct iphdr) {
232 .ihl = sizeof(*niph) / 4u,
235 .tot_len = htons(len + sizeof(*niph)),
237 .frag_off = htons(IP_DF),
239 .protocol = IPPROTO_ICMP,
244 skb_reset_network_header(skb);
246 skb->ip_summed = CHECKSUM_NONE;
248 eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
249 skb_reset_mac_header(skb);
255 * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed
256 * @skb: Buffer being sent by encapsulation, L2 headers expected
257 * @mtu: Network MTU for path
259 * Return: 0 for no ICMP reply, length if built, negative value on error.
261 static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
263 const struct icmphdr *icmph = icmp_hdr(skb);
264 const struct iphdr *iph = ip_hdr(skb);
266 if (mtu < 576 || iph->frag_off != htons(IP_DF))
269 if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) ||
270 ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) ||
271 ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr))
274 if (iph->protocol == IPPROTO_ICMP && icmp_is_err(icmph->type))
277 return iptunnel_pmtud_build_icmp(skb, mtu);
280 #if IS_ENABLED(CONFIG_IPV6)
282 * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD
283 * @skb: Original packet with L2 header
284 * @mtu: MTU value for ICMPv6 error
286 * Return: length on success, negative error code if message couldn't be built.
288 static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
290 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
291 struct icmp6hdr *icmp6h;
292 struct ipv6hdr *nip6h;
297 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
300 skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
301 pskb_pull(skb, ETH_HLEN);
302 skb_reset_network_header(skb);
304 err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
308 len = skb->len + sizeof(*icmp6h);
309 err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
313 icmp6h = skb_push(skb, sizeof(*icmp6h));
314 *icmp6h = (struct icmp6hdr) {
315 .icmp6_type = ICMPV6_PKT_TOOBIG,
318 .icmp6_mtu = htonl(mtu),
320 skb_reset_transport_header(skb);
322 nip6h = skb_push(skb, sizeof(*nip6h));
323 *nip6h = (struct ipv6hdr) {
327 .payload_len = htons(len),
328 .nexthdr = IPPROTO_ICMPV6,
329 .hop_limit = ip6h->hop_limit,
330 .saddr = ip6h->daddr,
331 .daddr = ip6h->saddr,
333 skb_reset_network_header(skb);
335 csum = skb_checksum(skb, skb_transport_offset(skb), len, 0);
336 icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
337 IPPROTO_ICMPV6, csum);
339 skb->ip_summed = CHECKSUM_NONE;
341 eth_header(skb, skb->dev, htons(eh.h_proto), eh.h_source, eh.h_dest, 0);
342 skb_reset_mac_header(skb);
348 * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed
349 * @skb: Buffer being sent by encapsulation, L2 headers expected
350 * @mtu: Network MTU for path
352 * Return: 0 for no ICMPv6 reply, length if built, negative value on error.
354 static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
356 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
357 int stype = ipv6_addr_type(&ip6h->saddr);
358 u8 proto = ip6h->nexthdr;
362 if (mtu < IPV6_MIN_MTU)
365 if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
366 stype == IPV6_ADDR_LOOPBACK)
369 offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
371 if (offset < 0 || (frag_off & htons(~0x7)))
374 if (proto == IPPROTO_ICMPV6) {
375 struct icmp6hdr *icmp6h;
377 if (!pskb_may_pull(skb, skb_network_header(skb) +
378 offset + 1 - skb->data))
381 icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
382 if (icmpv6_is_err(icmp6h->icmp6_type) ||
383 icmp6h->icmp6_type == NDISC_REDIRECT)
387 return iptunnel_pmtud_build_icmpv6(skb, mtu);
389 #endif /* IS_ENABLED(CONFIG_IPV6) */
392 * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed
393 * @skb: Buffer being sent by encapsulation, L2 headers expected
394 * @encap_dst: Destination for tunnel encapsulation (outer IP)
395 * @headroom: Encapsulation header size, bytes
396 * @reply: Build matching ICMP or ICMPv6 message as a result
398 * L2 tunnel implementations that can carry IP and can be directly bridged
399 * (currently UDP tunnels) can't always rely on IP forwarding paths to handle
400 * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built
401 * based on payload and sent back by the encapsulation itself.
403 * For routable interfaces, we just need to update the PMTU for the destination.
405 * Return: 0 if ICMP error not needed, length if built, negative value on error
407 int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
408 int headroom, bool reply)
410 u32 mtu = dst_mtu(encap_dst) - headroom;
412 if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
413 (!skb_is_gso(skb) && (skb->len - skb_network_offset(skb)) <= mtu))
416 skb_dst_update_pmtu_no_confirm(skb, mtu);
418 if (!reply || skb->pkt_type == PACKET_HOST)
421 if (skb->protocol == htons(ETH_P_IP))
422 return iptunnel_pmtud_check_icmp(skb, mtu);
424 #if IS_ENABLED(CONFIG_IPV6)
425 if (skb->protocol == htons(ETH_P_IPV6))
426 return iptunnel_pmtud_check_icmpv6(skb, mtu);
430 EXPORT_SYMBOL(skb_tunnel_check_pmtu);
432 /* Often modified stats are per cpu, other are shared (netdev->stats) */
433 void ip_tunnel_get_stats64(struct net_device *dev,
434 struct rtnl_link_stats64 *tot)
436 netdev_stats_to_stats64(tot, &dev->stats);
437 dev_fetch_sw_netstats(tot, dev->tstats);
439 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
441 static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
442 [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
443 [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
444 [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
445 [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
446 [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
447 [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
448 [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
449 [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
452 static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
453 [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
454 [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
455 [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
458 static const struct nla_policy
459 geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
460 [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
461 [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
462 [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 },
465 static const struct nla_policy
466 vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
467 [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
470 static const struct nla_policy
471 erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
472 [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
473 [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
474 [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
475 [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
478 static int ip_tun_parse_opts_geneve(struct nlattr *attr,
479 struct ip_tunnel_info *info, int opts_len,
480 struct netlink_ext_ack *extack)
482 struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
485 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
486 geneve_opt_policy, extack);
490 if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
491 !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
492 !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
495 attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
496 data_len = nla_len(attr);
501 struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
503 memcpy(opt->opt_data, nla_data(attr), data_len);
504 opt->length = data_len / 4;
505 attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
506 opt->opt_class = nla_get_be16(attr);
507 attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
508 opt->type = nla_get_u8(attr);
509 info->key.tun_flags |= TUNNEL_GENEVE_OPT;
512 return sizeof(struct geneve_opt) + data_len;
515 static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
516 struct ip_tunnel_info *info, int opts_len,
517 struct netlink_ext_ack *extack)
519 struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
522 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
523 vxlan_opt_policy, extack);
527 if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
531 struct vxlan_metadata *md =
532 ip_tunnel_info_opts(info) + opts_len;
534 attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
535 md->gbp = nla_get_u32(attr);
536 md->gbp &= VXLAN_GBP_MASK;
537 info->key.tun_flags |= TUNNEL_VXLAN_OPT;
540 return sizeof(struct vxlan_metadata);
543 static int ip_tun_parse_opts_erspan(struct nlattr *attr,
544 struct ip_tunnel_info *info, int opts_len,
545 struct netlink_ext_ack *extack)
547 struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
551 err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
552 erspan_opt_policy, extack);
556 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
559 ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
561 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
563 } else if (ver == 2) {
564 if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
565 !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
572 struct erspan_metadata *md =
573 ip_tunnel_info_opts(info) + opts_len;
577 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
578 md->u.index = nla_get_be32(attr);
580 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
581 md->u.md2.dir = nla_get_u8(attr);
582 attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
583 set_hwid(&md->u.md2, nla_get_u8(attr));
586 info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
589 return sizeof(struct erspan_metadata);
592 static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
593 struct netlink_ext_ack *extack)
595 int err, rem, opt_len, opts_len = 0, type = 0;
601 err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
602 ip_opts_policy, extack);
606 nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
607 switch (nla_type(nla)) {
608 case LWTUNNEL_IP_OPTS_GENEVE:
609 if (type && type != TUNNEL_GENEVE_OPT)
611 opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
616 if (opts_len > IP_TUNNEL_OPTS_MAX)
618 type = TUNNEL_GENEVE_OPT;
620 case LWTUNNEL_IP_OPTS_VXLAN:
623 opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
628 type = TUNNEL_VXLAN_OPT;
630 case LWTUNNEL_IP_OPTS_ERSPAN:
633 opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
638 type = TUNNEL_ERSPAN_OPT;
648 static int ip_tun_get_optlen(struct nlattr *attr,
649 struct netlink_ext_ack *extack)
651 return ip_tun_parse_opts(attr, NULL, extack);
654 static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
655 struct netlink_ext_ack *extack)
657 return ip_tun_parse_opts(attr, info, extack);
660 static int ip_tun_build_state(struct net *net, struct nlattr *attr,
661 unsigned int family, const void *cfg,
662 struct lwtunnel_state **ts,
663 struct netlink_ext_ack *extack)
665 struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
666 struct lwtunnel_state *new_state;
667 struct ip_tunnel_info *tun_info;
670 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
671 ip_tun_policy, extack);
675 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
679 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
683 new_state->type = LWTUNNEL_ENCAP_IP;
685 tun_info = lwt_tun_info(new_state);
687 err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
689 lwtstate_free(new_state);
693 #ifdef CONFIG_DST_CACHE
694 err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
696 lwtstate_free(new_state);
701 if (tb[LWTUNNEL_IP_ID])
702 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
704 if (tb[LWTUNNEL_IP_DST])
705 tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
707 if (tb[LWTUNNEL_IP_SRC])
708 tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
710 if (tb[LWTUNNEL_IP_TTL])
711 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
713 if (tb[LWTUNNEL_IP_TOS])
714 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
716 if (tb[LWTUNNEL_IP_FLAGS])
717 tun_info->key.tun_flags |=
718 (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
719 ~TUNNEL_OPTIONS_PRESENT);
721 tun_info->mode = IP_TUNNEL_INFO_TX;
722 tun_info->options_len = opt_len;
729 static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
731 #ifdef CONFIG_DST_CACHE
732 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
734 dst_cache_destroy(&tun_info->dst_cache);
738 static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
739 struct ip_tunnel_info *tun_info)
741 struct geneve_opt *opt;
745 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
749 while (tun_info->options_len > offset) {
750 opt = ip_tunnel_info_opts(tun_info) + offset;
751 if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
753 nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
754 nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
756 nla_nest_cancel(skb, nest);
759 offset += sizeof(*opt) + opt->length * 4;
762 nla_nest_end(skb, nest);
766 static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
767 struct ip_tunnel_info *tun_info)
769 struct vxlan_metadata *md;
772 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
776 md = ip_tunnel_info_opts(tun_info);
777 if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
778 nla_nest_cancel(skb, nest);
782 nla_nest_end(skb, nest);
786 static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
787 struct ip_tunnel_info *tun_info)
789 struct erspan_metadata *md;
792 nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
796 md = ip_tunnel_info_opts(tun_info);
797 if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
800 if (md->version == 1 &&
801 nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
804 if (md->version == 2 &&
805 (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
806 nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
807 get_hwid(&md->u.md2))))
810 nla_nest_end(skb, nest);
813 nla_nest_cancel(skb, nest);
817 static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
818 struct ip_tunnel_info *tun_info)
823 if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
826 nest = nla_nest_start_noflag(skb, type);
830 if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
831 err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
832 else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
833 err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
834 else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
835 err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
838 nla_nest_cancel(skb, nest);
842 nla_nest_end(skb, nest);
846 static int ip_tun_fill_encap_info(struct sk_buff *skb,
847 struct lwtunnel_state *lwtstate)
849 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
851 if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
853 nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
854 nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
855 nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
856 nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
857 nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
858 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
864 static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
868 if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
871 opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */
872 if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
873 struct geneve_opt *opt;
876 opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */
877 while (info->options_len > offset) {
878 opt = ip_tunnel_info_opts(info) + offset;
879 opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */
880 + nla_total_size(1) /* OPT_GENEVE_TYPE */
881 + nla_total_size(opt->length * 4);
882 /* OPT_GENEVE_DATA */
883 offset += sizeof(*opt) + opt->length * 4;
885 } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
886 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */
887 + nla_total_size(4); /* OPT_VXLAN_GBP */
888 } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
889 struct erspan_metadata *md = ip_tunnel_info_opts(info);
891 opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */
892 + nla_total_size(1) /* OPT_ERSPAN_VER */
893 + (md->version == 1 ? nla_total_size(4)
894 /* OPT_ERSPAN_INDEX (v1) */
895 : nla_total_size(1) +
897 /* OPT_ERSPAN_DIR + HWID (v2) */
903 static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
905 return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
906 + nla_total_size(4) /* LWTUNNEL_IP_DST */
907 + nla_total_size(4) /* LWTUNNEL_IP_SRC */
908 + nla_total_size(1) /* LWTUNNEL_IP_TOS */
909 + nla_total_size(1) /* LWTUNNEL_IP_TTL */
910 + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */
911 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
912 /* LWTUNNEL_IP_OPTS */
915 static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
917 struct ip_tunnel_info *info_a = lwt_tun_info(a);
918 struct ip_tunnel_info *info_b = lwt_tun_info(b);
920 return memcmp(info_a, info_b, sizeof(info_a->key)) ||
921 info_a->mode != info_b->mode ||
922 info_a->options_len != info_b->options_len ||
923 memcmp(ip_tunnel_info_opts(info_a),
924 ip_tunnel_info_opts(info_b), info_a->options_len);
927 static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
928 .build_state = ip_tun_build_state,
929 .destroy_state = ip_tun_destroy_state,
930 .fill_encap = ip_tun_fill_encap_info,
931 .get_encap_size = ip_tun_encap_nlsize,
932 .cmp_encap = ip_tun_cmp_encap,
933 .owner = THIS_MODULE,
936 static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
937 [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
938 [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
939 [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
940 [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
941 [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
942 [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
943 [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
944 [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
947 static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
948 unsigned int family, const void *cfg,
949 struct lwtunnel_state **ts,
950 struct netlink_ext_ack *extack)
952 struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
953 struct lwtunnel_state *new_state;
954 struct ip_tunnel_info *tun_info;
957 err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
958 ip6_tun_policy, extack);
962 opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
966 new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
970 new_state->type = LWTUNNEL_ENCAP_IP6;
972 tun_info = lwt_tun_info(new_state);
974 err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
976 lwtstate_free(new_state);
980 if (tb[LWTUNNEL_IP6_ID])
981 tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
983 if (tb[LWTUNNEL_IP6_DST])
984 tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
986 if (tb[LWTUNNEL_IP6_SRC])
987 tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
989 if (tb[LWTUNNEL_IP6_HOPLIMIT])
990 tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
992 if (tb[LWTUNNEL_IP6_TC])
993 tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
995 if (tb[LWTUNNEL_IP6_FLAGS])
996 tun_info->key.tun_flags |=
997 (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
998 ~TUNNEL_OPTIONS_PRESENT);
1000 tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
1001 tun_info->options_len = opt_len;
1008 static int ip6_tun_fill_encap_info(struct sk_buff *skb,
1009 struct lwtunnel_state *lwtstate)
1011 struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
1013 if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
1014 LWTUNNEL_IP6_PAD) ||
1015 nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
1016 nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
1017 nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
1018 nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
1019 nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
1020 ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
1026 static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
1028 return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
1029 + nla_total_size(16) /* LWTUNNEL_IP6_DST */
1030 + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
1031 + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
1032 + nla_total_size(1) /* LWTUNNEL_IP6_TC */
1033 + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */
1034 + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
1035 /* LWTUNNEL_IP6_OPTS */
1038 static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
1039 .build_state = ip6_tun_build_state,
1040 .fill_encap = ip6_tun_fill_encap_info,
1041 .get_encap_size = ip6_tun_encap_nlsize,
1042 .cmp_encap = ip_tun_cmp_encap,
1043 .owner = THIS_MODULE,
1046 void __init ip_tunnel_core_init(void)
1048 /* If you land here, make sure whether increasing ip_tunnel_info's
1049 * options_len is a reasonable choice with its usage in front ends
1050 * (f.e., it's part of flow keys, etc).
1052 BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
1054 lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
1055 lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
1058 DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
1059 EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
1061 void ip_tunnel_need_metadata(void)
1063 static_branch_inc(&ip_tunnel_metadata_cnt);
1065 EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
1067 void ip_tunnel_unneed_metadata(void)
1069 static_branch_dec(&ip_tunnel_metadata_cnt);
1071 EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
1073 /* Returns either the correct skb->protocol value, or 0 if invalid. */
1074 __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
1076 if (skb_network_header(skb) >= skb->head &&
1077 (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
1078 ip_hdr(skb)->version == 4)
1079 return htons(ETH_P_IP);
1080 if (skb_network_header(skb) >= skb->head &&
1081 (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
1082 ipv6_hdr(skb)->version == 6)
1083 return htons(ETH_P_IPV6);
1086 EXPORT_SYMBOL(ip_tunnel_parse_protocol);
1088 const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
1089 EXPORT_SYMBOL(ip_tunnel_header_ops);