1c9422999SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20e6fbc5bSPravin B Shelar /*
30e6fbc5bSPravin B Shelar * Copyright (c) 2013 Nicira, Inc.
40e6fbc5bSPravin B Shelar */
50e6fbc5bSPravin B Shelar
60e6fbc5bSPravin B Shelar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
70e6fbc5bSPravin B Shelar
80e6fbc5bSPravin B Shelar #include <linux/types.h>
90e6fbc5bSPravin B Shelar #include <linux/kernel.h>
100e6fbc5bSPravin B Shelar #include <linux/skbuff.h>
110e6fbc5bSPravin B Shelar #include <linux/netdevice.h>
120e6fbc5bSPravin B Shelar #include <linux/in.h>
130e6fbc5bSPravin B Shelar #include <linux/if_arp.h>
140e6fbc5bSPravin B Shelar #include <linux/init.h>
150e6fbc5bSPravin B Shelar #include <linux/in6.h>
160e6fbc5bSPravin B Shelar #include <linux/inetdevice.h>
170e6fbc5bSPravin B Shelar #include <linux/netfilter_ipv4.h>
180e6fbc5bSPravin B Shelar #include <linux/etherdevice.h>
190e6fbc5bSPravin B Shelar #include <linux/if_ether.h>
200e6fbc5bSPravin B Shelar #include <linux/if_vlan.h>
21e7030878SThomas Graf #include <linux/static_key.h>
220e6fbc5bSPravin B Shelar
230e6fbc5bSPravin B Shelar #include <net/ip.h>
240e6fbc5bSPravin B Shelar #include <net/icmp.h>
250e6fbc5bSPravin B Shelar #include <net/protocol.h>
260e6fbc5bSPravin B Shelar #include <net/ip_tunnels.h>
27058214a4STom Herbert #include <net/ip6_tunnel.h>
288ed54f16SStefano Brivio #include <net/ip6_checksum.h>
290e6fbc5bSPravin B Shelar #include <net/arp.h>
300e6fbc5bSPravin B Shelar #include <net/checksum.h>
310e6fbc5bSPravin B Shelar #include <net/dsfield.h>
320e6fbc5bSPravin B Shelar #include <net/inet_ecn.h>
330e6fbc5bSPravin B Shelar #include <net/xfrm.h>
340e6fbc5bSPravin B Shelar #include <net/net_namespace.h>
350e6fbc5bSPravin B Shelar #include <net/netns/generic.h>
360e6fbc5bSPravin B Shelar #include <net/rtnetlink.h>
3763d008a4SJiri Benc #include <net/dst_metadata.h>
384ece4778SXin Long #include <net/geneve.h>
39edf31cbbSXin Long #include <net/vxlan.h>
40b0a21810SXin Long #include <net/erspan.h>
410e6fbc5bSPravin B Shelar
4255c2bc14STom Herbert const struct ip_tunnel_encap_ops __rcu *
4355c2bc14STom Herbert iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
4455c2bc14STom Herbert EXPORT_SYMBOL(iptun_encaps);
4555c2bc14STom Herbert
46058214a4STom Herbert const struct ip6_tnl_encap_ops __rcu *
47058214a4STom Herbert ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
48058214a4STom Herbert EXPORT_SYMBOL(ip6tun_encaps);
49058214a4STom Herbert
iptunnel_xmit(struct sock * sk,struct rtable * rt,struct sk_buff * skb,__be32 src,__be32 dst,__u8 proto,__u8 tos,__u8 ttl,__be16 df,bool xnet)50039f5062SPravin B Shelar void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
510e6fbc5bSPravin B Shelar __be32 src, __be32 dst, __u8 proto,
52963a88b3SNicolas Dichtel __u8 tos, __u8 ttl, __be16 df, bool xnet)
530e6fbc5bSPravin B Shelar {
54bc22a0e2SNicolas Dichtel int pkt_len = skb->len - skb_inner_network_offset(skb);
55f859b0f6SEric W. Biederman struct net *net = dev_net(rt->dst.dev);
56039f5062SPravin B Shelar struct net_device *dev = skb->dev;
570e6fbc5bSPravin B Shelar struct iphdr *iph;
580e6fbc5bSPravin B Shelar int err;
590e6fbc5bSPravin B Shelar
60963a88b3SNicolas Dichtel skb_scrub_packet(skb, xnet);
61963a88b3SNicolas Dichtel
62bf8d85d4SEric Dumazet skb_clear_hash_if_not_l4(skb);
630e6fbc5bSPravin B Shelar skb_dst_set(skb, &rt->dst);
640e6fbc5bSPravin B Shelar memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
650e6fbc5bSPravin B Shelar
660e6fbc5bSPravin B Shelar /* Push down and install the IP header. */
6778a3694dSSteffen Klassert skb_push(skb, sizeof(struct iphdr));
680e6fbc5bSPravin B Shelar skb_reset_network_header(skb);
690e6fbc5bSPravin B Shelar
700e6fbc5bSPravin B Shelar iph = ip_hdr(skb);
710e6fbc5bSPravin B Shelar
720e6fbc5bSPravin B Shelar iph->version = 4;
730e6fbc5bSPravin B Shelar iph->ihl = sizeof(struct iphdr) >> 2;
7416f7eb2bSSabrina Dubroca iph->frag_off = ip_mtu_locked(&rt->dst) ? 0 : df;
750e6fbc5bSPravin B Shelar iph->protocol = proto;
760e6fbc5bSPravin B Shelar iph->tos = tos;
770e6fbc5bSPravin B Shelar iph->daddr = dst;
780e6fbc5bSPravin B Shelar iph->saddr = src;
790e6fbc5bSPravin B Shelar iph->ttl = ttl;
80f859b0f6SEric W. Biederman __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
810e6fbc5bSPravin B Shelar
8233224b16SEric W. Biederman err = ip_local_out(net, sk, skb);
835684abf7SXin Long
845684abf7SXin Long if (dev) {
850e6fbc5bSPravin B Shelar if (unlikely(net_xmit_eval(err)))
860e6fbc5bSPravin B Shelar pkt_len = 0;
87039f5062SPravin B Shelar iptunnel_xmit_stats(dev, pkt_len);
880e6fbc5bSPravin B Shelar }
895684abf7SXin Long }
900e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit);
913d7b46cdSPravin B Shelar
__iptunnel_pull_header(struct sk_buff * skb,int hdr_len,__be16 inner_proto,bool raw_proto,bool xnet)92a6d5bbf3SJiri Benc int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
93a6d5bbf3SJiri Benc __be16 inner_proto, bool raw_proto, bool xnet)
943d7b46cdSPravin B Shelar {
953d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, hdr_len)))
963d7b46cdSPravin B Shelar return -ENOMEM;
973d7b46cdSPravin B Shelar
983d7b46cdSPravin B Shelar skb_pull_rcsum(skb, hdr_len);
993d7b46cdSPravin B Shelar
100a6d5bbf3SJiri Benc if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
1011245dfc8SLi RongQing struct ethhdr *eh;
1023d7b46cdSPravin B Shelar
1033d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1043d7b46cdSPravin B Shelar return -ENOMEM;
1053d7b46cdSPravin B Shelar
1061245dfc8SLi RongQing eh = (struct ethhdr *)skb->data;
107d181ddcaSAlexander Duyck if (likely(eth_proto_is_802_3(eh->h_proto)))
1083d7b46cdSPravin B Shelar skb->protocol = eh->h_proto;
1093d7b46cdSPravin B Shelar else
1103d7b46cdSPravin B Shelar skb->protocol = htons(ETH_P_802_2);
1113d7b46cdSPravin B Shelar
1123d7b46cdSPravin B Shelar } else {
1133d7b46cdSPravin B Shelar skb->protocol = inner_proto;
1143d7b46cdSPravin B Shelar }
1153d7b46cdSPravin B Shelar
1167539fadcSTom Herbert skb_clear_hash_if_not_l4(skb);
1173e2ed0c2SMichał Mirosław __vlan_hwaccel_clear_tag(skb);
1183d7b46cdSPravin B Shelar skb_set_queue_mapping(skb, 0);
1197f290c94SJiri Benc skb_scrub_packet(skb, xnet);
120a09a4c8dSJesse Gross
121a09a4c8dSJesse Gross return iptunnel_pull_offloads(skb);
1223d7b46cdSPravin B Shelar }
123a6d5bbf3SJiri Benc EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
1242d26f0a3SEric Dumazet
iptunnel_metadata_reply(struct metadata_dst * md,gfp_t flags)12563d008a4SJiri Benc struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
12663d008a4SJiri Benc gfp_t flags)
12763d008a4SJiri Benc {
12863d008a4SJiri Benc struct metadata_dst *res;
12963d008a4SJiri Benc struct ip_tunnel_info *dst, *src;
13063d008a4SJiri Benc
1313fcece12SJakub Kicinski if (!md || md->type != METADATA_IP_TUNNEL ||
1323fcece12SJakub Kicinski md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
13363d008a4SJiri Benc return NULL;
13463d008a4SJiri Benc
135f52f11ecSXin Long src = &md->u.tun_info;
136f52f11ecSXin Long res = metadata_dst_alloc(src->options_len, METADATA_IP_TUNNEL, flags);
13763d008a4SJiri Benc if (!res)
13863d008a4SJiri Benc return NULL;
13963d008a4SJiri Benc
14063d008a4SJiri Benc dst = &res->u.tun_info;
14163d008a4SJiri Benc dst->key.tun_id = src->key.tun_id;
14263d008a4SJiri Benc if (src->mode & IP_TUNNEL_INFO_IPV6)
14363d008a4SJiri Benc memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
14463d008a4SJiri Benc sizeof(struct in6_addr));
14563d008a4SJiri Benc else
14663d008a4SJiri Benc dst->key.u.ipv4.dst = src->key.u.ipv4.src;
1477bdca378Swenxu dst->key.tun_flags = src->key.tun_flags;
14863d008a4SJiri Benc dst->mode = src->mode | IP_TUNNEL_INFO_TX;
149f52f11ecSXin Long ip_tunnel_info_opts_set(dst, ip_tunnel_info_opts(src),
150f52f11ecSXin Long src->options_len, 0);
15163d008a4SJiri Benc
15263d008a4SJiri Benc return res;
15363d008a4SJiri Benc }
15463d008a4SJiri Benc EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
15563d008a4SJiri Benc
iptunnel_handle_offloads(struct sk_buff * skb,int gso_type_mask)156aed069dfSAlexander Duyck int iptunnel_handle_offloads(struct sk_buff *skb,
1572d26f0a3SEric Dumazet int gso_type_mask)
1582d26f0a3SEric Dumazet {
1592d26f0a3SEric Dumazet int err;
1602d26f0a3SEric Dumazet
1612d26f0a3SEric Dumazet if (likely(!skb->encapsulation)) {
1622d26f0a3SEric Dumazet skb_reset_inner_headers(skb);
1632d26f0a3SEric Dumazet skb->encapsulation = 1;
1642d26f0a3SEric Dumazet }
1652d26f0a3SEric Dumazet
1662d26f0a3SEric Dumazet if (skb_is_gso(skb)) {
1679580bf2eSEric Dumazet err = skb_header_unclone(skb, GFP_ATOMIC);
1682d26f0a3SEric Dumazet if (unlikely(err))
169aed069dfSAlexander Duyck return err;
1702d26f0a3SEric Dumazet skb_shinfo(skb)->gso_type |= gso_type_mask;
171aed069dfSAlexander Duyck return 0;
1722d26f0a3SEric Dumazet }
1732d26f0a3SEric Dumazet
1746fa79666SEdward Cree if (skb->ip_summed != CHECKSUM_PARTIAL) {
1752d26f0a3SEric Dumazet skb->ip_summed = CHECKSUM_NONE;
1766fa79666SEdward Cree /* We clear encapsulation here to prevent badly-written
1776fa79666SEdward Cree * drivers potentially deciding to offload an inner checksum
1786fa79666SEdward Cree * if we set CHECKSUM_PARTIAL on the outer header.
1796fa79666SEdward Cree * This should go away when the drivers are all fixed.
1806fa79666SEdward Cree */
181179bc67fSEdward Cree skb->encapsulation = 0;
182179bc67fSEdward Cree }
1832d26f0a3SEric Dumazet
184aed069dfSAlexander Duyck return 0;
1852d26f0a3SEric Dumazet }
1862d26f0a3SEric Dumazet EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
187ebe44f35SDavid S. Miller
1884cb47a86SStefano Brivio /**
1894cb47a86SStefano Brivio * iptunnel_pmtud_build_icmp() - Build ICMP error message for PMTUD
1904cb47a86SStefano Brivio * @skb: Original packet with L2 header
1914cb47a86SStefano Brivio * @mtu: MTU value for ICMP error
1924cb47a86SStefano Brivio *
1934cb47a86SStefano Brivio * Return: length on success, negative error code if message couldn't be built.
1944cb47a86SStefano Brivio */
iptunnel_pmtud_build_icmp(struct sk_buff * skb,int mtu)1954cb47a86SStefano Brivio static int iptunnel_pmtud_build_icmp(struct sk_buff *skb, int mtu)
1964cb47a86SStefano Brivio {
1974cb47a86SStefano Brivio const struct iphdr *iph = ip_hdr(skb);
1984cb47a86SStefano Brivio struct icmphdr *icmph;
1994cb47a86SStefano Brivio struct iphdr *niph;
2004cb47a86SStefano Brivio struct ethhdr eh;
2014cb47a86SStefano Brivio int len, err;
2024cb47a86SStefano Brivio
2034cb47a86SStefano Brivio if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
2044cb47a86SStefano Brivio return -EINVAL;
2054cb47a86SStefano Brivio
2064cb47a86SStefano Brivio skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
2074cb47a86SStefano Brivio pskb_pull(skb, ETH_HLEN);
2084cb47a86SStefano Brivio skb_reset_network_header(skb);
2094cb47a86SStefano Brivio
2104cb47a86SStefano Brivio err = pskb_trim(skb, 576 - sizeof(*niph) - sizeof(*icmph));
2114cb47a86SStefano Brivio if (err)
2124cb47a86SStefano Brivio return err;
2134cb47a86SStefano Brivio
2144cb47a86SStefano Brivio len = skb->len + sizeof(*icmph);
2154cb47a86SStefano Brivio err = skb_cow(skb, sizeof(*niph) + sizeof(*icmph) + ETH_HLEN);
2164cb47a86SStefano Brivio if (err)
2174cb47a86SStefano Brivio return err;
2184cb47a86SStefano Brivio
2194cb47a86SStefano Brivio icmph = skb_push(skb, sizeof(*icmph));
2204cb47a86SStefano Brivio *icmph = (struct icmphdr) {
2214cb47a86SStefano Brivio .type = ICMP_DEST_UNREACH,
2224cb47a86SStefano Brivio .code = ICMP_FRAG_NEEDED,
2234cb47a86SStefano Brivio .checksum = 0,
2244cb47a86SStefano Brivio .un.frag.__unused = 0,
225fda4fde2SJulian Wiedmann .un.frag.mtu = htons(mtu),
2264cb47a86SStefano Brivio };
2276a7ac3d2SFlorian Westphal icmph->checksum = csum_fold(skb_checksum(skb, 0, len, 0));
2284cb47a86SStefano Brivio skb_reset_transport_header(skb);
2294cb47a86SStefano Brivio
2304cb47a86SStefano Brivio niph = skb_push(skb, sizeof(*niph));
2314cb47a86SStefano Brivio *niph = (struct iphdr) {
2324cb47a86SStefano Brivio .ihl = sizeof(*niph) / 4u,
2334cb47a86SStefano Brivio .version = 4,
2344cb47a86SStefano Brivio .tos = 0,
2354cb47a86SStefano Brivio .tot_len = htons(len + sizeof(*niph)),
2364cb47a86SStefano Brivio .id = 0,
2374cb47a86SStefano Brivio .frag_off = htons(IP_DF),
2384cb47a86SStefano Brivio .ttl = iph->ttl,
2394cb47a86SStefano Brivio .protocol = IPPROTO_ICMP,
2404cb47a86SStefano Brivio .saddr = iph->daddr,
2414cb47a86SStefano Brivio .daddr = iph->saddr,
2424cb47a86SStefano Brivio };
2434cb47a86SStefano Brivio ip_send_check(niph);
2444cb47a86SStefano Brivio skb_reset_network_header(skb);
2454cb47a86SStefano Brivio
2464cb47a86SStefano Brivio skb->ip_summed = CHECKSUM_NONE;
2474cb47a86SStefano Brivio
248fda4fde2SJulian Wiedmann eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
2494cb47a86SStefano Brivio skb_reset_mac_header(skb);
2504cb47a86SStefano Brivio
2514cb47a86SStefano Brivio return skb->len;
2524cb47a86SStefano Brivio }
2534cb47a86SStefano Brivio
2544cb47a86SStefano Brivio /**
2554cb47a86SStefano Brivio * iptunnel_pmtud_check_icmp() - Trigger ICMP reply if needed and allowed
2564cb47a86SStefano Brivio * @skb: Buffer being sent by encapsulation, L2 headers expected
2574cb47a86SStefano Brivio * @mtu: Network MTU for path
2584cb47a86SStefano Brivio *
2594cb47a86SStefano Brivio * Return: 0 for no ICMP reply, length if built, negative value on error.
2604cb47a86SStefano Brivio */
iptunnel_pmtud_check_icmp(struct sk_buff * skb,int mtu)2614cb47a86SStefano Brivio static int iptunnel_pmtud_check_icmp(struct sk_buff *skb, int mtu)
2624cb47a86SStefano Brivio {
2634cb47a86SStefano Brivio const struct icmphdr *icmph = icmp_hdr(skb);
2644cb47a86SStefano Brivio const struct iphdr *iph = ip_hdr(skb);
2654cb47a86SStefano Brivio
26677a2d673SStefano Brivio if (mtu < 576 || iph->frag_off != htons(IP_DF))
2674cb47a86SStefano Brivio return 0;
2684cb47a86SStefano Brivio
2694cb47a86SStefano Brivio if (ipv4_is_lbcast(iph->daddr) || ipv4_is_multicast(iph->daddr) ||
2704cb47a86SStefano Brivio ipv4_is_zeronet(iph->saddr) || ipv4_is_loopback(iph->saddr) ||
2714cb47a86SStefano Brivio ipv4_is_lbcast(iph->saddr) || ipv4_is_multicast(iph->saddr))
2724cb47a86SStefano Brivio return 0;
2734cb47a86SStefano Brivio
2744cb47a86SStefano Brivio if (iph->protocol == IPPROTO_ICMP && icmp_is_err(icmph->type))
2754cb47a86SStefano Brivio return 0;
2764cb47a86SStefano Brivio
2774cb47a86SStefano Brivio return iptunnel_pmtud_build_icmp(skb, mtu);
2784cb47a86SStefano Brivio }
2794cb47a86SStefano Brivio
2804cb47a86SStefano Brivio #if IS_ENABLED(CONFIG_IPV6)
2814cb47a86SStefano Brivio /**
2824cb47a86SStefano Brivio * iptunnel_pmtud_build_icmpv6() - Build ICMPv6 error message for PMTUD
2834cb47a86SStefano Brivio * @skb: Original packet with L2 header
2844cb47a86SStefano Brivio * @mtu: MTU value for ICMPv6 error
2854cb47a86SStefano Brivio *
2864cb47a86SStefano Brivio * Return: length on success, negative error code if message couldn't be built.
2874cb47a86SStefano Brivio */
iptunnel_pmtud_build_icmpv6(struct sk_buff * skb,int mtu)2884cb47a86SStefano Brivio static int iptunnel_pmtud_build_icmpv6(struct sk_buff *skb, int mtu)
2894cb47a86SStefano Brivio {
2904cb47a86SStefano Brivio const struct ipv6hdr *ip6h = ipv6_hdr(skb);
2914cb47a86SStefano Brivio struct icmp6hdr *icmp6h;
2924cb47a86SStefano Brivio struct ipv6hdr *nip6h;
2934cb47a86SStefano Brivio struct ethhdr eh;
2944cb47a86SStefano Brivio int len, err;
2954cb47a86SStefano Brivio __wsum csum;
2964cb47a86SStefano Brivio
2974cb47a86SStefano Brivio if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
2984cb47a86SStefano Brivio return -EINVAL;
2994cb47a86SStefano Brivio
3004cb47a86SStefano Brivio skb_copy_bits(skb, skb_mac_offset(skb), &eh, ETH_HLEN);
3014cb47a86SStefano Brivio pskb_pull(skb, ETH_HLEN);
3024cb47a86SStefano Brivio skb_reset_network_header(skb);
3034cb47a86SStefano Brivio
3044cb47a86SStefano Brivio err = pskb_trim(skb, IPV6_MIN_MTU - sizeof(*nip6h) - sizeof(*icmp6h));
3054cb47a86SStefano Brivio if (err)
3064cb47a86SStefano Brivio return err;
3074cb47a86SStefano Brivio
3084cb47a86SStefano Brivio len = skb->len + sizeof(*icmp6h);
3094cb47a86SStefano Brivio err = skb_cow(skb, sizeof(*nip6h) + sizeof(*icmp6h) + ETH_HLEN);
3104cb47a86SStefano Brivio if (err)
3114cb47a86SStefano Brivio return err;
3124cb47a86SStefano Brivio
3134cb47a86SStefano Brivio icmp6h = skb_push(skb, sizeof(*icmp6h));
3144cb47a86SStefano Brivio *icmp6h = (struct icmp6hdr) {
3154cb47a86SStefano Brivio .icmp6_type = ICMPV6_PKT_TOOBIG,
3164cb47a86SStefano Brivio .icmp6_code = 0,
3174cb47a86SStefano Brivio .icmp6_cksum = 0,
3184cb47a86SStefano Brivio .icmp6_mtu = htonl(mtu),
3194cb47a86SStefano Brivio };
3204cb47a86SStefano Brivio skb_reset_transport_header(skb);
3214cb47a86SStefano Brivio
3224cb47a86SStefano Brivio nip6h = skb_push(skb, sizeof(*nip6h));
3234cb47a86SStefano Brivio *nip6h = (struct ipv6hdr) {
3244cb47a86SStefano Brivio .priority = 0,
3254cb47a86SStefano Brivio .version = 6,
3264cb47a86SStefano Brivio .flow_lbl = { 0 },
3274cb47a86SStefano Brivio .payload_len = htons(len),
3284cb47a86SStefano Brivio .nexthdr = IPPROTO_ICMPV6,
3294cb47a86SStefano Brivio .hop_limit = ip6h->hop_limit,
3304cb47a86SStefano Brivio .saddr = ip6h->daddr,
3314cb47a86SStefano Brivio .daddr = ip6h->saddr,
3324cb47a86SStefano Brivio };
3334cb47a86SStefano Brivio skb_reset_network_header(skb);
3344cb47a86SStefano Brivio
335*510c869fSAntoine Tenart csum = skb_checksum(skb, skb_transport_offset(skb), len, 0);
3364cb47a86SStefano Brivio icmp6h->icmp6_cksum = csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr, len,
3374cb47a86SStefano Brivio IPPROTO_ICMPV6, csum);
3384cb47a86SStefano Brivio
3394cb47a86SStefano Brivio skb->ip_summed = CHECKSUM_NONE;
3404cb47a86SStefano Brivio
341fda4fde2SJulian Wiedmann eth_header(skb, skb->dev, ntohs(eh.h_proto), eh.h_source, eh.h_dest, 0);
3424cb47a86SStefano Brivio skb_reset_mac_header(skb);
3434cb47a86SStefano Brivio
3444cb47a86SStefano Brivio return skb->len;
3454cb47a86SStefano Brivio }
3464cb47a86SStefano Brivio
3474cb47a86SStefano Brivio /**
3484cb47a86SStefano Brivio * iptunnel_pmtud_check_icmpv6() - Trigger ICMPv6 reply if needed and allowed
3494cb47a86SStefano Brivio * @skb: Buffer being sent by encapsulation, L2 headers expected
3504cb47a86SStefano Brivio * @mtu: Network MTU for path
3514cb47a86SStefano Brivio *
3524cb47a86SStefano Brivio * Return: 0 for no ICMPv6 reply, length if built, negative value on error.
3534cb47a86SStefano Brivio */
iptunnel_pmtud_check_icmpv6(struct sk_buff * skb,int mtu)3544cb47a86SStefano Brivio static int iptunnel_pmtud_check_icmpv6(struct sk_buff *skb, int mtu)
3554cb47a86SStefano Brivio {
3564cb47a86SStefano Brivio const struct ipv6hdr *ip6h = ipv6_hdr(skb);
3574cb47a86SStefano Brivio int stype = ipv6_addr_type(&ip6h->saddr);
3584cb47a86SStefano Brivio u8 proto = ip6h->nexthdr;
3594cb47a86SStefano Brivio __be16 frag_off;
3604cb47a86SStefano Brivio int offset;
3614cb47a86SStefano Brivio
36277a2d673SStefano Brivio if (mtu < IPV6_MIN_MTU)
3634cb47a86SStefano Brivio return 0;
3644cb47a86SStefano Brivio
3654cb47a86SStefano Brivio if (stype == IPV6_ADDR_ANY || stype == IPV6_ADDR_MULTICAST ||
3664cb47a86SStefano Brivio stype == IPV6_ADDR_LOOPBACK)
3674cb47a86SStefano Brivio return 0;
3684cb47a86SStefano Brivio
3694cb47a86SStefano Brivio offset = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &proto,
3704cb47a86SStefano Brivio &frag_off);
3714cb47a86SStefano Brivio if (offset < 0 || (frag_off & htons(~0x7)))
3724cb47a86SStefano Brivio return 0;
3734cb47a86SStefano Brivio
3744cb47a86SStefano Brivio if (proto == IPPROTO_ICMPV6) {
3754cb47a86SStefano Brivio struct icmp6hdr *icmp6h;
3764cb47a86SStefano Brivio
3774cb47a86SStefano Brivio if (!pskb_may_pull(skb, skb_network_header(skb) +
3784cb47a86SStefano Brivio offset + 1 - skb->data))
3794cb47a86SStefano Brivio return 0;
3804cb47a86SStefano Brivio
3814cb47a86SStefano Brivio icmp6h = (struct icmp6hdr *)(skb_network_header(skb) + offset);
3824cb47a86SStefano Brivio if (icmpv6_is_err(icmp6h->icmp6_type) ||
3834cb47a86SStefano Brivio icmp6h->icmp6_type == NDISC_REDIRECT)
3844cb47a86SStefano Brivio return 0;
3854cb47a86SStefano Brivio }
3864cb47a86SStefano Brivio
3874cb47a86SStefano Brivio return iptunnel_pmtud_build_icmpv6(skb, mtu);
3884cb47a86SStefano Brivio }
3894cb47a86SStefano Brivio #endif /* IS_ENABLED(CONFIG_IPV6) */
3904cb47a86SStefano Brivio
3914cb47a86SStefano Brivio /**
3924cb47a86SStefano Brivio * skb_tunnel_check_pmtu() - Check, update PMTU and trigger ICMP reply as needed
3934cb47a86SStefano Brivio * @skb: Buffer being sent by encapsulation, L2 headers expected
3944cb47a86SStefano Brivio * @encap_dst: Destination for tunnel encapsulation (outer IP)
3954cb47a86SStefano Brivio * @headroom: Encapsulation header size, bytes
3964cb47a86SStefano Brivio * @reply: Build matching ICMP or ICMPv6 message as a result
3974cb47a86SStefano Brivio *
3984cb47a86SStefano Brivio * L2 tunnel implementations that can carry IP and can be directly bridged
3994cb47a86SStefano Brivio * (currently UDP tunnels) can't always rely on IP forwarding paths to handle
4004cb47a86SStefano Brivio * PMTU discovery. In the bridged case, ICMP or ICMPv6 messages need to be built
4014cb47a86SStefano Brivio * based on payload and sent back by the encapsulation itself.
4024cb47a86SStefano Brivio *
4034cb47a86SStefano Brivio * For routable interfaces, we just need to update the PMTU for the destination.
4044cb47a86SStefano Brivio *
4054cb47a86SStefano Brivio * Return: 0 if ICMP error not needed, length if built, negative value on error
4064cb47a86SStefano Brivio */
skb_tunnel_check_pmtu(struct sk_buff * skb,struct dst_entry * encap_dst,int headroom,bool reply)4074cb47a86SStefano Brivio int skb_tunnel_check_pmtu(struct sk_buff *skb, struct dst_entry *encap_dst,
4084cb47a86SStefano Brivio int headroom, bool reply)
4094cb47a86SStefano Brivio {
4104cb47a86SStefano Brivio u32 mtu = dst_mtu(encap_dst) - headroom;
4114cb47a86SStefano Brivio
4124cb47a86SStefano Brivio if ((skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu)) ||
413853a7614SEric Dumazet (!skb_is_gso(skb) && (skb->len - skb_network_offset(skb)) <= mtu))
4144cb47a86SStefano Brivio return 0;
4154cb47a86SStefano Brivio
4164cb47a86SStefano Brivio skb_dst_update_pmtu_no_confirm(skb, mtu);
4174cb47a86SStefano Brivio
4184cb47a86SStefano Brivio if (!reply || skb->pkt_type == PACKET_HOST)
4194cb47a86SStefano Brivio return 0;
4204cb47a86SStefano Brivio
4214cb47a86SStefano Brivio if (skb->protocol == htons(ETH_P_IP))
4224cb47a86SStefano Brivio return iptunnel_pmtud_check_icmp(skb, mtu);
4234cb47a86SStefano Brivio
4244cb47a86SStefano Brivio #if IS_ENABLED(CONFIG_IPV6)
4254cb47a86SStefano Brivio if (skb->protocol == htons(ETH_P_IPV6))
4264cb47a86SStefano Brivio return iptunnel_pmtud_check_icmpv6(skb, mtu);
4274cb47a86SStefano Brivio #endif
4284cb47a86SStefano Brivio return 0;
4294cb47a86SStefano Brivio }
4304cb47a86SStefano Brivio EXPORT_SYMBOL(skb_tunnel_check_pmtu);
4314cb47a86SStefano Brivio
432a1c234f9SJiri Benc static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
4337b6a70f7SXin Long [LWTUNNEL_IP_UNSPEC] = { .strict_start_type = LWTUNNEL_IP_OPTS },
434a1c234f9SJiri Benc [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
435a1c234f9SJiri Benc [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
436a1c234f9SJiri Benc [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
437a1c234f9SJiri Benc [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
438a1c234f9SJiri Benc [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
439a1c234f9SJiri Benc [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
4404ece4778SXin Long [LWTUNNEL_IP_OPTS] = { .type = NLA_NESTED },
4413093fbe7SThomas Graf };
4423093fbe7SThomas Graf
4434ece4778SXin Long static const struct nla_policy ip_opts_policy[LWTUNNEL_IP_OPTS_MAX + 1] = {
4444ece4778SXin Long [LWTUNNEL_IP_OPTS_GENEVE] = { .type = NLA_NESTED },
445edf31cbbSXin Long [LWTUNNEL_IP_OPTS_VXLAN] = { .type = NLA_NESTED },
446b0a21810SXin Long [LWTUNNEL_IP_OPTS_ERSPAN] = { .type = NLA_NESTED },
4474ece4778SXin Long };
4484ece4778SXin Long
4494ece4778SXin Long static const struct nla_policy
4504ece4778SXin Long geneve_opt_policy[LWTUNNEL_IP_OPT_GENEVE_MAX + 1] = {
4514ece4778SXin Long [LWTUNNEL_IP_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
4524ece4778SXin Long [LWTUNNEL_IP_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
4534ece4778SXin Long [LWTUNNEL_IP_OPT_GENEVE_DATA] = { .type = NLA_BINARY, .len = 128 },
4544ece4778SXin Long };
4554ece4778SXin Long
456edf31cbbSXin Long static const struct nla_policy
457edf31cbbSXin Long vxlan_opt_policy[LWTUNNEL_IP_OPT_VXLAN_MAX + 1] = {
458edf31cbbSXin Long [LWTUNNEL_IP_OPT_VXLAN_GBP] = { .type = NLA_U32 },
459edf31cbbSXin Long };
460edf31cbbSXin Long
461b0a21810SXin Long static const struct nla_policy
462b0a21810SXin Long erspan_opt_policy[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1] = {
463b0a21810SXin Long [LWTUNNEL_IP_OPT_ERSPAN_VER] = { .type = NLA_U8 },
464b0a21810SXin Long [LWTUNNEL_IP_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
465b0a21810SXin Long [LWTUNNEL_IP_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
466b0a21810SXin Long [LWTUNNEL_IP_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
467b0a21810SXin Long };
468b0a21810SXin Long
ip_tun_parse_opts_geneve(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)4694ece4778SXin Long static int ip_tun_parse_opts_geneve(struct nlattr *attr,
4702f1d370bSXin Long struct ip_tunnel_info *info, int opts_len,
4714ece4778SXin Long struct netlink_ext_ack *extack)
4724ece4778SXin Long {
4734ece4778SXin Long struct nlattr *tb[LWTUNNEL_IP_OPT_GENEVE_MAX + 1];
4744ece4778SXin Long int data_len, err;
4754ece4778SXin Long
476ed02551fSXin Long err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_GENEVE_MAX, attr,
477ed02551fSXin Long geneve_opt_policy, extack);
4784ece4778SXin Long if (err)
4794ece4778SXin Long return err;
4804ece4778SXin Long
4814ece4778SXin Long if (!tb[LWTUNNEL_IP_OPT_GENEVE_CLASS] ||
4824ece4778SXin Long !tb[LWTUNNEL_IP_OPT_GENEVE_TYPE] ||
4834ece4778SXin Long !tb[LWTUNNEL_IP_OPT_GENEVE_DATA])
4844ece4778SXin Long return -EINVAL;
4854ece4778SXin Long
4864ece4778SXin Long attr = tb[LWTUNNEL_IP_OPT_GENEVE_DATA];
4874ece4778SXin Long data_len = nla_len(attr);
4884ece4778SXin Long if (data_len % 4)
4894ece4778SXin Long return -EINVAL;
4904ece4778SXin Long
4914ece4778SXin Long if (info) {
4922f1d370bSXin Long struct geneve_opt *opt = ip_tunnel_info_opts(info) + opts_len;
4934ece4778SXin Long
4944ece4778SXin Long memcpy(opt->opt_data, nla_data(attr), data_len);
4954ece4778SXin Long opt->length = data_len / 4;
4964ece4778SXin Long attr = tb[LWTUNNEL_IP_OPT_GENEVE_CLASS];
4974ece4778SXin Long opt->opt_class = nla_get_be16(attr);
4984ece4778SXin Long attr = tb[LWTUNNEL_IP_OPT_GENEVE_TYPE];
4994ece4778SXin Long opt->type = nla_get_u8(attr);
5004ece4778SXin Long info->key.tun_flags |= TUNNEL_GENEVE_OPT;
5014ece4778SXin Long }
5024ece4778SXin Long
5034ece4778SXin Long return sizeof(struct geneve_opt) + data_len;
5044ece4778SXin Long }
5054ece4778SXin Long
ip_tun_parse_opts_vxlan(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)506edf31cbbSXin Long static int ip_tun_parse_opts_vxlan(struct nlattr *attr,
5072f1d370bSXin Long struct ip_tunnel_info *info, int opts_len,
508edf31cbbSXin Long struct netlink_ext_ack *extack)
509edf31cbbSXin Long {
510edf31cbbSXin Long struct nlattr *tb[LWTUNNEL_IP_OPT_VXLAN_MAX + 1];
511edf31cbbSXin Long int err;
512edf31cbbSXin Long
513ed02551fSXin Long err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_VXLAN_MAX, attr,
514ed02551fSXin Long vxlan_opt_policy, extack);
515edf31cbbSXin Long if (err)
516edf31cbbSXin Long return err;
517edf31cbbSXin Long
518edf31cbbSXin Long if (!tb[LWTUNNEL_IP_OPT_VXLAN_GBP])
519edf31cbbSXin Long return -EINVAL;
520edf31cbbSXin Long
521edf31cbbSXin Long if (info) {
5222f1d370bSXin Long struct vxlan_metadata *md =
5232f1d370bSXin Long ip_tunnel_info_opts(info) + opts_len;
524edf31cbbSXin Long
525edf31cbbSXin Long attr = tb[LWTUNNEL_IP_OPT_VXLAN_GBP];
526edf31cbbSXin Long md->gbp = nla_get_u32(attr);
527681d2cfbSXin Long md->gbp &= VXLAN_GBP_MASK;
528edf31cbbSXin Long info->key.tun_flags |= TUNNEL_VXLAN_OPT;
529edf31cbbSXin Long }
530edf31cbbSXin Long
531edf31cbbSXin Long return sizeof(struct vxlan_metadata);
532edf31cbbSXin Long }
533edf31cbbSXin Long
ip_tun_parse_opts_erspan(struct nlattr * attr,struct ip_tunnel_info * info,int opts_len,struct netlink_ext_ack * extack)534b0a21810SXin Long static int ip_tun_parse_opts_erspan(struct nlattr *attr,
5352f1d370bSXin Long struct ip_tunnel_info *info, int opts_len,
536b0a21810SXin Long struct netlink_ext_ack *extack)
537b0a21810SXin Long {
538b0a21810SXin Long struct nlattr *tb[LWTUNNEL_IP_OPT_ERSPAN_MAX + 1];
539b0a21810SXin Long int err;
5401841b982SXin Long u8 ver;
541b0a21810SXin Long
542ed02551fSXin Long err = nla_parse_nested(tb, LWTUNNEL_IP_OPT_ERSPAN_MAX, attr,
543ed02551fSXin Long erspan_opt_policy, extack);
544b0a21810SXin Long if (err)
545b0a21810SXin Long return err;
546b0a21810SXin Long
547b0a21810SXin Long if (!tb[LWTUNNEL_IP_OPT_ERSPAN_VER])
548b0a21810SXin Long return -EINVAL;
549b0a21810SXin Long
5501841b982SXin Long ver = nla_get_u8(tb[LWTUNNEL_IP_OPT_ERSPAN_VER]);
5511841b982SXin Long if (ver == 1) {
5521841b982SXin Long if (!tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX])
5531841b982SXin Long return -EINVAL;
5541841b982SXin Long } else if (ver == 2) {
5551841b982SXin Long if (!tb[LWTUNNEL_IP_OPT_ERSPAN_DIR] ||
5561841b982SXin Long !tb[LWTUNNEL_IP_OPT_ERSPAN_HWID])
5571841b982SXin Long return -EINVAL;
5581841b982SXin Long } else {
5591841b982SXin Long return -EINVAL;
5601841b982SXin Long }
5611841b982SXin Long
562b0a21810SXin Long if (info) {
5632f1d370bSXin Long struct erspan_metadata *md =
5642f1d370bSXin Long ip_tunnel_info_opts(info) + opts_len;
565b0a21810SXin Long
5661841b982SXin Long md->version = ver;
5671841b982SXin Long if (ver == 1) {
568b0a21810SXin Long attr = tb[LWTUNNEL_IP_OPT_ERSPAN_INDEX];
569b0a21810SXin Long md->u.index = nla_get_be32(attr);
5701841b982SXin Long } else {
571b0a21810SXin Long attr = tb[LWTUNNEL_IP_OPT_ERSPAN_DIR];
572b0a21810SXin Long md->u.md2.dir = nla_get_u8(attr);
573b0a21810SXin Long attr = tb[LWTUNNEL_IP_OPT_ERSPAN_HWID];
574b0a21810SXin Long set_hwid(&md->u.md2, nla_get_u8(attr));
575b0a21810SXin Long }
576b0a21810SXin Long
577b0a21810SXin Long info->key.tun_flags |= TUNNEL_ERSPAN_OPT;
578b0a21810SXin Long }
579b0a21810SXin Long
580b0a21810SXin Long return sizeof(struct erspan_metadata);
581b0a21810SXin Long }
582b0a21810SXin Long
ip_tun_parse_opts(struct nlattr * attr,struct ip_tunnel_info * info,struct netlink_ext_ack * extack)5834ece4778SXin Long static int ip_tun_parse_opts(struct nlattr *attr, struct ip_tunnel_info *info,
5844ece4778SXin Long struct netlink_ext_ack *extack)
5854ece4778SXin Long {
586fda4fde2SJulian Wiedmann int err, rem, opt_len, opts_len = 0;
5872f1d370bSXin Long struct nlattr *nla;
588fda4fde2SJulian Wiedmann __be16 type = 0;
5894ece4778SXin Long
5904ece4778SXin Long if (!attr)
5914ece4778SXin Long return 0;
5924ece4778SXin Long
5932f1d370bSXin Long err = nla_validate(nla_data(attr), nla_len(attr), LWTUNNEL_IP_OPTS_MAX,
5944ece4778SXin Long ip_opts_policy, extack);
5954ece4778SXin Long if (err)
5964ece4778SXin Long return err;
5974ece4778SXin Long
5982f1d370bSXin Long nla_for_each_attr(nla, nla_data(attr), nla_len(attr), rem) {
5992f1d370bSXin Long switch (nla_type(nla)) {
6002f1d370bSXin Long case LWTUNNEL_IP_OPTS_GENEVE:
6012f1d370bSXin Long if (type && type != TUNNEL_GENEVE_OPT)
6022f1d370bSXin Long return -EINVAL;
6032f1d370bSXin Long opt_len = ip_tun_parse_opts_geneve(nla, info, opts_len,
6042f1d370bSXin Long extack);
6052f1d370bSXin Long if (opt_len < 0)
6062f1d370bSXin Long return opt_len;
6072f1d370bSXin Long opts_len += opt_len;
6082f1d370bSXin Long if (opts_len > IP_TUNNEL_OPTS_MAX)
6092f1d370bSXin Long return -EINVAL;
6102f1d370bSXin Long type = TUNNEL_GENEVE_OPT;
6112f1d370bSXin Long break;
6122f1d370bSXin Long case LWTUNNEL_IP_OPTS_VXLAN:
6132f1d370bSXin Long if (type)
6142f1d370bSXin Long return -EINVAL;
6152f1d370bSXin Long opt_len = ip_tun_parse_opts_vxlan(nla, info, opts_len,
6162f1d370bSXin Long extack);
6172f1d370bSXin Long if (opt_len < 0)
6182f1d370bSXin Long return opt_len;
6192f1d370bSXin Long opts_len += opt_len;
6202f1d370bSXin Long type = TUNNEL_VXLAN_OPT;
6212f1d370bSXin Long break;
6222f1d370bSXin Long case LWTUNNEL_IP_OPTS_ERSPAN:
6232f1d370bSXin Long if (type)
6242f1d370bSXin Long return -EINVAL;
6252f1d370bSXin Long opt_len = ip_tun_parse_opts_erspan(nla, info, opts_len,
6262f1d370bSXin Long extack);
6272f1d370bSXin Long if (opt_len < 0)
6282f1d370bSXin Long return opt_len;
6292f1d370bSXin Long opts_len += opt_len;
6302f1d370bSXin Long type = TUNNEL_ERSPAN_OPT;
6312f1d370bSXin Long break;
6322f1d370bSXin Long default:
6332f1d370bSXin Long return -EINVAL;
6342f1d370bSXin Long }
6352f1d370bSXin Long }
6364ece4778SXin Long
6372f1d370bSXin Long return opts_len;
6384ece4778SXin Long }
6394ece4778SXin Long
ip_tun_get_optlen(struct nlattr * attr,struct netlink_ext_ack * extack)6404ece4778SXin Long static int ip_tun_get_optlen(struct nlattr *attr,
6414ece4778SXin Long struct netlink_ext_ack *extack)
6424ece4778SXin Long {
6434ece4778SXin Long return ip_tun_parse_opts(attr, NULL, extack);
6444ece4778SXin Long }
6454ece4778SXin Long
ip_tun_set_opts(struct nlattr * attr,struct ip_tunnel_info * info,struct netlink_ext_ack * extack)6464ece4778SXin Long static int ip_tun_set_opts(struct nlattr *attr, struct ip_tunnel_info *info,
6474ece4778SXin Long struct netlink_ext_ack *extack)
6484ece4778SXin Long {
6494ece4778SXin Long return ip_tun_parse_opts(attr, info, extack);
6504ece4778SXin Long }
6514ece4778SXin Long
ip_tun_build_state(struct net * net,struct nlattr * attr,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)652faee6769SAlexander Aring static int ip_tun_build_state(struct net *net, struct nlattr *attr,
653127eb7cdSTom Herbert unsigned int family, const void *cfg,
6549ae28727SDavid Ahern struct lwtunnel_state **ts,
6559ae28727SDavid Ahern struct netlink_ext_ack *extack)
6563093fbe7SThomas Graf {
657a1c234f9SJiri Benc struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
6584ece4778SXin Long struct lwtunnel_state *new_state;
6594ece4778SXin Long struct ip_tunnel_info *tun_info;
6604ece4778SXin Long int err, opt_len;
6613093fbe7SThomas Graf
6628cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP_MAX, attr,
6638cb08174SJohannes Berg ip_tun_policy, extack);
6643093fbe7SThomas Graf if (err < 0)
6653093fbe7SThomas Graf return err;
6663093fbe7SThomas Graf
6674ece4778SXin Long opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP_OPTS], extack);
6684ece4778SXin Long if (opt_len < 0)
6694ece4778SXin Long return opt_len;
6704ece4778SXin Long
6714ece4778SXin Long new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
6723093fbe7SThomas Graf if (!new_state)
6733093fbe7SThomas Graf return -ENOMEM;
6743093fbe7SThomas Graf
6753093fbe7SThomas Graf new_state->type = LWTUNNEL_ENCAP_IP;
6763093fbe7SThomas Graf
6773093fbe7SThomas Graf tun_info = lwt_tun_info(new_state);
6783093fbe7SThomas Graf
6794ece4778SXin Long err = ip_tun_set_opts(tb[LWTUNNEL_IP_OPTS], tun_info, extack);
6804ece4778SXin Long if (err < 0) {
6814ece4778SXin Long lwtstate_free(new_state);
6824ece4778SXin Long return err;
6834ece4778SXin Long }
6844ece4778SXin Long
6853d25eabbSwenxu #ifdef CONFIG_DST_CACHE
6863d25eabbSwenxu err = dst_cache_init(&tun_info->dst_cache, GFP_KERNEL);
6873d25eabbSwenxu if (err) {
6883d25eabbSwenxu lwtstate_free(new_state);
6893d25eabbSwenxu return err;
6903d25eabbSwenxu }
6913d25eabbSwenxu #endif
6923d25eabbSwenxu
693a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_ID])
69430d3d83aSLance Richardson tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
6953093fbe7SThomas Graf
696a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_DST])
6977822ce73SHaishuang Yan tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
6983093fbe7SThomas Graf
699a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_SRC])
7007822ce73SHaishuang Yan tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
7013093fbe7SThomas Graf
702a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TTL])
7037c383fb2SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
7043093fbe7SThomas Graf
705a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TOS])
7067c383fb2SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
7073093fbe7SThomas Graf
708a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_FLAGS])
7090c06d166SXin Long tun_info->key.tun_flags |=
7100c06d166SXin Long (nla_get_be16(tb[LWTUNNEL_IP_FLAGS]) &
7110c06d166SXin Long ~TUNNEL_OPTIONS_PRESENT);
7123093fbe7SThomas Graf
7133093fbe7SThomas Graf tun_info->mode = IP_TUNNEL_INFO_TX;
7144ece4778SXin Long tun_info->options_len = opt_len;
7153093fbe7SThomas Graf
7163093fbe7SThomas Graf *ts = new_state;
7173093fbe7SThomas Graf
7183093fbe7SThomas Graf return 0;
7193093fbe7SThomas Graf }
7203093fbe7SThomas Graf
ip_tun_destroy_state(struct lwtunnel_state * lwtstate)7213d25eabbSwenxu static void ip_tun_destroy_state(struct lwtunnel_state *lwtstate)
7223d25eabbSwenxu {
7233d25eabbSwenxu #ifdef CONFIG_DST_CACHE
7243d25eabbSwenxu struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
7253d25eabbSwenxu
7263d25eabbSwenxu dst_cache_destroy(&tun_info->dst_cache);
7273d25eabbSwenxu #endif
7283d25eabbSwenxu }
7293d25eabbSwenxu
ip_tun_fill_encap_opts_geneve(struct sk_buff * skb,struct ip_tunnel_info * tun_info)7304ece4778SXin Long static int ip_tun_fill_encap_opts_geneve(struct sk_buff *skb,
7314ece4778SXin Long struct ip_tunnel_info *tun_info)
7324ece4778SXin Long {
7334ece4778SXin Long struct geneve_opt *opt;
7344ece4778SXin Long struct nlattr *nest;
7352f1d370bSXin Long int offset = 0;
7364ece4778SXin Long
7374ece4778SXin Long nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_GENEVE);
7384ece4778SXin Long if (!nest)
7394ece4778SXin Long return -ENOMEM;
7404ece4778SXin Long
7412f1d370bSXin Long while (tun_info->options_len > offset) {
7422f1d370bSXin Long opt = ip_tunnel_info_opts(tun_info) + offset;
7432f1d370bSXin Long if (nla_put_be16(skb, LWTUNNEL_IP_OPT_GENEVE_CLASS,
7442f1d370bSXin Long opt->opt_class) ||
7454ece4778SXin Long nla_put_u8(skb, LWTUNNEL_IP_OPT_GENEVE_TYPE, opt->type) ||
7464ece4778SXin Long nla_put(skb, LWTUNNEL_IP_OPT_GENEVE_DATA, opt->length * 4,
7474ece4778SXin Long opt->opt_data)) {
7484ece4778SXin Long nla_nest_cancel(skb, nest);
7494ece4778SXin Long return -ENOMEM;
7504ece4778SXin Long }
7512f1d370bSXin Long offset += sizeof(*opt) + opt->length * 4;
7522f1d370bSXin Long }
7534ece4778SXin Long
7544ece4778SXin Long nla_nest_end(skb, nest);
7554ece4778SXin Long return 0;
7564ece4778SXin Long }
7574ece4778SXin Long
ip_tun_fill_encap_opts_vxlan(struct sk_buff * skb,struct ip_tunnel_info * tun_info)758edf31cbbSXin Long static int ip_tun_fill_encap_opts_vxlan(struct sk_buff *skb,
759edf31cbbSXin Long struct ip_tunnel_info *tun_info)
760edf31cbbSXin Long {
761edf31cbbSXin Long struct vxlan_metadata *md;
762edf31cbbSXin Long struct nlattr *nest;
763edf31cbbSXin Long
764edf31cbbSXin Long nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_VXLAN);
765edf31cbbSXin Long if (!nest)
766edf31cbbSXin Long return -ENOMEM;
767edf31cbbSXin Long
768edf31cbbSXin Long md = ip_tunnel_info_opts(tun_info);
769edf31cbbSXin Long if (nla_put_u32(skb, LWTUNNEL_IP_OPT_VXLAN_GBP, md->gbp)) {
770edf31cbbSXin Long nla_nest_cancel(skb, nest);
771edf31cbbSXin Long return -ENOMEM;
772edf31cbbSXin Long }
773edf31cbbSXin Long
774edf31cbbSXin Long nla_nest_end(skb, nest);
775edf31cbbSXin Long return 0;
776edf31cbbSXin Long }
777edf31cbbSXin Long
ip_tun_fill_encap_opts_erspan(struct sk_buff * skb,struct ip_tunnel_info * tun_info)778b0a21810SXin Long static int ip_tun_fill_encap_opts_erspan(struct sk_buff *skb,
779b0a21810SXin Long struct ip_tunnel_info *tun_info)
780b0a21810SXin Long {
781b0a21810SXin Long struct erspan_metadata *md;
782b0a21810SXin Long struct nlattr *nest;
783b0a21810SXin Long
784b0a21810SXin Long nest = nla_nest_start_noflag(skb, LWTUNNEL_IP_OPTS_ERSPAN);
785b0a21810SXin Long if (!nest)
786b0a21810SXin Long return -ENOMEM;
787b0a21810SXin Long
788b0a21810SXin Long md = ip_tunnel_info_opts(tun_info);
7893132174bSXin Long if (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_VER, md->version))
790b0a21810SXin Long goto err;
791b0a21810SXin Long
792b0a21810SXin Long if (md->version == 1 &&
793b0a21810SXin Long nla_put_be32(skb, LWTUNNEL_IP_OPT_ERSPAN_INDEX, md->u.index))
794b0a21810SXin Long goto err;
795b0a21810SXin Long
796b0a21810SXin Long if (md->version == 2 &&
797b0a21810SXin Long (nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_DIR, md->u.md2.dir) ||
798b0a21810SXin Long nla_put_u8(skb, LWTUNNEL_IP_OPT_ERSPAN_HWID,
799b0a21810SXin Long get_hwid(&md->u.md2))))
800b0a21810SXin Long goto err;
801b0a21810SXin Long
802b0a21810SXin Long nla_nest_end(skb, nest);
803b0a21810SXin Long return 0;
804b0a21810SXin Long err:
805b0a21810SXin Long nla_nest_cancel(skb, nest);
806b0a21810SXin Long return -ENOMEM;
807b0a21810SXin Long }
808b0a21810SXin Long
ip_tun_fill_encap_opts(struct sk_buff * skb,int type,struct ip_tunnel_info * tun_info)8094ece4778SXin Long static int ip_tun_fill_encap_opts(struct sk_buff *skb, int type,
8104ece4778SXin Long struct ip_tunnel_info *tun_info)
8114ece4778SXin Long {
8124ece4778SXin Long struct nlattr *nest;
8134ece4778SXin Long int err = 0;
8144ece4778SXin Long
8150c06d166SXin Long if (!(tun_info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
8164ece4778SXin Long return 0;
8174ece4778SXin Long
8184ece4778SXin Long nest = nla_nest_start_noflag(skb, type);
8194ece4778SXin Long if (!nest)
8204ece4778SXin Long return -ENOMEM;
8214ece4778SXin Long
8224ece4778SXin Long if (tun_info->key.tun_flags & TUNNEL_GENEVE_OPT)
8234ece4778SXin Long err = ip_tun_fill_encap_opts_geneve(skb, tun_info);
824edf31cbbSXin Long else if (tun_info->key.tun_flags & TUNNEL_VXLAN_OPT)
825edf31cbbSXin Long err = ip_tun_fill_encap_opts_vxlan(skb, tun_info);
826b0a21810SXin Long else if (tun_info->key.tun_flags & TUNNEL_ERSPAN_OPT)
827b0a21810SXin Long err = ip_tun_fill_encap_opts_erspan(skb, tun_info);
8284ece4778SXin Long
8294ece4778SXin Long if (err) {
8304ece4778SXin Long nla_nest_cancel(skb, nest);
8314ece4778SXin Long return err;
8324ece4778SXin Long }
8334ece4778SXin Long
8344ece4778SXin Long nla_nest_end(skb, nest);
8354ece4778SXin Long return 0;
8364ece4778SXin Long }
8374ece4778SXin Long
ip_tun_fill_encap_info(struct sk_buff * skb,struct lwtunnel_state * lwtstate)8383093fbe7SThomas Graf static int ip_tun_fill_encap_info(struct sk_buff *skb,
8393093fbe7SThomas Graf struct lwtunnel_state *lwtstate)
8403093fbe7SThomas Graf {
8413093fbe7SThomas Graf struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
8423093fbe7SThomas Graf
843b46f6dedSNicolas Dichtel if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
844b46f6dedSNicolas Dichtel LWTUNNEL_IP_PAD) ||
8457822ce73SHaishuang Yan nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
8467822ce73SHaishuang Yan nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
8477c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
8487c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
8494ece4778SXin Long nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags) ||
8504ece4778SXin Long ip_tun_fill_encap_opts(skb, LWTUNNEL_IP_OPTS, tun_info))
8513093fbe7SThomas Graf return -ENOMEM;
8523093fbe7SThomas Graf
8533093fbe7SThomas Graf return 0;
8543093fbe7SThomas Graf }
8553093fbe7SThomas Graf
ip_tun_opts_nlsize(struct ip_tunnel_info * info)8564ece4778SXin Long static int ip_tun_opts_nlsize(struct ip_tunnel_info *info)
8574ece4778SXin Long {
8584ece4778SXin Long int opt_len;
8594ece4778SXin Long
8600c06d166SXin Long if (!(info->key.tun_flags & TUNNEL_OPTIONS_PRESENT))
8614ece4778SXin Long return 0;
8624ece4778SXin Long
8634ece4778SXin Long opt_len = nla_total_size(0); /* LWTUNNEL_IP_OPTS */
8644ece4778SXin Long if (info->key.tun_flags & TUNNEL_GENEVE_OPT) {
8652f1d370bSXin Long struct geneve_opt *opt;
8662f1d370bSXin Long int offset = 0;
8674ece4778SXin Long
8682f1d370bSXin Long opt_len += nla_total_size(0); /* LWTUNNEL_IP_OPTS_GENEVE */
8692f1d370bSXin Long while (info->options_len > offset) {
8702f1d370bSXin Long opt = ip_tunnel_info_opts(info) + offset;
8712f1d370bSXin Long opt_len += nla_total_size(2) /* OPT_GENEVE_CLASS */
8724ece4778SXin Long + nla_total_size(1) /* OPT_GENEVE_TYPE */
8734ece4778SXin Long + nla_total_size(opt->length * 4);
8744ece4778SXin Long /* OPT_GENEVE_DATA */
8752f1d370bSXin Long offset += sizeof(*opt) + opt->length * 4;
8762f1d370bSXin Long }
877edf31cbbSXin Long } else if (info->key.tun_flags & TUNNEL_VXLAN_OPT) {
878edf31cbbSXin Long opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_VXLAN */
879edf31cbbSXin Long + nla_total_size(4); /* OPT_VXLAN_GBP */
880b0a21810SXin Long } else if (info->key.tun_flags & TUNNEL_ERSPAN_OPT) {
88158e8494eSXin Long struct erspan_metadata *md = ip_tunnel_info_opts(info);
88258e8494eSXin Long
883b0a21810SXin Long opt_len += nla_total_size(0) /* LWTUNNEL_IP_OPTS_ERSPAN */
884b0a21810SXin Long + nla_total_size(1) /* OPT_ERSPAN_VER */
88558e8494eSXin Long + (md->version == 1 ? nla_total_size(4)
88658e8494eSXin Long /* OPT_ERSPAN_INDEX (v1) */
88758e8494eSXin Long : nla_total_size(1) +
88858e8494eSXin Long nla_total_size(1));
88958e8494eSXin Long /* OPT_ERSPAN_DIR + HWID (v2) */
8904ece4778SXin Long }
8914ece4778SXin Long
8924ece4778SXin Long return opt_len;
8934ece4778SXin Long }
8944ece4778SXin Long
ip_tun_encap_nlsize(struct lwtunnel_state * lwtstate)8953093fbe7SThomas Graf static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
8963093fbe7SThomas Graf {
897b46f6dedSNicolas Dichtel return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
898a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_DST */
899a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_SRC */
900a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TOS */
901a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TTL */
9024ece4778SXin Long + nla_total_size(2) /* LWTUNNEL_IP_FLAGS */
9034ece4778SXin Long + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
9044ece4778SXin Long /* LWTUNNEL_IP_OPTS */
9053093fbe7SThomas Graf }
9063093fbe7SThomas Graf
ip_tun_cmp_encap(struct lwtunnel_state * a,struct lwtunnel_state * b)9072d798499SJiri Benc static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
9082d798499SJiri Benc {
9090eb8eb2fSXin Long struct ip_tunnel_info *info_a = lwt_tun_info(a);
9100eb8eb2fSXin Long struct ip_tunnel_info *info_b = lwt_tun_info(b);
9110eb8eb2fSXin Long
9120eb8eb2fSXin Long return memcmp(info_a, info_b, sizeof(info_a->key)) ||
9130eb8eb2fSXin Long info_a->mode != info_b->mode ||
9140eb8eb2fSXin Long info_a->options_len != info_b->options_len ||
9150eb8eb2fSXin Long memcmp(ip_tunnel_info_opts(info_a),
9160eb8eb2fSXin Long ip_tunnel_info_opts(info_b), info_a->options_len);
9172d798499SJiri Benc }
9182d798499SJiri Benc
9193093fbe7SThomas Graf static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
9203093fbe7SThomas Graf .build_state = ip_tun_build_state,
9213d25eabbSwenxu .destroy_state = ip_tun_destroy_state,
9223093fbe7SThomas Graf .fill_encap = ip_tun_fill_encap_info,
9233093fbe7SThomas Graf .get_encap_size = ip_tun_encap_nlsize,
9242d798499SJiri Benc .cmp_encap = ip_tun_cmp_encap,
92588ff7334SRobert Shearman .owner = THIS_MODULE,
9263093fbe7SThomas Graf };
9273093fbe7SThomas Graf
92832a2b002SJiri Benc static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
9297b6a70f7SXin Long [LWTUNNEL_IP6_UNSPEC] = { .strict_start_type = LWTUNNEL_IP6_OPTS },
93032a2b002SJiri Benc [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
93132a2b002SJiri Benc [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
93232a2b002SJiri Benc [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
93332a2b002SJiri Benc [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
93432a2b002SJiri Benc [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
93532a2b002SJiri Benc [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
9367b6a70f7SXin Long [LWTUNNEL_IP6_OPTS] = { .type = NLA_NESTED },
93732a2b002SJiri Benc };
93832a2b002SJiri Benc
ip6_tun_build_state(struct net * net,struct nlattr * attr,unsigned int family,const void * cfg,struct lwtunnel_state ** ts,struct netlink_ext_ack * extack)939faee6769SAlexander Aring static int ip6_tun_build_state(struct net *net, struct nlattr *attr,
940127eb7cdSTom Herbert unsigned int family, const void *cfg,
9419ae28727SDavid Ahern struct lwtunnel_state **ts,
9429ae28727SDavid Ahern struct netlink_ext_ack *extack)
94332a2b002SJiri Benc {
94432a2b002SJiri Benc struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
9454ece4778SXin Long struct lwtunnel_state *new_state;
9464ece4778SXin Long struct ip_tunnel_info *tun_info;
9474ece4778SXin Long int err, opt_len;
94832a2b002SJiri Benc
9498cb08174SJohannes Berg err = nla_parse_nested_deprecated(tb, LWTUNNEL_IP6_MAX, attr,
9508cb08174SJohannes Berg ip6_tun_policy, extack);
95132a2b002SJiri Benc if (err < 0)
95232a2b002SJiri Benc return err;
95332a2b002SJiri Benc
9544ece4778SXin Long opt_len = ip_tun_get_optlen(tb[LWTUNNEL_IP6_OPTS], extack);
9554ece4778SXin Long if (opt_len < 0)
9564ece4778SXin Long return opt_len;
9574ece4778SXin Long
9584ece4778SXin Long new_state = lwtunnel_state_alloc(sizeof(*tun_info) + opt_len);
95932a2b002SJiri Benc if (!new_state)
96032a2b002SJiri Benc return -ENOMEM;
96132a2b002SJiri Benc
96232a2b002SJiri Benc new_state->type = LWTUNNEL_ENCAP_IP6;
96332a2b002SJiri Benc
96432a2b002SJiri Benc tun_info = lwt_tun_info(new_state);
96532a2b002SJiri Benc
9664ece4778SXin Long err = ip_tun_set_opts(tb[LWTUNNEL_IP6_OPTS], tun_info, extack);
9674ece4778SXin Long if (err < 0) {
9684ece4778SXin Long lwtstate_free(new_state);
9694ece4778SXin Long return err;
9704ece4778SXin Long }
9714ece4778SXin Long
97232a2b002SJiri Benc if (tb[LWTUNNEL_IP6_ID])
97330d3d83aSLance Richardson tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
97432a2b002SJiri Benc
97532a2b002SJiri Benc if (tb[LWTUNNEL_IP6_DST])
97632a2b002SJiri Benc tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
97732a2b002SJiri Benc
97832a2b002SJiri Benc if (tb[LWTUNNEL_IP6_SRC])
97932a2b002SJiri Benc tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
98032a2b002SJiri Benc
98132a2b002SJiri Benc if (tb[LWTUNNEL_IP6_HOPLIMIT])
98232a2b002SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
98332a2b002SJiri Benc
98432a2b002SJiri Benc if (tb[LWTUNNEL_IP6_TC])
98532a2b002SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
98632a2b002SJiri Benc
98732a2b002SJiri Benc if (tb[LWTUNNEL_IP6_FLAGS])
9880c06d166SXin Long tun_info->key.tun_flags |=
9890c06d166SXin Long (nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]) &
9900c06d166SXin Long ~TUNNEL_OPTIONS_PRESENT);
99132a2b002SJiri Benc
9927f9562a1SJiri Benc tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
9934ece4778SXin Long tun_info->options_len = opt_len;
99432a2b002SJiri Benc
99532a2b002SJiri Benc *ts = new_state;
99632a2b002SJiri Benc
99732a2b002SJiri Benc return 0;
99832a2b002SJiri Benc }
99932a2b002SJiri Benc
ip6_tun_fill_encap_info(struct sk_buff * skb,struct lwtunnel_state * lwtstate)100032a2b002SJiri Benc static int ip6_tun_fill_encap_info(struct sk_buff *skb,
100132a2b002SJiri Benc struct lwtunnel_state *lwtstate)
100232a2b002SJiri Benc {
100332a2b002SJiri Benc struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
100432a2b002SJiri Benc
1005b46f6dedSNicolas Dichtel if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
1006b46f6dedSNicolas Dichtel LWTUNNEL_IP6_PAD) ||
100732a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
100832a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
1009995096a0SQuentin Armitage nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
1010995096a0SQuentin Armitage nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
10114ece4778SXin Long nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags) ||
10124ece4778SXin Long ip_tun_fill_encap_opts(skb, LWTUNNEL_IP6_OPTS, tun_info))
101332a2b002SJiri Benc return -ENOMEM;
101432a2b002SJiri Benc
101532a2b002SJiri Benc return 0;
101632a2b002SJiri Benc }
101732a2b002SJiri Benc
ip6_tun_encap_nlsize(struct lwtunnel_state * lwtstate)101832a2b002SJiri Benc static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
101932a2b002SJiri Benc {
1020b46f6dedSNicolas Dichtel return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
102132a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_DST */
102232a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
102332a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
102432a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_TC */
10254ece4778SXin Long + nla_total_size(2) /* LWTUNNEL_IP6_FLAGS */
10264ece4778SXin Long + ip_tun_opts_nlsize(lwt_tun_info(lwtstate));
10274ece4778SXin Long /* LWTUNNEL_IP6_OPTS */
102832a2b002SJiri Benc }
102932a2b002SJiri Benc
103032a2b002SJiri Benc static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
103132a2b002SJiri Benc .build_state = ip6_tun_build_state,
103232a2b002SJiri Benc .fill_encap = ip6_tun_fill_encap_info,
103332a2b002SJiri Benc .get_encap_size = ip6_tun_encap_nlsize,
103432a2b002SJiri Benc .cmp_encap = ip_tun_cmp_encap,
103588ff7334SRobert Shearman .owner = THIS_MODULE,
103632a2b002SJiri Benc };
103732a2b002SJiri Benc
ip_tunnel_core_init(void)1038045a0fa0SThomas Graf void __init ip_tunnel_core_init(void)
10393093fbe7SThomas Graf {
1040fca5fdf6SDaniel Borkmann /* If you land here, make sure whether increasing ip_tunnel_info's
1041fca5fdf6SDaniel Borkmann * options_len is a reasonable choice with its usage in front ends
1042fca5fdf6SDaniel Borkmann * (f.e., it's part of flow keys, etc).
1043fca5fdf6SDaniel Borkmann */
1044fca5fdf6SDaniel Borkmann BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
1045fca5fdf6SDaniel Borkmann
10463093fbe7SThomas Graf lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
104732a2b002SJiri Benc lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
10483093fbe7SThomas Graf }
1049e7030878SThomas Graf
10505263a98fSDavidlohr Bueso DEFINE_STATIC_KEY_FALSE(ip_tunnel_metadata_cnt);
1051e7030878SThomas Graf EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
1052e7030878SThomas Graf
ip_tunnel_need_metadata(void)1053e7030878SThomas Graf void ip_tunnel_need_metadata(void)
1054e7030878SThomas Graf {
10555263a98fSDavidlohr Bueso static_branch_inc(&ip_tunnel_metadata_cnt);
1056e7030878SThomas Graf }
1057e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
1058e7030878SThomas Graf
ip_tunnel_unneed_metadata(void)1059e7030878SThomas Graf void ip_tunnel_unneed_metadata(void)
1060e7030878SThomas Graf {
10615263a98fSDavidlohr Bueso static_branch_dec(&ip_tunnel_metadata_cnt);
1062e7030878SThomas Graf }
1063e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);
10642606aff9SJason A. Donenfeld
10652606aff9SJason A. Donenfeld /* Returns either the correct skb->protocol value, or 0 if invalid. */
ip_tunnel_parse_protocol(const struct sk_buff * skb)10662606aff9SJason A. Donenfeld __be16 ip_tunnel_parse_protocol(const struct sk_buff *skb)
10672606aff9SJason A. Donenfeld {
10682606aff9SJason A. Donenfeld if (skb_network_header(skb) >= skb->head &&
10692606aff9SJason A. Donenfeld (skb_network_header(skb) + sizeof(struct iphdr)) <= skb_tail_pointer(skb) &&
10702606aff9SJason A. Donenfeld ip_hdr(skb)->version == 4)
10712606aff9SJason A. Donenfeld return htons(ETH_P_IP);
10722606aff9SJason A. Donenfeld if (skb_network_header(skb) >= skb->head &&
10732606aff9SJason A. Donenfeld (skb_network_header(skb) + sizeof(struct ipv6hdr)) <= skb_tail_pointer(skb) &&
10742606aff9SJason A. Donenfeld ipv6_hdr(skb)->version == 6)
10752606aff9SJason A. Donenfeld return htons(ETH_P_IPV6);
10762606aff9SJason A. Donenfeld return 0;
10772606aff9SJason A. Donenfeld }
10782606aff9SJason A. Donenfeld EXPORT_SYMBOL(ip_tunnel_parse_protocol);
10792606aff9SJason A. Donenfeld
10802606aff9SJason A. Donenfeld const struct header_ops ip_tunnel_header_ops = { .parse_protocol = ip_tunnel_parse_protocol };
10812606aff9SJason A. Donenfeld EXPORT_SYMBOL(ip_tunnel_header_ops);
1082537dd2d9SLiu Jian
1083537dd2d9SLiu Jian /* This function returns true when ENCAP attributes are present in the nl msg */
ip_tunnel_netlink_encap_parms(struct nlattr * data[],struct ip_tunnel_encap * encap)1084537dd2d9SLiu Jian bool ip_tunnel_netlink_encap_parms(struct nlattr *data[],
1085537dd2d9SLiu Jian struct ip_tunnel_encap *encap)
1086537dd2d9SLiu Jian {
1087537dd2d9SLiu Jian bool ret = false;
1088537dd2d9SLiu Jian
1089537dd2d9SLiu Jian memset(encap, 0, sizeof(*encap));
1090537dd2d9SLiu Jian
1091537dd2d9SLiu Jian if (!data)
1092537dd2d9SLiu Jian return ret;
1093537dd2d9SLiu Jian
1094537dd2d9SLiu Jian if (data[IFLA_IPTUN_ENCAP_TYPE]) {
1095537dd2d9SLiu Jian ret = true;
1096537dd2d9SLiu Jian encap->type = nla_get_u16(data[IFLA_IPTUN_ENCAP_TYPE]);
1097537dd2d9SLiu Jian }
1098537dd2d9SLiu Jian
1099537dd2d9SLiu Jian if (data[IFLA_IPTUN_ENCAP_FLAGS]) {
1100537dd2d9SLiu Jian ret = true;
1101537dd2d9SLiu Jian encap->flags = nla_get_u16(data[IFLA_IPTUN_ENCAP_FLAGS]);
1102537dd2d9SLiu Jian }
1103537dd2d9SLiu Jian
1104537dd2d9SLiu Jian if (data[IFLA_IPTUN_ENCAP_SPORT]) {
1105537dd2d9SLiu Jian ret = true;
1106537dd2d9SLiu Jian encap->sport = nla_get_be16(data[IFLA_IPTUN_ENCAP_SPORT]);
1107537dd2d9SLiu Jian }
1108537dd2d9SLiu Jian
1109537dd2d9SLiu Jian if (data[IFLA_IPTUN_ENCAP_DPORT]) {
1110537dd2d9SLiu Jian ret = true;
1111537dd2d9SLiu Jian encap->dport = nla_get_be16(data[IFLA_IPTUN_ENCAP_DPORT]);
1112537dd2d9SLiu Jian }
1113537dd2d9SLiu Jian
1114537dd2d9SLiu Jian return ret;
1115537dd2d9SLiu Jian }
1116537dd2d9SLiu Jian EXPORT_SYMBOL_GPL(ip_tunnel_netlink_encap_parms);
1117b86fca80SLiu Jian
ip_tunnel_netlink_parms(struct nlattr * data[],struct ip_tunnel_parm * parms)1118b86fca80SLiu Jian void ip_tunnel_netlink_parms(struct nlattr *data[],
1119b86fca80SLiu Jian struct ip_tunnel_parm *parms)
1120b86fca80SLiu Jian {
1121b86fca80SLiu Jian if (data[IFLA_IPTUN_LINK])
1122b86fca80SLiu Jian parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1123b86fca80SLiu Jian
1124b86fca80SLiu Jian if (data[IFLA_IPTUN_LOCAL])
1125b86fca80SLiu Jian parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]);
1126b86fca80SLiu Jian
1127b86fca80SLiu Jian if (data[IFLA_IPTUN_REMOTE])
1128b86fca80SLiu Jian parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]);
1129b86fca80SLiu Jian
1130b86fca80SLiu Jian if (data[IFLA_IPTUN_TTL]) {
1131b86fca80SLiu Jian parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
1132b86fca80SLiu Jian if (parms->iph.ttl)
1133b86fca80SLiu Jian parms->iph.frag_off = htons(IP_DF);
1134b86fca80SLiu Jian }
1135b86fca80SLiu Jian
1136b86fca80SLiu Jian if (data[IFLA_IPTUN_TOS])
1137b86fca80SLiu Jian parms->iph.tos = nla_get_u8(data[IFLA_IPTUN_TOS]);
1138b86fca80SLiu Jian
1139b86fca80SLiu Jian if (!data[IFLA_IPTUN_PMTUDISC] || nla_get_u8(data[IFLA_IPTUN_PMTUDISC]))
1140b86fca80SLiu Jian parms->iph.frag_off = htons(IP_DF);
1141b86fca80SLiu Jian
1142b86fca80SLiu Jian if (data[IFLA_IPTUN_FLAGS])
1143b86fca80SLiu Jian parms->i_flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);
1144b86fca80SLiu Jian
1145b86fca80SLiu Jian if (data[IFLA_IPTUN_PROTO])
1146b86fca80SLiu Jian parms->iph.protocol = nla_get_u8(data[IFLA_IPTUN_PROTO]);
1147b86fca80SLiu Jian }
1148b86fca80SLiu Jian EXPORT_SYMBOL_GPL(ip_tunnel_netlink_parms);
1149