ip_tunnel_core.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/in.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/mroute.h>
  26. #include <linux/init.h>
  27. #include <linux/in6.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/netfilter_ipv4.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/if_ether.h>
  32. #include <linux/if_vlan.h>
  33. #include <net/ip.h>
  34. #include <net/icmp.h>
  35. #include <net/protocol.h>
  36. #include <net/ip_tunnels.h>
  37. #include <net/arp.h>
  38. #include <net/checksum.h>
  39. #include <net/dsfield.h>
  40. #include <net/inet_ecn.h>
  41. #include <net/xfrm.h>
  42. #include <net/net_namespace.h>
  43. #include <net/netns/generic.h>
  44. #include <net/rtnetlink.h>
  45. int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  46. __be32 src, __be32 dst, __u8 proto,
  47. __u8 tos, __u8 ttl, __be16 df, bool xnet)
  48. {
  49. int pkt_len = skb->len;
  50. struct iphdr *iph;
  51. int err;
  52. skb_scrub_packet(skb, xnet);
  53. skb_clear_hash(skb);
  54. skb_dst_set(skb, &rt->dst);
  55. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  56. /* Push down and install the IP header. */
  57. skb_push(skb, sizeof(struct iphdr));
  58. skb_reset_network_header(skb);
  59. iph = ip_hdr(skb);
  60. iph->version = 4;
  61. iph->ihl = sizeof(struct iphdr) >> 2;
  62. iph->frag_off = df;
  63. iph->protocol = proto;
  64. iph->tos = tos;
  65. iph->daddr = dst;
  66. iph->saddr = src;
  67. iph->ttl = ttl;
  68. __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1);
  69. err = ip_local_out_sk(sk, skb);
  70. if (unlikely(net_xmit_eval(err)))
  71. pkt_len = 0;
  72. return pkt_len;
  73. }
  74. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  75. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
  76. {
  77. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  78. return -ENOMEM;
  79. skb_pull_rcsum(skb, hdr_len);
  80. if (inner_proto == htons(ETH_P_TEB)) {
  81. struct ethhdr *eh;
  82. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  83. return -ENOMEM;
  84. eh = (struct ethhdr *)skb->data;
  85. if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
  86. skb->protocol = eh->h_proto;
  87. else
  88. skb->protocol = htons(ETH_P_802_2);
  89. } else {
  90. skb->protocol = inner_proto;
  91. }
  92. nf_reset(skb);
  93. secpath_reset(skb);
  94. skb_clear_hash_if_not_l4(skb);
  95. skb_dst_drop(skb);
  96. skb->vlan_tci = 0;
  97. skb_set_queue_mapping(skb, 0);
  98. skb->pkt_type = PACKET_HOST;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(iptunnel_pull_header);
  102. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
  103. bool csum_help,
  104. int gso_type_mask)
  105. {
  106. int err;
  107. if (likely(!skb->encapsulation)) {
  108. skb_reset_inner_headers(skb);
  109. skb->encapsulation = 1;
  110. }
  111. if (skb_is_gso(skb)) {
  112. err = skb_unclone(skb, GFP_ATOMIC);
  113. if (unlikely(err))
  114. goto error;
  115. skb_shinfo(skb)->gso_type |= gso_type_mask;
  116. return skb;
  117. }
  118. /* If packet is not gso and we are resolving any partial checksum,
  119. * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
  120. * on the outer header without confusing devices that implement
  121. * NETIF_F_IP_CSUM with encapsulation.
  122. */
  123. if (csum_help)
  124. skb->encapsulation = 0;
  125. if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
  126. err = skb_checksum_help(skb);
  127. if (unlikely(err))
  128. goto error;
  129. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  130. skb->ip_summed = CHECKSUM_NONE;
  131. return skb;
  132. error:
  133. kfree_skb(skb);
  134. return ERR_PTR(err);
  135. }
  136. EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
  137. /* Often modified stats are per cpu, other are shared (netdev->stats) */
  138. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  139. struct rtnl_link_stats64 *tot)
  140. {
  141. int i;
  142. for_each_possible_cpu(i) {
  143. const struct pcpu_sw_netstats *tstats =
  144. per_cpu_ptr(dev->tstats, i);
  145. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  146. unsigned int start;
  147. do {
  148. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  149. rx_packets = tstats->rx_packets;
  150. tx_packets = tstats->tx_packets;
  151. rx_bytes = tstats->rx_bytes;
  152. tx_bytes = tstats->tx_bytes;
  153. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  154. tot->rx_packets += rx_packets;
  155. tot->tx_packets += tx_packets;
  156. tot->rx_bytes += rx_bytes;
  157. tot->tx_bytes += tx_bytes;
  158. }
  159. tot->multicast = dev->stats.multicast;
  160. tot->rx_crc_errors = dev->stats.rx_crc_errors;
  161. tot->rx_fifo_errors = dev->stats.rx_fifo_errors;
  162. tot->rx_length_errors = dev->stats.rx_length_errors;
  163. tot->rx_frame_errors = dev->stats.rx_frame_errors;
  164. tot->rx_errors = dev->stats.rx_errors;
  165. tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
  166. tot->tx_carrier_errors = dev->stats.tx_carrier_errors;
  167. tot->tx_dropped = dev->stats.tx_dropped;
  168. tot->tx_aborted_errors = dev->stats.tx_aborted_errors;
  169. tot->tx_errors = dev->stats.tx_errors;
  170. tot->collisions = dev->stats.collisions;
  171. return tot;
  172. }
  173. EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);