ip_tunnel_core.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 net *net, struct rtable *rt,
  46. struct sk_buff *skb,
  47. __be32 src, __be32 dst, __u8 proto,
  48. __u8 tos, __u8 ttl, __be16 df)
  49. {
  50. int pkt_len = skb->len;
  51. struct iphdr *iph;
  52. int err;
  53. nf_reset(skb);
  54. secpath_reset(skb);
  55. skb->rxhash = 0;
  56. skb_dst_drop(skb);
  57. skb_dst_set(skb, &rt->dst);
  58. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  59. /* Push down and install the IP header. */
  60. __skb_push(skb, sizeof(struct iphdr));
  61. skb_reset_network_header(skb);
  62. iph = ip_hdr(skb);
  63. iph->version = 4;
  64. iph->ihl = sizeof(struct iphdr) >> 2;
  65. iph->frag_off = df;
  66. iph->protocol = proto;
  67. iph->tos = tos;
  68. iph->daddr = dst;
  69. iph->saddr = src;
  70. iph->ttl = ttl;
  71. tunnel_ip_select_ident(skb,
  72. (const struct iphdr *)skb_inner_network_header(skb),
  73. &rt->dst);
  74. err = ip_local_out(skb);
  75. if (unlikely(net_xmit_eval(err)))
  76. pkt_len = 0;
  77. return pkt_len;
  78. }
  79. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  80. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
  81. {
  82. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  83. return -ENOMEM;
  84. skb_pull_rcsum(skb, hdr_len);
  85. if (inner_proto == htons(ETH_P_TEB)) {
  86. struct ethhdr *eh = (struct ethhdr *)skb->data;
  87. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  88. return -ENOMEM;
  89. if (likely(ntohs(eh->h_proto) >= ETH_P_802_3_MIN))
  90. skb->protocol = eh->h_proto;
  91. else
  92. skb->protocol = htons(ETH_P_802_2);
  93. } else {
  94. skb->protocol = inner_proto;
  95. }
  96. nf_reset(skb);
  97. secpath_reset(skb);
  98. if (!skb->l4_rxhash)
  99. skb->rxhash = 0;
  100. skb_dst_drop(skb);
  101. skb->vlan_tci = 0;
  102. skb_set_queue_mapping(skb, 0);
  103. skb->pkt_type = PACKET_HOST;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(iptunnel_pull_header);