2 * Copyright (c) 2013 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <linux/mroute.h>
28 #include <linux/init.h>
29 #include <linux/in6.h>
30 #include <linux/inetdevice.h>
31 #include <linux/netfilter_ipv4.h>
32 #include <linux/etherdevice.h>
33 #include <linux/if_ether.h>
34 #include <linux/if_vlan.h>
38 #include <net/protocol.h>
39 #include <net/ip_tunnels.h>
41 #include <net/checksum.h>
42 #include <net/dsfield.h>
43 #include <net/inet_ecn.h>
45 #include <net/net_namespace.h>
46 #include <net/netns/generic.h>
47 #include <net/rtnetlink.h>
49 int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
50 __be32 src, __be32 dst, __u8 proto,
51 __u8 tos, __u8 ttl, __be16 df, bool xnet)
53 int pkt_len = skb->len;
57 skb_scrub_packet(skb, xnet);
60 skb_dst_set(skb, &rt->dst);
61 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
63 /* Push down and install the IP header. */
64 skb_push(skb, sizeof(struct iphdr));
65 skb_reset_network_header(skb);
70 iph->ihl = sizeof(struct iphdr) >> 2;
72 iph->protocol = proto;
77 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
79 err = ip_local_out_sk(sk, skb);
80 if (unlikely(net_xmit_eval(err)))
84 EXPORT_SYMBOL_GPL(iptunnel_xmit);
86 int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
88 if (unlikely(!pskb_may_pull(skb, hdr_len)))
91 skb_pull_rcsum(skb, hdr_len);
93 if (inner_proto == htons(ETH_P_TEB)) {
96 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
99 eh = (struct ethhdr *)skb->data;
100 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
101 skb->protocol = eh->h_proto;
103 skb->protocol = htons(ETH_P_802_2);
106 skb->protocol = inner_proto;
111 skb_clear_hash_if_not_l4(skb);
114 skb_set_queue_mapping(skb, 0);
115 skb->pkt_type = PACKET_HOST;
118 EXPORT_SYMBOL_GPL(iptunnel_pull_header);
120 struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
126 if (likely(!skb->encapsulation)) {
127 skb_reset_inner_headers(skb);
128 skb->encapsulation = 1;
131 if (skb_is_gso(skb)) {
132 err = skb_unclone(skb, GFP_ATOMIC);
135 skb_shinfo(skb)->gso_type |= gso_type_mask;
139 /* If packet is not gso and we are resolving any partial checksum,
140 * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
141 * on the outer header without confusing devices that implement
142 * NETIF_F_IP_CSUM with encapsulation.
145 skb->encapsulation = 0;
147 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
148 err = skb_checksum_help(skb);
151 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
152 skb->ip_summed = CHECKSUM_NONE;
159 EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
161 /* Often modified stats are per cpu, other are shared (netdev->stats) */
162 struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
163 struct rtnl_link_stats64 *tot)
167 for_each_possible_cpu(i) {
168 const struct pcpu_sw_netstats *tstats =
169 per_cpu_ptr(dev->tstats, i);
170 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
174 start = u64_stats_fetch_begin_irq(&tstats->syncp);
175 rx_packets = tstats->rx_packets;
176 tx_packets = tstats->tx_packets;
177 rx_bytes = tstats->rx_bytes;
178 tx_bytes = tstats->tx_bytes;
179 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
181 tot->rx_packets += rx_packets;
182 tot->tx_packets += tx_packets;
183 tot->rx_bytes += rx_bytes;
184 tot->tx_bytes += tx_bytes;
187 tot->multicast = dev->stats.multicast;
189 tot->rx_crc_errors = dev->stats.rx_crc_errors;
190 tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
191 tot->rx_length_errors = dev->stats.rx_length_errors;
192 tot->rx_frame_errors = dev->stats.rx_frame_errors;
193 tot->rx_errors = dev->stats.rx_errors;
195 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
196 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
197 tot->tx_dropped = dev->stats.tx_dropped;
198 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
199 tot->tx_errors = dev->stats.tx_errors;
201 tot->collisions = dev->stats.collisions;
205 EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);