10e6fbc5bSPravin B Shelar /* 20e6fbc5bSPravin B Shelar * Copyright (c) 2013 Nicira, Inc. 30e6fbc5bSPravin B Shelar * 40e6fbc5bSPravin B Shelar * This program is free software; you can redistribute it and/or 50e6fbc5bSPravin B Shelar * modify it under the terms of version 2 of the GNU General Public 60e6fbc5bSPravin B Shelar * License as published by the Free Software Foundation. 70e6fbc5bSPravin B Shelar * 80e6fbc5bSPravin B Shelar * This program is distributed in the hope that it will be useful, but 90e6fbc5bSPravin B Shelar * WITHOUT ANY WARRANTY; without even the implied warranty of 100e6fbc5bSPravin B Shelar * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 110e6fbc5bSPravin B Shelar * General Public License for more details. 120e6fbc5bSPravin B Shelar * 130e6fbc5bSPravin B Shelar * You should have received a copy of the GNU General Public License 140e6fbc5bSPravin B Shelar * along with this program; if not, write to the Free Software 150e6fbc5bSPravin B Shelar * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 160e6fbc5bSPravin B Shelar * 02110-1301, USA 170e6fbc5bSPravin B Shelar */ 180e6fbc5bSPravin B Shelar 190e6fbc5bSPravin B Shelar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 200e6fbc5bSPravin B Shelar 210e6fbc5bSPravin B Shelar #include <linux/types.h> 220e6fbc5bSPravin B Shelar #include <linux/kernel.h> 230e6fbc5bSPravin B Shelar #include <linux/skbuff.h> 240e6fbc5bSPravin B Shelar #include <linux/netdevice.h> 250e6fbc5bSPravin B Shelar #include <linux/in.h> 260e6fbc5bSPravin B Shelar #include <linux/if_arp.h> 270e6fbc5bSPravin B Shelar #include <linux/init.h> 280e6fbc5bSPravin B Shelar #include <linux/in6.h> 290e6fbc5bSPravin B Shelar #include <linux/inetdevice.h> 300e6fbc5bSPravin B Shelar #include <linux/netfilter_ipv4.h> 310e6fbc5bSPravin B Shelar #include <linux/etherdevice.h> 320e6fbc5bSPravin B Shelar #include <linux/if_ether.h> 330e6fbc5bSPravin B Shelar #include <linux/if_vlan.h> 34e7030878SThomas Graf #include <linux/static_key.h> 350e6fbc5bSPravin B Shelar 360e6fbc5bSPravin B Shelar #include <net/ip.h> 370e6fbc5bSPravin B Shelar #include <net/icmp.h> 380e6fbc5bSPravin B Shelar #include <net/protocol.h> 390e6fbc5bSPravin B Shelar #include <net/ip_tunnels.h> 400e6fbc5bSPravin B Shelar #include <net/arp.h> 410e6fbc5bSPravin B Shelar #include <net/checksum.h> 420e6fbc5bSPravin B Shelar #include <net/dsfield.h> 430e6fbc5bSPravin B Shelar #include <net/inet_ecn.h> 440e6fbc5bSPravin B Shelar #include <net/xfrm.h> 450e6fbc5bSPravin B Shelar #include <net/net_namespace.h> 460e6fbc5bSPravin B Shelar #include <net/netns/generic.h> 470e6fbc5bSPravin B Shelar #include <net/rtnetlink.h> 4863d008a4SJiri Benc #include <net/dst_metadata.h> 490e6fbc5bSPravin B Shelar 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 627539fadcSTom Herbert skb_clear_hash(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; 740e6fbc5bSPravin B Shelar iph->frag_off = 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); 830e6fbc5bSPravin B Shelar if (unlikely(net_xmit_eval(err))) 840e6fbc5bSPravin B Shelar pkt_len = 0; 85039f5062SPravin B Shelar iptunnel_xmit_stats(dev, pkt_len); 860e6fbc5bSPravin B Shelar } 870e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit); 883d7b46cdSPravin B Shelar 897f290c94SJiri Benc int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto, 907f290c94SJiri Benc bool xnet) 913d7b46cdSPravin B Shelar { 923d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, hdr_len))) 933d7b46cdSPravin B Shelar return -ENOMEM; 943d7b46cdSPravin B Shelar 953d7b46cdSPravin B Shelar skb_pull_rcsum(skb, hdr_len); 963d7b46cdSPravin B Shelar 973d7b46cdSPravin B Shelar if (inner_proto == htons(ETH_P_TEB)) { 981245dfc8SLi RongQing struct ethhdr *eh; 993d7b46cdSPravin B Shelar 1003d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 1013d7b46cdSPravin B Shelar return -ENOMEM; 1023d7b46cdSPravin B Shelar 1031245dfc8SLi RongQing eh = (struct ethhdr *)skb->data; 104d181ddcaSAlexander Duyck if (likely(eth_proto_is_802_3(eh->h_proto))) 1053d7b46cdSPravin B Shelar skb->protocol = eh->h_proto; 1063d7b46cdSPravin B Shelar else 1073d7b46cdSPravin B Shelar skb->protocol = htons(ETH_P_802_2); 1083d7b46cdSPravin B Shelar 1093d7b46cdSPravin B Shelar } else { 1103d7b46cdSPravin B Shelar skb->protocol = inner_proto; 1113d7b46cdSPravin B Shelar } 1123d7b46cdSPravin B Shelar 1137539fadcSTom Herbert skb_clear_hash_if_not_l4(skb); 1143d7b46cdSPravin B Shelar skb->vlan_tci = 0; 1153d7b46cdSPravin B Shelar skb_set_queue_mapping(skb, 0); 1167f290c94SJiri Benc skb_scrub_packet(skb, xnet); 117a09a4c8dSJesse Gross 118a09a4c8dSJesse Gross return iptunnel_pull_offloads(skb); 1193d7b46cdSPravin B Shelar } 1203d7b46cdSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_pull_header); 1212d26f0a3SEric Dumazet 12263d008a4SJiri Benc struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md, 12363d008a4SJiri Benc gfp_t flags) 12463d008a4SJiri Benc { 12563d008a4SJiri Benc struct metadata_dst *res; 12663d008a4SJiri Benc struct ip_tunnel_info *dst, *src; 12763d008a4SJiri Benc 12863d008a4SJiri Benc if (!md || md->u.tun_info.mode & IP_TUNNEL_INFO_TX) 12963d008a4SJiri Benc return NULL; 13063d008a4SJiri Benc 13163d008a4SJiri Benc res = metadata_dst_alloc(0, flags); 13263d008a4SJiri Benc if (!res) 13363d008a4SJiri Benc return NULL; 13463d008a4SJiri Benc 13563d008a4SJiri Benc dst = &res->u.tun_info; 13663d008a4SJiri Benc src = &md->u.tun_info; 13763d008a4SJiri Benc dst->key.tun_id = src->key.tun_id; 13863d008a4SJiri Benc if (src->mode & IP_TUNNEL_INFO_IPV6) 13963d008a4SJiri Benc memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src, 14063d008a4SJiri Benc sizeof(struct in6_addr)); 14163d008a4SJiri Benc else 14263d008a4SJiri Benc dst->key.u.ipv4.dst = src->key.u.ipv4.src; 14363d008a4SJiri Benc dst->mode = src->mode | IP_TUNNEL_INFO_TX; 14463d008a4SJiri Benc 14563d008a4SJiri Benc return res; 14663d008a4SJiri Benc } 14763d008a4SJiri Benc EXPORT_SYMBOL_GPL(iptunnel_metadata_reply); 14863d008a4SJiri Benc 1492d26f0a3SEric Dumazet struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, 1502d26f0a3SEric Dumazet int gso_type_mask) 1512d26f0a3SEric Dumazet { 1522d26f0a3SEric Dumazet int err; 1532d26f0a3SEric Dumazet 1542d26f0a3SEric Dumazet if (likely(!skb->encapsulation)) { 1552d26f0a3SEric Dumazet skb_reset_inner_headers(skb); 1562d26f0a3SEric Dumazet skb->encapsulation = 1; 1572d26f0a3SEric Dumazet } 1582d26f0a3SEric Dumazet 1592d26f0a3SEric Dumazet if (skb_is_gso(skb)) { 1602d26f0a3SEric Dumazet err = skb_unclone(skb, GFP_ATOMIC); 1612d26f0a3SEric Dumazet if (unlikely(err)) 1622d26f0a3SEric Dumazet goto error; 1632d26f0a3SEric Dumazet skb_shinfo(skb)->gso_type |= gso_type_mask; 1642d26f0a3SEric Dumazet return skb; 1652d26f0a3SEric Dumazet } 1662d26f0a3SEric Dumazet 1676fa79666SEdward Cree if (skb->ip_summed != CHECKSUM_PARTIAL) { 1682d26f0a3SEric Dumazet skb->ip_summed = CHECKSUM_NONE; 1696fa79666SEdward Cree /* We clear encapsulation here to prevent badly-written 1706fa79666SEdward Cree * drivers potentially deciding to offload an inner checksum 1716fa79666SEdward Cree * if we set CHECKSUM_PARTIAL on the outer header. 1726fa79666SEdward Cree * This should go away when the drivers are all fixed. 1736fa79666SEdward Cree */ 174179bc67fSEdward Cree skb->encapsulation = 0; 175179bc67fSEdward Cree } 1762d26f0a3SEric Dumazet 1772d26f0a3SEric Dumazet return skb; 1782d26f0a3SEric Dumazet error: 1792d26f0a3SEric Dumazet kfree_skb(skb); 1802d26f0a3SEric Dumazet return ERR_PTR(err); 1812d26f0a3SEric Dumazet } 1822d26f0a3SEric Dumazet EXPORT_SYMBOL_GPL(iptunnel_handle_offloads); 183ebe44f35SDavid S. Miller 184ebe44f35SDavid S. Miller /* Often modified stats are per cpu, other are shared (netdev->stats) */ 185ebe44f35SDavid S. Miller struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev, 186ebe44f35SDavid S. Miller struct rtnl_link_stats64 *tot) 187ebe44f35SDavid S. Miller { 188ebe44f35SDavid S. Miller int i; 189ebe44f35SDavid S. Miller 190c24a5964SAlexander Duyck netdev_stats_to_stats64(tot, &dev->stats); 191c24a5964SAlexander Duyck 192ebe44f35SDavid S. Miller for_each_possible_cpu(i) { 193ebe44f35SDavid S. Miller const struct pcpu_sw_netstats *tstats = 194ebe44f35SDavid S. Miller per_cpu_ptr(dev->tstats, i); 195ebe44f35SDavid S. Miller u64 rx_packets, rx_bytes, tx_packets, tx_bytes; 196ebe44f35SDavid S. Miller unsigned int start; 197ebe44f35SDavid S. Miller 198ebe44f35SDavid S. Miller do { 19957a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&tstats->syncp); 200ebe44f35SDavid S. Miller rx_packets = tstats->rx_packets; 201ebe44f35SDavid S. Miller tx_packets = tstats->tx_packets; 202ebe44f35SDavid S. Miller rx_bytes = tstats->rx_bytes; 203ebe44f35SDavid S. Miller tx_bytes = tstats->tx_bytes; 20457a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); 205ebe44f35SDavid S. Miller 206ebe44f35SDavid S. Miller tot->rx_packets += rx_packets; 207ebe44f35SDavid S. Miller tot->tx_packets += tx_packets; 208ebe44f35SDavid S. Miller tot->rx_bytes += rx_bytes; 209ebe44f35SDavid S. Miller tot->tx_bytes += tx_bytes; 210ebe44f35SDavid S. Miller } 211ebe44f35SDavid S. Miller 212ebe44f35SDavid S. Miller return tot; 213ebe44f35SDavid S. Miller } 214ebe44f35SDavid S. Miller EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64); 2153093fbe7SThomas Graf 216a1c234f9SJiri Benc static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { 217a1c234f9SJiri Benc [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, 218a1c234f9SJiri Benc [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, 219a1c234f9SJiri Benc [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, 220a1c234f9SJiri Benc [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, 221a1c234f9SJiri Benc [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, 222a1c234f9SJiri Benc [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, 2233093fbe7SThomas Graf }; 2243093fbe7SThomas Graf 2253093fbe7SThomas Graf static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr, 226127eb7cdSTom Herbert unsigned int family, const void *cfg, 2273093fbe7SThomas Graf struct lwtunnel_state **ts) 2283093fbe7SThomas Graf { 2293093fbe7SThomas Graf struct ip_tunnel_info *tun_info; 2303093fbe7SThomas Graf struct lwtunnel_state *new_state; 231a1c234f9SJiri Benc struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; 2323093fbe7SThomas Graf int err; 2333093fbe7SThomas Graf 234a1c234f9SJiri Benc err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy); 2353093fbe7SThomas Graf if (err < 0) 2363093fbe7SThomas Graf return err; 2373093fbe7SThomas Graf 2383093fbe7SThomas Graf new_state = lwtunnel_state_alloc(sizeof(*tun_info)); 2393093fbe7SThomas Graf if (!new_state) 2403093fbe7SThomas Graf return -ENOMEM; 2413093fbe7SThomas Graf 2423093fbe7SThomas Graf new_state->type = LWTUNNEL_ENCAP_IP; 2433093fbe7SThomas Graf 2443093fbe7SThomas Graf tun_info = lwt_tun_info(new_state); 2453093fbe7SThomas Graf 246a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_ID]) 24730d3d83aSLance Richardson tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]); 2483093fbe7SThomas Graf 249a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_DST]) 250c1ea5d67SJiri Benc tun_info->key.u.ipv4.dst = nla_get_be32(tb[LWTUNNEL_IP_DST]); 2513093fbe7SThomas Graf 252a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_SRC]) 253c1ea5d67SJiri Benc tun_info->key.u.ipv4.src = nla_get_be32(tb[LWTUNNEL_IP_SRC]); 2543093fbe7SThomas Graf 255a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TTL]) 2567c383fb2SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); 2573093fbe7SThomas Graf 258a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TOS]) 2597c383fb2SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); 2603093fbe7SThomas Graf 261a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_FLAGS]) 26230d3d83aSLance Richardson tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP_FLAGS]); 2633093fbe7SThomas Graf 2643093fbe7SThomas Graf tun_info->mode = IP_TUNNEL_INFO_TX; 2653093fbe7SThomas Graf tun_info->options_len = 0; 2663093fbe7SThomas Graf 2673093fbe7SThomas Graf *ts = new_state; 2683093fbe7SThomas Graf 2693093fbe7SThomas Graf return 0; 2703093fbe7SThomas Graf } 2713093fbe7SThomas Graf 2723093fbe7SThomas Graf static int ip_tun_fill_encap_info(struct sk_buff *skb, 2733093fbe7SThomas Graf struct lwtunnel_state *lwtstate) 2743093fbe7SThomas Graf { 2753093fbe7SThomas Graf struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 2763093fbe7SThomas Graf 27730d3d83aSLance Richardson if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id) || 278c1ea5d67SJiri Benc nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || 279c1ea5d67SJiri Benc nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || 2807c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || 2817c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || 28230d3d83aSLance Richardson nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags)) 2833093fbe7SThomas Graf return -ENOMEM; 2843093fbe7SThomas Graf 2853093fbe7SThomas Graf return 0; 2863093fbe7SThomas Graf } 2873093fbe7SThomas Graf 2883093fbe7SThomas Graf static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 2893093fbe7SThomas Graf { 290a1c234f9SJiri Benc return nla_total_size(8) /* LWTUNNEL_IP_ID */ 291a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_DST */ 292a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_SRC */ 293a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TOS */ 294a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TTL */ 295a1c234f9SJiri Benc + nla_total_size(2); /* LWTUNNEL_IP_FLAGS */ 2963093fbe7SThomas Graf } 2973093fbe7SThomas Graf 2982d798499SJiri Benc static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) 2992d798499SJiri Benc { 3002d798499SJiri Benc return memcmp(lwt_tun_info(a), lwt_tun_info(b), 3012d798499SJiri Benc sizeof(struct ip_tunnel_info)); 3022d798499SJiri Benc } 3032d798499SJiri Benc 3043093fbe7SThomas Graf static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { 3053093fbe7SThomas Graf .build_state = ip_tun_build_state, 3063093fbe7SThomas Graf .fill_encap = ip_tun_fill_encap_info, 3073093fbe7SThomas Graf .get_encap_size = ip_tun_encap_nlsize, 3082d798499SJiri Benc .cmp_encap = ip_tun_cmp_encap, 3093093fbe7SThomas Graf }; 3103093fbe7SThomas Graf 31132a2b002SJiri Benc static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { 31232a2b002SJiri Benc [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, 31332a2b002SJiri Benc [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, 31432a2b002SJiri Benc [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, 31532a2b002SJiri Benc [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, 31632a2b002SJiri Benc [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, 31732a2b002SJiri Benc [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, 31832a2b002SJiri Benc }; 31932a2b002SJiri Benc 32032a2b002SJiri Benc static int ip6_tun_build_state(struct net_device *dev, struct nlattr *attr, 321127eb7cdSTom Herbert unsigned int family, const void *cfg, 32232a2b002SJiri Benc struct lwtunnel_state **ts) 32332a2b002SJiri Benc { 32432a2b002SJiri Benc struct ip_tunnel_info *tun_info; 32532a2b002SJiri Benc struct lwtunnel_state *new_state; 32632a2b002SJiri Benc struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; 32732a2b002SJiri Benc int err; 32832a2b002SJiri Benc 32932a2b002SJiri Benc err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy); 33032a2b002SJiri Benc if (err < 0) 33132a2b002SJiri Benc return err; 33232a2b002SJiri Benc 33332a2b002SJiri Benc new_state = lwtunnel_state_alloc(sizeof(*tun_info)); 33432a2b002SJiri Benc if (!new_state) 33532a2b002SJiri Benc return -ENOMEM; 33632a2b002SJiri Benc 33732a2b002SJiri Benc new_state->type = LWTUNNEL_ENCAP_IP6; 33832a2b002SJiri Benc 33932a2b002SJiri Benc tun_info = lwt_tun_info(new_state); 34032a2b002SJiri Benc 34132a2b002SJiri Benc if (tb[LWTUNNEL_IP6_ID]) 34230d3d83aSLance Richardson tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]); 34332a2b002SJiri Benc 34432a2b002SJiri Benc if (tb[LWTUNNEL_IP6_DST]) 34532a2b002SJiri Benc tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); 34632a2b002SJiri Benc 34732a2b002SJiri Benc if (tb[LWTUNNEL_IP6_SRC]) 34832a2b002SJiri Benc tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); 34932a2b002SJiri Benc 35032a2b002SJiri Benc if (tb[LWTUNNEL_IP6_HOPLIMIT]) 35132a2b002SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); 35232a2b002SJiri Benc 35332a2b002SJiri Benc if (tb[LWTUNNEL_IP6_TC]) 35432a2b002SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); 35532a2b002SJiri Benc 35632a2b002SJiri Benc if (tb[LWTUNNEL_IP6_FLAGS]) 35730d3d83aSLance Richardson tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]); 35832a2b002SJiri Benc 3597f9562a1SJiri Benc tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6; 36032a2b002SJiri Benc tun_info->options_len = 0; 36132a2b002SJiri Benc 36232a2b002SJiri Benc *ts = new_state; 36332a2b002SJiri Benc 36432a2b002SJiri Benc return 0; 36532a2b002SJiri Benc } 36632a2b002SJiri Benc 36732a2b002SJiri Benc static int ip6_tun_fill_encap_info(struct sk_buff *skb, 36832a2b002SJiri Benc struct lwtunnel_state *lwtstate) 36932a2b002SJiri Benc { 37032a2b002SJiri Benc struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 37132a2b002SJiri Benc 37230d3d83aSLance Richardson if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id) || 37332a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || 37432a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || 375995096a0SQuentin Armitage nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) || 376995096a0SQuentin Armitage nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) || 37730d3d83aSLance Richardson nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags)) 37832a2b002SJiri Benc return -ENOMEM; 37932a2b002SJiri Benc 38032a2b002SJiri Benc return 0; 38132a2b002SJiri Benc } 38232a2b002SJiri Benc 38332a2b002SJiri Benc static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 38432a2b002SJiri Benc { 38532a2b002SJiri Benc return nla_total_size(8) /* LWTUNNEL_IP6_ID */ 38632a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_DST */ 38732a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ 38832a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ 38932a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_TC */ 39032a2b002SJiri Benc + nla_total_size(2); /* LWTUNNEL_IP6_FLAGS */ 39132a2b002SJiri Benc } 39232a2b002SJiri Benc 39332a2b002SJiri Benc static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { 39432a2b002SJiri Benc .build_state = ip6_tun_build_state, 39532a2b002SJiri Benc .fill_encap = ip6_tun_fill_encap_info, 39632a2b002SJiri Benc .get_encap_size = ip6_tun_encap_nlsize, 39732a2b002SJiri Benc .cmp_encap = ip_tun_cmp_encap, 39832a2b002SJiri Benc }; 39932a2b002SJiri Benc 400045a0fa0SThomas Graf void __init ip_tunnel_core_init(void) 4013093fbe7SThomas Graf { 402fca5fdf6SDaniel Borkmann /* If you land here, make sure whether increasing ip_tunnel_info's 403fca5fdf6SDaniel Borkmann * options_len is a reasonable choice with its usage in front ends 404fca5fdf6SDaniel Borkmann * (f.e., it's part of flow keys, etc). 405fca5fdf6SDaniel Borkmann */ 406fca5fdf6SDaniel Borkmann BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255); 407fca5fdf6SDaniel Borkmann 4083093fbe7SThomas Graf lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); 40932a2b002SJiri Benc lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); 4103093fbe7SThomas Graf } 411e7030878SThomas Graf 412e7030878SThomas Graf struct static_key ip_tunnel_metadata_cnt = STATIC_KEY_INIT_FALSE; 413e7030878SThomas Graf EXPORT_SYMBOL(ip_tunnel_metadata_cnt); 414e7030878SThomas Graf 415e7030878SThomas Graf void ip_tunnel_need_metadata(void) 416e7030878SThomas Graf { 417e7030878SThomas Graf static_key_slow_inc(&ip_tunnel_metadata_cnt); 418e7030878SThomas Graf } 419e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); 420e7030878SThomas Graf 421e7030878SThomas Graf void ip_tunnel_unneed_metadata(void) 422e7030878SThomas Graf { 423e7030878SThomas Graf static_key_slow_dec(&ip_tunnel_metadata_cnt); 424e7030878SThomas Graf } 425e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); 426