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/mroute.h> 280e6fbc5bSPravin B Shelar #include <linux/init.h> 290e6fbc5bSPravin B Shelar #include <linux/in6.h> 300e6fbc5bSPravin B Shelar #include <linux/inetdevice.h> 310e6fbc5bSPravin B Shelar #include <linux/netfilter_ipv4.h> 320e6fbc5bSPravin B Shelar #include <linux/etherdevice.h> 330e6fbc5bSPravin B Shelar #include <linux/if_ether.h> 340e6fbc5bSPravin B Shelar #include <linux/if_vlan.h> 35e7030878SThomas Graf #include <linux/static_key.h> 360e6fbc5bSPravin B Shelar 370e6fbc5bSPravin B Shelar #include <net/ip.h> 380e6fbc5bSPravin B Shelar #include <net/icmp.h> 390e6fbc5bSPravin B Shelar #include <net/protocol.h> 400e6fbc5bSPravin B Shelar #include <net/ip_tunnels.h> 410e6fbc5bSPravin B Shelar #include <net/arp.h> 420e6fbc5bSPravin B Shelar #include <net/checksum.h> 430e6fbc5bSPravin B Shelar #include <net/dsfield.h> 440e6fbc5bSPravin B Shelar #include <net/inet_ecn.h> 450e6fbc5bSPravin B Shelar #include <net/xfrm.h> 460e6fbc5bSPravin B Shelar #include <net/net_namespace.h> 470e6fbc5bSPravin B Shelar #include <net/netns/generic.h> 480e6fbc5bSPravin B Shelar #include <net/rtnetlink.h> 490e6fbc5bSPravin B Shelar 50aad88724SEric Dumazet int 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 { 540e6fbc5bSPravin B Shelar int pkt_len = skb->len; 550e6fbc5bSPravin B Shelar struct iphdr *iph; 560e6fbc5bSPravin B Shelar int err; 570e6fbc5bSPravin B Shelar 58963a88b3SNicolas Dichtel skb_scrub_packet(skb, xnet); 59963a88b3SNicolas Dichtel 607539fadcSTom Herbert skb_clear_hash(skb); 610e6fbc5bSPravin B Shelar skb_dst_set(skb, &rt->dst); 620e6fbc5bSPravin B Shelar memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 630e6fbc5bSPravin B Shelar 640e6fbc5bSPravin B Shelar /* Push down and install the IP header. */ 6578a3694dSSteffen Klassert skb_push(skb, sizeof(struct iphdr)); 660e6fbc5bSPravin B Shelar skb_reset_network_header(skb); 670e6fbc5bSPravin B Shelar 680e6fbc5bSPravin B Shelar iph = ip_hdr(skb); 690e6fbc5bSPravin B Shelar 700e6fbc5bSPravin B Shelar iph->version = 4; 710e6fbc5bSPravin B Shelar iph->ihl = sizeof(struct iphdr) >> 2; 720e6fbc5bSPravin B Shelar iph->frag_off = df; 730e6fbc5bSPravin B Shelar iph->protocol = proto; 740e6fbc5bSPravin B Shelar iph->tos = tos; 750e6fbc5bSPravin B Shelar iph->daddr = dst; 760e6fbc5bSPravin B Shelar iph->saddr = src; 770e6fbc5bSPravin B Shelar iph->ttl = ttl; 78926a882fSHannes Frederic Sowa __ip_select_ident(dev_net(rt->dst.dev), iph, 79926a882fSHannes Frederic Sowa skb_shinfo(skb)->gso_segs ?: 1); 800e6fbc5bSPravin B Shelar 81aad88724SEric Dumazet err = ip_local_out_sk(sk, skb); 820e6fbc5bSPravin B Shelar if (unlikely(net_xmit_eval(err))) 830e6fbc5bSPravin B Shelar pkt_len = 0; 840e6fbc5bSPravin B Shelar return pkt_len; 850e6fbc5bSPravin B Shelar } 860e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit); 873d7b46cdSPravin B Shelar 883d7b46cdSPravin B Shelar int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto) 893d7b46cdSPravin B Shelar { 903d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, hdr_len))) 913d7b46cdSPravin B Shelar return -ENOMEM; 923d7b46cdSPravin B Shelar 933d7b46cdSPravin B Shelar skb_pull_rcsum(skb, hdr_len); 943d7b46cdSPravin B Shelar 953d7b46cdSPravin B Shelar if (inner_proto == htons(ETH_P_TEB)) { 961245dfc8SLi RongQing struct ethhdr *eh; 973d7b46cdSPravin B Shelar 983d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 993d7b46cdSPravin B Shelar return -ENOMEM; 1003d7b46cdSPravin B Shelar 1011245dfc8SLi RongQing eh = (struct ethhdr *)skb->data; 102d181ddcaSAlexander Duyck if (likely(eth_proto_is_802_3(eh->h_proto))) 1033d7b46cdSPravin B Shelar skb->protocol = eh->h_proto; 1043d7b46cdSPravin B Shelar else 1053d7b46cdSPravin B Shelar skb->protocol = htons(ETH_P_802_2); 1063d7b46cdSPravin B Shelar 1073d7b46cdSPravin B Shelar } else { 1083d7b46cdSPravin B Shelar skb->protocol = inner_proto; 1093d7b46cdSPravin B Shelar } 1103d7b46cdSPravin B Shelar 1113d7b46cdSPravin B Shelar nf_reset(skb); 1123d7b46cdSPravin B Shelar secpath_reset(skb); 1137539fadcSTom Herbert skb_clear_hash_if_not_l4(skb); 114fbd02dd4SPravin B Shelar skb_dst_drop(skb); 1153d7b46cdSPravin B Shelar skb->vlan_tci = 0; 1163d7b46cdSPravin B Shelar skb_set_queue_mapping(skb, 0); 1173d7b46cdSPravin B Shelar skb->pkt_type = PACKET_HOST; 1183d7b46cdSPravin B Shelar return 0; 1193d7b46cdSPravin B Shelar } 1203d7b46cdSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_pull_header); 1212d26f0a3SEric Dumazet 1222d26f0a3SEric Dumazet struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb, 1232d26f0a3SEric Dumazet bool csum_help, 1242d26f0a3SEric Dumazet int gso_type_mask) 1252d26f0a3SEric Dumazet { 1262d26f0a3SEric Dumazet int err; 1272d26f0a3SEric Dumazet 1282d26f0a3SEric Dumazet if (likely(!skb->encapsulation)) { 1292d26f0a3SEric Dumazet skb_reset_inner_headers(skb); 1302d26f0a3SEric Dumazet skb->encapsulation = 1; 1312d26f0a3SEric Dumazet } 1322d26f0a3SEric Dumazet 1332d26f0a3SEric Dumazet if (skb_is_gso(skb)) { 1342d26f0a3SEric Dumazet err = skb_unclone(skb, GFP_ATOMIC); 1352d26f0a3SEric Dumazet if (unlikely(err)) 1362d26f0a3SEric Dumazet goto error; 1372d26f0a3SEric Dumazet skb_shinfo(skb)->gso_type |= gso_type_mask; 1382d26f0a3SEric Dumazet return skb; 1392d26f0a3SEric Dumazet } 1402d26f0a3SEric Dumazet 1417e2b10c1STom Herbert /* If packet is not gso and we are resolving any partial checksum, 1427e2b10c1STom Herbert * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL 1437e2b10c1STom Herbert * on the outer header without confusing devices that implement 1447e2b10c1STom Herbert * NETIF_F_IP_CSUM with encapsulation. 1457e2b10c1STom Herbert */ 1467e2b10c1STom Herbert if (csum_help) 1477e2b10c1STom Herbert skb->encapsulation = 0; 1487e2b10c1STom Herbert 1492d26f0a3SEric Dumazet if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) { 1502d26f0a3SEric Dumazet err = skb_checksum_help(skb); 1512d26f0a3SEric Dumazet if (unlikely(err)) 1522d26f0a3SEric Dumazet goto error; 1532d26f0a3SEric Dumazet } else if (skb->ip_summed != CHECKSUM_PARTIAL) 1542d26f0a3SEric Dumazet skb->ip_summed = CHECKSUM_NONE; 1552d26f0a3SEric Dumazet 1562d26f0a3SEric Dumazet return skb; 1572d26f0a3SEric Dumazet error: 1582d26f0a3SEric Dumazet kfree_skb(skb); 1592d26f0a3SEric Dumazet return ERR_PTR(err); 1602d26f0a3SEric Dumazet } 1612d26f0a3SEric Dumazet EXPORT_SYMBOL_GPL(iptunnel_handle_offloads); 162ebe44f35SDavid S. Miller 163ebe44f35SDavid S. Miller /* Often modified stats are per cpu, other are shared (netdev->stats) */ 164ebe44f35SDavid S. Miller struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev, 165ebe44f35SDavid S. Miller struct rtnl_link_stats64 *tot) 166ebe44f35SDavid S. Miller { 167ebe44f35SDavid S. Miller int i; 168ebe44f35SDavid S. Miller 169c24a5964SAlexander Duyck netdev_stats_to_stats64(tot, &dev->stats); 170c24a5964SAlexander Duyck 171ebe44f35SDavid S. Miller for_each_possible_cpu(i) { 172ebe44f35SDavid S. Miller const struct pcpu_sw_netstats *tstats = 173ebe44f35SDavid S. Miller per_cpu_ptr(dev->tstats, i); 174ebe44f35SDavid S. Miller u64 rx_packets, rx_bytes, tx_packets, tx_bytes; 175ebe44f35SDavid S. Miller unsigned int start; 176ebe44f35SDavid S. Miller 177ebe44f35SDavid S. Miller do { 17857a7744eSEric W. Biederman start = u64_stats_fetch_begin_irq(&tstats->syncp); 179ebe44f35SDavid S. Miller rx_packets = tstats->rx_packets; 180ebe44f35SDavid S. Miller tx_packets = tstats->tx_packets; 181ebe44f35SDavid S. Miller rx_bytes = tstats->rx_bytes; 182ebe44f35SDavid S. Miller tx_bytes = tstats->tx_bytes; 18357a7744eSEric W. Biederman } while (u64_stats_fetch_retry_irq(&tstats->syncp, start)); 184ebe44f35SDavid S. Miller 185ebe44f35SDavid S. Miller tot->rx_packets += rx_packets; 186ebe44f35SDavid S. Miller tot->tx_packets += tx_packets; 187ebe44f35SDavid S. Miller tot->rx_bytes += rx_bytes; 188ebe44f35SDavid S. Miller tot->tx_bytes += tx_bytes; 189ebe44f35SDavid S. Miller } 190ebe44f35SDavid S. Miller 191ebe44f35SDavid S. Miller return tot; 192ebe44f35SDavid S. Miller } 193ebe44f35SDavid S. Miller EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64); 1943093fbe7SThomas Graf 195a1c234f9SJiri Benc static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = { 196a1c234f9SJiri Benc [LWTUNNEL_IP_ID] = { .type = NLA_U64 }, 197a1c234f9SJiri Benc [LWTUNNEL_IP_DST] = { .type = NLA_U32 }, 198a1c234f9SJiri Benc [LWTUNNEL_IP_SRC] = { .type = NLA_U32 }, 199a1c234f9SJiri Benc [LWTUNNEL_IP_TTL] = { .type = NLA_U8 }, 200a1c234f9SJiri Benc [LWTUNNEL_IP_TOS] = { .type = NLA_U8 }, 201a1c234f9SJiri Benc [LWTUNNEL_IP_SPORT] = { .type = NLA_U16 }, 202a1c234f9SJiri Benc [LWTUNNEL_IP_DPORT] = { .type = NLA_U16 }, 203a1c234f9SJiri Benc [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 }, 2043093fbe7SThomas Graf }; 2053093fbe7SThomas Graf 2063093fbe7SThomas Graf static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr, 2073093fbe7SThomas Graf struct lwtunnel_state **ts) 2083093fbe7SThomas Graf { 2093093fbe7SThomas Graf struct ip_tunnel_info *tun_info; 2103093fbe7SThomas Graf struct lwtunnel_state *new_state; 211a1c234f9SJiri Benc struct nlattr *tb[LWTUNNEL_IP_MAX + 1]; 2123093fbe7SThomas Graf int err; 2133093fbe7SThomas Graf 214a1c234f9SJiri Benc err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy); 2153093fbe7SThomas Graf if (err < 0) 2163093fbe7SThomas Graf return err; 2173093fbe7SThomas Graf 2183093fbe7SThomas Graf new_state = lwtunnel_state_alloc(sizeof(*tun_info)); 2193093fbe7SThomas Graf if (!new_state) 2203093fbe7SThomas Graf return -ENOMEM; 2213093fbe7SThomas Graf 2223093fbe7SThomas Graf new_state->type = LWTUNNEL_ENCAP_IP; 2233093fbe7SThomas Graf 2243093fbe7SThomas Graf tun_info = lwt_tun_info(new_state); 2253093fbe7SThomas Graf 226a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_ID]) 227a1c234f9SJiri Benc tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP_ID]); 2283093fbe7SThomas Graf 229a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_DST]) 230c1ea5d67SJiri Benc tun_info->key.u.ipv4.dst = nla_get_be32(tb[LWTUNNEL_IP_DST]); 2313093fbe7SThomas Graf 232a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_SRC]) 233c1ea5d67SJiri Benc tun_info->key.u.ipv4.src = nla_get_be32(tb[LWTUNNEL_IP_SRC]); 2343093fbe7SThomas Graf 235a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TTL]) 2367c383fb2SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]); 2373093fbe7SThomas Graf 238a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_TOS]) 2397c383fb2SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]); 2403093fbe7SThomas Graf 241a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_SPORT]) 242a1c234f9SJiri Benc tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP_SPORT]); 2433093fbe7SThomas Graf 244a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_DPORT]) 245a1c234f9SJiri Benc tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP_DPORT]); 2463093fbe7SThomas Graf 247a1c234f9SJiri Benc if (tb[LWTUNNEL_IP_FLAGS]) 248a1c234f9SJiri Benc tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP_FLAGS]); 2493093fbe7SThomas Graf 2503093fbe7SThomas Graf tun_info->mode = IP_TUNNEL_INFO_TX; 2513093fbe7SThomas Graf tun_info->options = NULL; 2523093fbe7SThomas Graf tun_info->options_len = 0; 2533093fbe7SThomas Graf 2543093fbe7SThomas Graf *ts = new_state; 2553093fbe7SThomas Graf 2563093fbe7SThomas Graf return 0; 2573093fbe7SThomas Graf } 2583093fbe7SThomas Graf 2593093fbe7SThomas Graf static int ip_tun_fill_encap_info(struct sk_buff *skb, 2603093fbe7SThomas Graf struct lwtunnel_state *lwtstate) 2613093fbe7SThomas Graf { 2623093fbe7SThomas Graf struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 2633093fbe7SThomas Graf 264a1c234f9SJiri Benc if (nla_put_u64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id) || 265c1ea5d67SJiri Benc nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) || 266c1ea5d67SJiri Benc nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) || 2677c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) || 2687c383fb2SJiri Benc nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) || 269a1c234f9SJiri Benc nla_put_u16(skb, LWTUNNEL_IP_SPORT, tun_info->key.tp_src) || 270a1c234f9SJiri Benc nla_put_u16(skb, LWTUNNEL_IP_DPORT, tun_info->key.tp_dst) || 271a1c234f9SJiri Benc nla_put_u16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags)) 2723093fbe7SThomas Graf return -ENOMEM; 2733093fbe7SThomas Graf 2743093fbe7SThomas Graf return 0; 2753093fbe7SThomas Graf } 2763093fbe7SThomas Graf 2773093fbe7SThomas Graf static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 2783093fbe7SThomas Graf { 279a1c234f9SJiri Benc return nla_total_size(8) /* LWTUNNEL_IP_ID */ 280a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_DST */ 281a1c234f9SJiri Benc + nla_total_size(4) /* LWTUNNEL_IP_SRC */ 282a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TOS */ 283a1c234f9SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP_TTL */ 284a1c234f9SJiri Benc + nla_total_size(2) /* LWTUNNEL_IP_SPORT */ 285a1c234f9SJiri Benc + nla_total_size(2) /* LWTUNNEL_IP_DPORT */ 286a1c234f9SJiri Benc + nla_total_size(2); /* LWTUNNEL_IP_FLAGS */ 2873093fbe7SThomas Graf } 2883093fbe7SThomas Graf 2892d798499SJiri Benc static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b) 2902d798499SJiri Benc { 2912d798499SJiri Benc return memcmp(lwt_tun_info(a), lwt_tun_info(b), 2922d798499SJiri Benc sizeof(struct ip_tunnel_info)); 2932d798499SJiri Benc } 2942d798499SJiri Benc 2953093fbe7SThomas Graf static const struct lwtunnel_encap_ops ip_tun_lwt_ops = { 2963093fbe7SThomas Graf .build_state = ip_tun_build_state, 2973093fbe7SThomas Graf .fill_encap = ip_tun_fill_encap_info, 2983093fbe7SThomas Graf .get_encap_size = ip_tun_encap_nlsize, 2992d798499SJiri Benc .cmp_encap = ip_tun_cmp_encap, 3003093fbe7SThomas Graf }; 3013093fbe7SThomas Graf 30232a2b002SJiri Benc static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = { 30332a2b002SJiri Benc [LWTUNNEL_IP6_ID] = { .type = NLA_U64 }, 30432a2b002SJiri Benc [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) }, 30532a2b002SJiri Benc [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) }, 30632a2b002SJiri Benc [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 }, 30732a2b002SJiri Benc [LWTUNNEL_IP6_TC] = { .type = NLA_U8 }, 30832a2b002SJiri Benc [LWTUNNEL_IP6_SPORT] = { .type = NLA_U16 }, 30932a2b002SJiri Benc [LWTUNNEL_IP6_DPORT] = { .type = NLA_U16 }, 31032a2b002SJiri Benc [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 }, 31132a2b002SJiri Benc }; 31232a2b002SJiri Benc 31332a2b002SJiri Benc static int ip6_tun_build_state(struct net_device *dev, struct nlattr *attr, 31432a2b002SJiri Benc struct lwtunnel_state **ts) 31532a2b002SJiri Benc { 31632a2b002SJiri Benc struct ip_tunnel_info *tun_info; 31732a2b002SJiri Benc struct lwtunnel_state *new_state; 31832a2b002SJiri Benc struct nlattr *tb[LWTUNNEL_IP6_MAX + 1]; 31932a2b002SJiri Benc int err; 32032a2b002SJiri Benc 32132a2b002SJiri Benc err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy); 32232a2b002SJiri Benc if (err < 0) 32332a2b002SJiri Benc return err; 32432a2b002SJiri Benc 32532a2b002SJiri Benc new_state = lwtunnel_state_alloc(sizeof(*tun_info)); 32632a2b002SJiri Benc if (!new_state) 32732a2b002SJiri Benc return -ENOMEM; 32832a2b002SJiri Benc 32932a2b002SJiri Benc new_state->type = LWTUNNEL_ENCAP_IP6; 33032a2b002SJiri Benc 33132a2b002SJiri Benc tun_info = lwt_tun_info(new_state); 33232a2b002SJiri Benc 33332a2b002SJiri Benc if (tb[LWTUNNEL_IP6_ID]) 33432a2b002SJiri Benc tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP6_ID]); 33532a2b002SJiri Benc 33632a2b002SJiri Benc if (tb[LWTUNNEL_IP6_DST]) 33732a2b002SJiri Benc tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]); 33832a2b002SJiri Benc 33932a2b002SJiri Benc if (tb[LWTUNNEL_IP6_SRC]) 34032a2b002SJiri Benc tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]); 34132a2b002SJiri Benc 34232a2b002SJiri Benc if (tb[LWTUNNEL_IP6_HOPLIMIT]) 34332a2b002SJiri Benc tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]); 34432a2b002SJiri Benc 34532a2b002SJiri Benc if (tb[LWTUNNEL_IP6_TC]) 34632a2b002SJiri Benc tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]); 34732a2b002SJiri Benc 34832a2b002SJiri Benc if (tb[LWTUNNEL_IP6_SPORT]) 34932a2b002SJiri Benc tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP6_SPORT]); 35032a2b002SJiri Benc 35132a2b002SJiri Benc if (tb[LWTUNNEL_IP6_DPORT]) 35232a2b002SJiri Benc tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP6_DPORT]); 35332a2b002SJiri Benc 35432a2b002SJiri Benc if (tb[LWTUNNEL_IP6_FLAGS]) 35532a2b002SJiri Benc tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP6_FLAGS]); 35632a2b002SJiri Benc 35732a2b002SJiri Benc tun_info->mode = IP_TUNNEL_INFO_TX; 35832a2b002SJiri Benc tun_info->options = NULL; 35932a2b002SJiri Benc tun_info->options_len = 0; 36032a2b002SJiri Benc 36132a2b002SJiri Benc *ts = new_state; 36232a2b002SJiri Benc 36332a2b002SJiri Benc return 0; 36432a2b002SJiri Benc } 36532a2b002SJiri Benc 36632a2b002SJiri Benc static int ip6_tun_fill_encap_info(struct sk_buff *skb, 36732a2b002SJiri Benc struct lwtunnel_state *lwtstate) 36832a2b002SJiri Benc { 36932a2b002SJiri Benc struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate); 37032a2b002SJiri Benc 37132a2b002SJiri Benc if (nla_put_u64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id) || 37232a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) || 37332a2b002SJiri Benc nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) || 37432a2b002SJiri Benc nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.tos) || 37532a2b002SJiri Benc nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.ttl) || 37632a2b002SJiri Benc nla_put_u16(skb, LWTUNNEL_IP6_SPORT, tun_info->key.tp_src) || 37732a2b002SJiri Benc nla_put_u16(skb, LWTUNNEL_IP6_DPORT, tun_info->key.tp_dst) || 37832a2b002SJiri Benc nla_put_u16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags)) 37932a2b002SJiri Benc return -ENOMEM; 38032a2b002SJiri Benc 38132a2b002SJiri Benc return 0; 38232a2b002SJiri Benc } 38332a2b002SJiri Benc 38432a2b002SJiri Benc static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate) 38532a2b002SJiri Benc { 38632a2b002SJiri Benc return nla_total_size(8) /* LWTUNNEL_IP6_ID */ 38732a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_DST */ 38832a2b002SJiri Benc + nla_total_size(16) /* LWTUNNEL_IP6_SRC */ 38932a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */ 39032a2b002SJiri Benc + nla_total_size(1) /* LWTUNNEL_IP6_TC */ 39132a2b002SJiri Benc + nla_total_size(2) /* LWTUNNEL_IP6_SPORT */ 39232a2b002SJiri Benc + nla_total_size(2) /* LWTUNNEL_IP6_DPORT */ 39332a2b002SJiri Benc + nla_total_size(2); /* LWTUNNEL_IP6_FLAGS */ 39432a2b002SJiri Benc } 39532a2b002SJiri Benc 39632a2b002SJiri Benc static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = { 39732a2b002SJiri Benc .build_state = ip6_tun_build_state, 39832a2b002SJiri Benc .fill_encap = ip6_tun_fill_encap_info, 39932a2b002SJiri Benc .get_encap_size = ip6_tun_encap_nlsize, 40032a2b002SJiri Benc .cmp_encap = ip_tun_cmp_encap, 40132a2b002SJiri Benc }; 40232a2b002SJiri Benc 403045a0fa0SThomas Graf void __init ip_tunnel_core_init(void) 4043093fbe7SThomas Graf { 4053093fbe7SThomas Graf lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP); 40632a2b002SJiri Benc lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6); 4073093fbe7SThomas Graf } 408e7030878SThomas Graf 409e7030878SThomas Graf struct static_key ip_tunnel_metadata_cnt = STATIC_KEY_INIT_FALSE; 410e7030878SThomas Graf EXPORT_SYMBOL(ip_tunnel_metadata_cnt); 411e7030878SThomas Graf 412e7030878SThomas Graf void ip_tunnel_need_metadata(void) 413e7030878SThomas Graf { 414e7030878SThomas Graf static_key_slow_inc(&ip_tunnel_metadata_cnt); 415e7030878SThomas Graf } 416e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata); 417e7030878SThomas Graf 418e7030878SThomas Graf void ip_tunnel_unneed_metadata(void) 419e7030878SThomas Graf { 420e7030878SThomas Graf static_key_slow_dec(&ip_tunnel_metadata_cnt); 421e7030878SThomas Graf } 422e7030878SThomas Graf EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata); 423