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> 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> 480e6fbc5bSPravin B Shelar 490e6fbc5bSPravin B Shelar int iptunnel_xmit(struct net *net, struct rtable *rt, 500e6fbc5bSPravin B Shelar struct sk_buff *skb, 510e6fbc5bSPravin B Shelar __be32 src, __be32 dst, __u8 proto, 520e6fbc5bSPravin B Shelar __u8 tos, __u8 ttl, __be16 df) 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 580e6fbc5bSPravin B Shelar nf_reset(skb); 590e6fbc5bSPravin B Shelar secpath_reset(skb); 600e6fbc5bSPravin B Shelar skb->rxhash = 0; 610e6fbc5bSPravin B Shelar skb_dst_drop(skb); 620e6fbc5bSPravin B Shelar skb_dst_set(skb, &rt->dst); 630e6fbc5bSPravin B Shelar memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 640e6fbc5bSPravin B Shelar 650e6fbc5bSPravin B Shelar /* Push down and install the IP header. */ 660e6fbc5bSPravin B Shelar __skb_push(skb, sizeof(struct iphdr)); 670e6fbc5bSPravin B Shelar skb_reset_network_header(skb); 680e6fbc5bSPravin B Shelar 690e6fbc5bSPravin B Shelar iph = ip_hdr(skb); 700e6fbc5bSPravin B Shelar 710e6fbc5bSPravin B Shelar iph->version = 4; 720e6fbc5bSPravin B Shelar iph->ihl = sizeof(struct iphdr) >> 2; 730e6fbc5bSPravin B Shelar iph->frag_off = df; 740e6fbc5bSPravin B Shelar iph->protocol = proto; 750e6fbc5bSPravin B Shelar iph->tos = tos; 760e6fbc5bSPravin B Shelar iph->daddr = dst; 770e6fbc5bSPravin B Shelar iph->saddr = src; 780e6fbc5bSPravin B Shelar iph->ttl = ttl; 790e6fbc5bSPravin B Shelar tunnel_ip_select_ident(skb, 800e6fbc5bSPravin B Shelar (const struct iphdr *)skb_inner_network_header(skb), 810e6fbc5bSPravin B Shelar &rt->dst); 820e6fbc5bSPravin B Shelar 830e6fbc5bSPravin B Shelar err = ip_local_out(skb); 840e6fbc5bSPravin B Shelar if (unlikely(net_xmit_eval(err))) 850e6fbc5bSPravin B Shelar pkt_len = 0; 860e6fbc5bSPravin B Shelar return pkt_len; 870e6fbc5bSPravin B Shelar } 880e6fbc5bSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_xmit); 893d7b46cdSPravin B Shelar 903d7b46cdSPravin B Shelar int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto) 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)) { 983d7b46cdSPravin B Shelar struct ethhdr *eh = (struct ethhdr *)skb->data; 993d7b46cdSPravin B Shelar 1003d7b46cdSPravin B Shelar if (unlikely(!pskb_may_pull(skb, ETH_HLEN))) 1013d7b46cdSPravin B Shelar return -ENOMEM; 1023d7b46cdSPravin B Shelar 1033d7b46cdSPravin B Shelar if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN)) 1043d7b46cdSPravin B Shelar skb->protocol = eh->h_proto; 1053d7b46cdSPravin B Shelar else 1063d7b46cdSPravin B Shelar skb->protocol = htons(ETH_P_802_2); 1073d7b46cdSPravin B Shelar 1083d7b46cdSPravin B Shelar } else { 1093d7b46cdSPravin B Shelar skb->protocol = inner_proto; 1103d7b46cdSPravin B Shelar } 1113d7b46cdSPravin B Shelar 1123d7b46cdSPravin B Shelar nf_reset(skb); 1133d7b46cdSPravin B Shelar secpath_reset(skb); 1143d7b46cdSPravin B Shelar if (!skb->l4_rxhash) 1153d7b46cdSPravin B Shelar skb->rxhash = 0; 1163d7b46cdSPravin B Shelar skb_dst_drop(skb); 1173d7b46cdSPravin B Shelar skb->vlan_tci = 0; 1183d7b46cdSPravin B Shelar skb_set_queue_mapping(skb, 0); 1193d7b46cdSPravin B Shelar skb->pkt_type = PACKET_HOST; 1203d7b46cdSPravin B Shelar return 0; 1213d7b46cdSPravin B Shelar } 1223d7b46cdSPravin B Shelar EXPORT_SYMBOL_GPL(iptunnel_pull_header); 123