ip_tunnel_core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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(dev_net(rt->dst.dev), iph,
  69. skb_shinfo(skb)->gso_segs ?: 1);
  70. err = ip_local_out_sk(sk, skb);
  71. if (unlikely(net_xmit_eval(err)))
  72. pkt_len = 0;
  73. return pkt_len;
  74. }
  75. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  76. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
  77. {
  78. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  79. return -ENOMEM;
  80. skb_pull_rcsum(skb, hdr_len);
  81. if (inner_proto == htons(ETH_P_TEB)) {
  82. struct ethhdr *eh;
  83. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  84. return -ENOMEM;
  85. eh = (struct ethhdr *)skb->data;
  86. if (likely(eth_proto_is_802_3(eh->h_proto)))
  87. skb->protocol = eh->h_proto;
  88. else
  89. skb->protocol = htons(ETH_P_802_2);
  90. } else {
  91. skb->protocol = inner_proto;
  92. }
  93. nf_reset(skb);
  94. secpath_reset(skb);
  95. skb_clear_hash_if_not_l4(skb);
  96. skb_dst_drop(skb);
  97. skb->vlan_tci = 0;
  98. skb_set_queue_mapping(skb, 0);
  99. skb->pkt_type = PACKET_HOST;
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(iptunnel_pull_header);
  103. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
  104. bool csum_help,
  105. int gso_type_mask)
  106. {
  107. int err;
  108. if (likely(!skb->encapsulation)) {
  109. skb_reset_inner_headers(skb);
  110. skb->encapsulation = 1;
  111. }
  112. if (skb_is_gso(skb)) {
  113. err = skb_unclone(skb, GFP_ATOMIC);
  114. if (unlikely(err))
  115. goto error;
  116. skb_shinfo(skb)->gso_type |= gso_type_mask;
  117. return skb;
  118. }
  119. /* If packet is not gso and we are resolving any partial checksum,
  120. * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
  121. * on the outer header without confusing devices that implement
  122. * NETIF_F_IP_CSUM with encapsulation.
  123. */
  124. if (csum_help)
  125. skb->encapsulation = 0;
  126. if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
  127. err = skb_checksum_help(skb);
  128. if (unlikely(err))
  129. goto error;
  130. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  131. skb->ip_summed = CHECKSUM_NONE;
  132. return skb;
  133. error:
  134. kfree_skb(skb);
  135. return ERR_PTR(err);
  136. }
  137. EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
  138. /* Often modified stats are per cpu, other are shared (netdev->stats) */
  139. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  140. struct rtnl_link_stats64 *tot)
  141. {
  142. int i;
  143. netdev_stats_to_stats64(tot, &dev->stats);
  144. for_each_possible_cpu(i) {
  145. const struct pcpu_sw_netstats *tstats =
  146. per_cpu_ptr(dev->tstats, i);
  147. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  148. unsigned int start;
  149. do {
  150. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  151. rx_packets = tstats->rx_packets;
  152. tx_packets = tstats->tx_packets;
  153. rx_bytes = tstats->rx_bytes;
  154. tx_bytes = tstats->tx_bytes;
  155. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  156. tot->rx_packets += rx_packets;
  157. tot->tx_packets += tx_packets;
  158. tot->rx_bytes += rx_bytes;
  159. tot->tx_bytes += tx_bytes;
  160. }
  161. return tot;
  162. }
  163. EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
  164. static const struct nla_policy ip_tun_policy[IP_TUN_MAX + 1] = {
  165. [IP_TUN_ID] = { .type = NLA_U64 },
  166. [IP_TUN_DST] = { .type = NLA_U32 },
  167. [IP_TUN_SRC] = { .type = NLA_U32 },
  168. [IP_TUN_TTL] = { .type = NLA_U8 },
  169. [IP_TUN_TOS] = { .type = NLA_U8 },
  170. [IP_TUN_SPORT] = { .type = NLA_U16 },
  171. [IP_TUN_DPORT] = { .type = NLA_U16 },
  172. [IP_TUN_FLAGS] = { .type = NLA_U16 },
  173. };
  174. static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr,
  175. struct lwtunnel_state **ts)
  176. {
  177. struct ip_tunnel_info *tun_info;
  178. struct lwtunnel_state *new_state;
  179. struct nlattr *tb[IP_TUN_MAX + 1];
  180. int err;
  181. err = nla_parse_nested(tb, IP_TUN_MAX, attr, ip_tun_policy);
  182. if (err < 0)
  183. return err;
  184. new_state = lwtunnel_state_alloc(sizeof(*tun_info));
  185. if (!new_state)
  186. return -ENOMEM;
  187. new_state->type = LWTUNNEL_ENCAP_IP;
  188. tun_info = lwt_tun_info(new_state);
  189. if (tb[IP_TUN_ID])
  190. tun_info->key.tun_id = nla_get_u64(tb[IP_TUN_ID]);
  191. if (tb[IP_TUN_DST])
  192. tun_info->key.ipv4_dst = nla_get_be32(tb[IP_TUN_DST]);
  193. if (tb[IP_TUN_SRC])
  194. tun_info->key.ipv4_src = nla_get_be32(tb[IP_TUN_SRC]);
  195. if (tb[IP_TUN_TTL])
  196. tun_info->key.ipv4_ttl = nla_get_u8(tb[IP_TUN_TTL]);
  197. if (tb[IP_TUN_TOS])
  198. tun_info->key.ipv4_tos = nla_get_u8(tb[IP_TUN_TOS]);
  199. if (tb[IP_TUN_SPORT])
  200. tun_info->key.tp_src = nla_get_be16(tb[IP_TUN_SPORT]);
  201. if (tb[IP_TUN_DPORT])
  202. tun_info->key.tp_dst = nla_get_be16(tb[IP_TUN_DPORT]);
  203. if (tb[IP_TUN_FLAGS])
  204. tun_info->key.tun_flags = nla_get_u16(tb[IP_TUN_FLAGS]);
  205. tun_info->mode = IP_TUNNEL_INFO_TX;
  206. tun_info->options = NULL;
  207. tun_info->options_len = 0;
  208. *ts = new_state;
  209. return 0;
  210. }
  211. static int ip_tun_fill_encap_info(struct sk_buff *skb,
  212. struct lwtunnel_state *lwtstate)
  213. {
  214. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  215. if (nla_put_u64(skb, IP_TUN_ID, tun_info->key.tun_id) ||
  216. nla_put_be32(skb, IP_TUN_DST, tun_info->key.ipv4_dst) ||
  217. nla_put_be32(skb, IP_TUN_SRC, tun_info->key.ipv4_src) ||
  218. nla_put_u8(skb, IP_TUN_TOS, tun_info->key.ipv4_tos) ||
  219. nla_put_u8(skb, IP_TUN_TTL, tun_info->key.ipv4_ttl) ||
  220. nla_put_u16(skb, IP_TUN_SPORT, tun_info->key.tp_src) ||
  221. nla_put_u16(skb, IP_TUN_DPORT, tun_info->key.tp_dst) ||
  222. nla_put_u16(skb, IP_TUN_FLAGS, tun_info->key.tun_flags))
  223. return -ENOMEM;
  224. return 0;
  225. }
  226. static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  227. {
  228. return nla_total_size(8) /* IP_TUN_ID */
  229. + nla_total_size(4) /* IP_TUN_DST */
  230. + nla_total_size(4) /* IP_TUN_SRC */
  231. + nla_total_size(1) /* IP_TUN_TOS */
  232. + nla_total_size(1) /* IP_TUN_TTL */
  233. + nla_total_size(2) /* IP_TUN_SPORT */
  234. + nla_total_size(2) /* IP_TUN_DPORT */
  235. + nla_total_size(2); /* IP_TUN_FLAGS */
  236. }
  237. static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
  238. .build_state = ip_tun_build_state,
  239. .fill_encap = ip_tun_fill_encap_info,
  240. .get_encap_size = ip_tun_encap_nlsize,
  241. };
  242. static int __init ip_tunnel_core_init(void)
  243. {
  244. lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
  245. return 0;
  246. }
  247. module_init(ip_tunnel_core_init);
  248. static void __exit ip_tunnel_core_exit(void)
  249. {
  250. lwtunnel_encap_del_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
  251. }
  252. module_exit(ip_tunnel_core_exit);