blob: ce63ab21b6cda87f8caea29967a4651b7f78c909 [file] [log] [blame]
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001/*
2 * Copyright (c) 2013 Nicira, Inc.
3 *
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.
7 *
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.
12 *
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
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/types.h>
22#include <linux/kernel.h>
23#include <linux/skbuff.h>
24#include <linux/netdevice.h>
25#include <linux/in.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>
35
36#include <net/ip.h>
37#include <net/icmp.h>
38#include <net/protocol.h>
39#include <net/ip_tunnels.h>
40#include <net/arp.h>
41#include <net/checksum.h>
42#include <net/dsfield.h>
43#include <net/inet_ecn.h>
44#include <net/xfrm.h>
45#include <net/net_namespace.h>
46#include <net/netns/generic.h>
47#include <net/rtnetlink.h>
48
Eric Dumazetaad88722014-04-15 13:47:15 -040049int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070050 __be32 src, __be32 dst, __u8 proto,
Nicolas Dichtel963a88b2013-09-02 15:34:57 +020051 __u8 tos, __u8 ttl, __be16 df, bool xnet)
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070052{
53 int pkt_len = skb->len;
54 struct iphdr *iph;
55 int err;
56
Nicolas Dichtel963a88b2013-09-02 15:34:57 +020057 skb_scrub_packet(skb, xnet);
58
Tom Herbert7539fad2013-12-15 22:12:18 -080059 skb_clear_hash(skb);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070060 skb_dst_set(skb, &rt->dst);
61 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
62
63 /* Push down and install the IP header. */
Steffen Klassert78a36942013-10-01 11:35:51 +020064 skb_push(skb, sizeof(struct iphdr));
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070065 skb_reset_network_header(skb);
66
67 iph = ip_hdr(skb);
68
69 iph->version = 4;
70 iph->ihl = sizeof(struct iphdr) >> 2;
71 iph->frag_off = df;
72 iph->protocol = proto;
73 iph->tos = tos;
74 iph->daddr = dst;
75 iph->saddr = src;
76 iph->ttl = ttl;
Hannes Frederic Sowa926a8822015-04-08 17:01:22 +020077 __ip_select_ident(dev_net(rt->dst.dev), iph,
78 skb_shinfo(skb)->gso_segs ?: 1);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070079
Eric Dumazetaad88722014-04-15 13:47:15 -040080 err = ip_local_out_sk(sk, skb);
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -070081 if (unlikely(net_xmit_eval(err)))
82 pkt_len = 0;
83 return pkt_len;
84}
85EXPORT_SYMBOL_GPL(iptunnel_xmit);
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -070086
87int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
88{
89 if (unlikely(!pskb_may_pull(skb, hdr_len)))
90 return -ENOMEM;
91
92 skb_pull_rcsum(skb, hdr_len);
93
94 if (inner_proto == htons(ETH_P_TEB)) {
Li RongQing1245dfc2014-10-17 16:53:23 +080095 struct ethhdr *eh;
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -070096
97 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
98 return -ENOMEM;
99
Li RongQing1245dfc2014-10-17 16:53:23 +0800100 eh = (struct ethhdr *)skb->data;
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -0700101 if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
102 skb->protocol = eh->h_proto;
103 else
104 skb->protocol = htons(ETH_P_802_2);
105
106 } else {
107 skb->protocol = inner_proto;
108 }
109
110 nf_reset(skb);
111 secpath_reset(skb);
Tom Herbert7539fad2013-12-15 22:12:18 -0800112 skb_clear_hash_if_not_l4(skb);
Pravin B Shelarfbd02dd2014-03-23 22:06:36 -0700113 skb_dst_drop(skb);
Pravin B Shelar3d7b46c2013-06-17 17:50:02 -0700114 skb->vlan_tci = 0;
115 skb_set_queue_mapping(skb, 0);
116 skb->pkt_type = PACKET_HOST;
117 return 0;
118}
119EXPORT_SYMBOL_GPL(iptunnel_pull_header);
Eric Dumazet2d26f0a2013-10-19 11:42:55 -0700120
121struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
122 bool csum_help,
123 int gso_type_mask)
124{
125 int err;
126
127 if (likely(!skb->encapsulation)) {
128 skb_reset_inner_headers(skb);
129 skb->encapsulation = 1;
130 }
131
132 if (skb_is_gso(skb)) {
133 err = skb_unclone(skb, GFP_ATOMIC);
134 if (unlikely(err))
135 goto error;
136 skb_shinfo(skb)->gso_type |= gso_type_mask;
137 return skb;
138 }
139
Tom Herbert7e2b10c2014-06-04 17:20:02 -0700140 /* If packet is not gso and we are resolving any partial checksum,
141 * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
142 * on the outer header without confusing devices that implement
143 * NETIF_F_IP_CSUM with encapsulation.
144 */
145 if (csum_help)
146 skb->encapsulation = 0;
147
Eric Dumazet2d26f0a2013-10-19 11:42:55 -0700148 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
149 err = skb_checksum_help(skb);
150 if (unlikely(err))
151 goto error;
152 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
153 skb->ip_summed = CHECKSUM_NONE;
154
155 return skb;
156error:
157 kfree_skb(skb);
158 return ERR_PTR(err);
159}
160EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
David S. Millerebe44f32014-02-20 02:14:23 -0500161
162/* Often modified stats are per cpu, other are shared (netdev->stats) */
163struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
164 struct rtnl_link_stats64 *tot)
165{
166 int i;
167
168 for_each_possible_cpu(i) {
169 const struct pcpu_sw_netstats *tstats =
170 per_cpu_ptr(dev->tstats, i);
171 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
172 unsigned int start;
173
174 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700175 start = u64_stats_fetch_begin_irq(&tstats->syncp);
David S. Millerebe44f32014-02-20 02:14:23 -0500176 rx_packets = tstats->rx_packets;
177 tx_packets = tstats->tx_packets;
178 rx_bytes = tstats->rx_bytes;
179 tx_bytes = tstats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700180 } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
David S. Millerebe44f32014-02-20 02:14:23 -0500181
182 tot->rx_packets += rx_packets;
183 tot->tx_packets += tx_packets;
184 tot->rx_bytes += rx_bytes;
185 tot->tx_bytes += tx_bytes;
186 }
187
188 tot->multicast = dev->stats.multicast;
189
190 tot->rx_crc_errors = dev->stats.rx_crc_errors;
191 tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
192 tot->rx_length_errors = dev->stats.rx_length_errors;
193 tot->rx_frame_errors = dev->stats.rx_frame_errors;
194 tot->rx_errors = dev->stats.rx_errors;
195
196 tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
197 tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
198 tot->tx_dropped = dev->stats.tx_dropped;
199 tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
200 tot->tx_errors = dev->stats.tx_errors;
201
202 tot->collisions = dev->stats.collisions;
203
204 return tot;
205}
206EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);