ip_tunnel_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/init.h>
  26. #include <linux/in6.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/netfilter_ipv4.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/if_vlan.h>
  32. #include <linux/static_key.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/ip6_tunnel.h>
  38. #include <net/arp.h>
  39. #include <net/checksum.h>
  40. #include <net/dsfield.h>
  41. #include <net/inet_ecn.h>
  42. #include <net/xfrm.h>
  43. #include <net/net_namespace.h>
  44. #include <net/netns/generic.h>
  45. #include <net/rtnetlink.h>
  46. #include <net/dst_metadata.h>
  47. const struct ip_tunnel_encap_ops __rcu *
  48. iptun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
  49. EXPORT_SYMBOL(iptun_encaps);
  50. const struct ip6_tnl_encap_ops __rcu *
  51. ip6tun_encaps[MAX_IPTUN_ENCAP_OPS] __read_mostly;
  52. EXPORT_SYMBOL(ip6tun_encaps);
  53. void iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  54. __be32 src, __be32 dst, __u8 proto,
  55. __u8 tos, __u8 ttl, __be16 df, bool xnet)
  56. {
  57. int pkt_len = skb->len - skb_inner_network_offset(skb);
  58. struct net *net = dev_net(rt->dst.dev);
  59. struct net_device *dev = skb->dev;
  60. int skb_iif = skb->skb_iif;
  61. struct iphdr *iph;
  62. int err;
  63. skb_scrub_packet(skb, xnet);
  64. skb_clear_hash(skb);
  65. skb_dst_set(skb, &rt->dst);
  66. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  67. if (skb_iif && !(df & htons(IP_DF))) {
  68. /* Arrived from an ingress interface, got encapsulated, with
  69. * fragmentation of encapulating frames allowed.
  70. * If skb is gso, the resulting encapsulated network segments
  71. * may exceed dst mtu.
  72. * Allow IP Fragmentation of segments.
  73. */
  74. IPCB(skb)->flags |= IPSKB_FRAG_SEGS;
  75. }
  76. /* Push down and install the IP header. */
  77. skb_push(skb, sizeof(struct iphdr));
  78. skb_reset_network_header(skb);
  79. iph = ip_hdr(skb);
  80. iph->version = 4;
  81. iph->ihl = sizeof(struct iphdr) >> 2;
  82. iph->frag_off = df;
  83. iph->protocol = proto;
  84. iph->tos = tos;
  85. iph->daddr = dst;
  86. iph->saddr = src;
  87. iph->ttl = ttl;
  88. __ip_select_ident(net, iph, skb_shinfo(skb)->gso_segs ?: 1);
  89. err = ip_local_out(net, sk, skb);
  90. if (unlikely(net_xmit_eval(err)))
  91. pkt_len = 0;
  92. iptunnel_xmit_stats(dev, pkt_len);
  93. }
  94. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  95. int __iptunnel_pull_header(struct sk_buff *skb, int hdr_len,
  96. __be16 inner_proto, bool raw_proto, bool xnet)
  97. {
  98. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  99. return -ENOMEM;
  100. skb_pull_rcsum(skb, hdr_len);
  101. if (!raw_proto && inner_proto == htons(ETH_P_TEB)) {
  102. struct ethhdr *eh;
  103. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  104. return -ENOMEM;
  105. eh = (struct ethhdr *)skb->data;
  106. if (likely(eth_proto_is_802_3(eh->h_proto)))
  107. skb->protocol = eh->h_proto;
  108. else
  109. skb->protocol = htons(ETH_P_802_2);
  110. } else {
  111. skb->protocol = inner_proto;
  112. }
  113. skb_clear_hash_if_not_l4(skb);
  114. skb->vlan_tci = 0;
  115. skb_set_queue_mapping(skb, 0);
  116. skb_scrub_packet(skb, xnet);
  117. return iptunnel_pull_offloads(skb);
  118. }
  119. EXPORT_SYMBOL_GPL(__iptunnel_pull_header);
  120. struct metadata_dst *iptunnel_metadata_reply(struct metadata_dst *md,
  121. gfp_t flags)
  122. {
  123. struct metadata_dst *res;
  124. struct ip_tunnel_info *dst, *src;
  125. if (!md || md->u.tun_info.mode & IP_TUNNEL_INFO_TX)
  126. return NULL;
  127. res = metadata_dst_alloc(0, flags);
  128. if (!res)
  129. return NULL;
  130. dst = &res->u.tun_info;
  131. src = &md->u.tun_info;
  132. dst->key.tun_id = src->key.tun_id;
  133. if (src->mode & IP_TUNNEL_INFO_IPV6)
  134. memcpy(&dst->key.u.ipv6.dst, &src->key.u.ipv6.src,
  135. sizeof(struct in6_addr));
  136. else
  137. dst->key.u.ipv4.dst = src->key.u.ipv4.src;
  138. dst->mode = src->mode | IP_TUNNEL_INFO_TX;
  139. return res;
  140. }
  141. EXPORT_SYMBOL_GPL(iptunnel_metadata_reply);
  142. int iptunnel_handle_offloads(struct sk_buff *skb,
  143. int gso_type_mask)
  144. {
  145. int err;
  146. if (likely(!skb->encapsulation)) {
  147. skb_reset_inner_headers(skb);
  148. skb->encapsulation = 1;
  149. }
  150. if (skb_is_gso(skb)) {
  151. err = skb_header_unclone(skb, GFP_ATOMIC);
  152. if (unlikely(err))
  153. return err;
  154. skb_shinfo(skb)->gso_type |= gso_type_mask;
  155. return 0;
  156. }
  157. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  158. skb->ip_summed = CHECKSUM_NONE;
  159. /* We clear encapsulation here to prevent badly-written
  160. * drivers potentially deciding to offload an inner checksum
  161. * if we set CHECKSUM_PARTIAL on the outer header.
  162. * This should go away when the drivers are all fixed.
  163. */
  164. skb->encapsulation = 0;
  165. }
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
  169. /* Often modified stats are per cpu, other are shared (netdev->stats) */
  170. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  171. struct rtnl_link_stats64 *tot)
  172. {
  173. int i;
  174. netdev_stats_to_stats64(tot, &dev->stats);
  175. for_each_possible_cpu(i) {
  176. const struct pcpu_sw_netstats *tstats =
  177. per_cpu_ptr(dev->tstats, i);
  178. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  179. unsigned int start;
  180. do {
  181. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  182. rx_packets = tstats->rx_packets;
  183. tx_packets = tstats->tx_packets;
  184. rx_bytes = tstats->rx_bytes;
  185. tx_bytes = tstats->tx_bytes;
  186. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  187. tot->rx_packets += rx_packets;
  188. tot->tx_packets += tx_packets;
  189. tot->rx_bytes += rx_bytes;
  190. tot->tx_bytes += tx_bytes;
  191. }
  192. return tot;
  193. }
  194. EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
  195. static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
  196. [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
  197. [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
  198. [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
  199. [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
  200. [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
  201. [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
  202. };
  203. static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr,
  204. unsigned int family, const void *cfg,
  205. struct lwtunnel_state **ts)
  206. {
  207. struct ip_tunnel_info *tun_info;
  208. struct lwtunnel_state *new_state;
  209. struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
  210. int err;
  211. err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy);
  212. if (err < 0)
  213. return err;
  214. new_state = lwtunnel_state_alloc(sizeof(*tun_info));
  215. if (!new_state)
  216. return -ENOMEM;
  217. new_state->type = LWTUNNEL_ENCAP_IP;
  218. tun_info = lwt_tun_info(new_state);
  219. if (tb[LWTUNNEL_IP_ID])
  220. tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP_ID]);
  221. if (tb[LWTUNNEL_IP_DST])
  222. tun_info->key.u.ipv4.dst = nla_get_in_addr(tb[LWTUNNEL_IP_DST]);
  223. if (tb[LWTUNNEL_IP_SRC])
  224. tun_info->key.u.ipv4.src = nla_get_in_addr(tb[LWTUNNEL_IP_SRC]);
  225. if (tb[LWTUNNEL_IP_TTL])
  226. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
  227. if (tb[LWTUNNEL_IP_TOS])
  228. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
  229. if (tb[LWTUNNEL_IP_FLAGS])
  230. tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP_FLAGS]);
  231. tun_info->mode = IP_TUNNEL_INFO_TX;
  232. tun_info->options_len = 0;
  233. *ts = new_state;
  234. return 0;
  235. }
  236. static int ip_tun_fill_encap_info(struct sk_buff *skb,
  237. struct lwtunnel_state *lwtstate)
  238. {
  239. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  240. if (nla_put_be64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id,
  241. LWTUNNEL_IP_PAD) ||
  242. nla_put_in_addr(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
  243. nla_put_in_addr(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
  244. nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
  245. nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
  246. nla_put_be16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags))
  247. return -ENOMEM;
  248. return 0;
  249. }
  250. static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  251. {
  252. return nla_total_size_64bit(8) /* LWTUNNEL_IP_ID */
  253. + nla_total_size(4) /* LWTUNNEL_IP_DST */
  254. + nla_total_size(4) /* LWTUNNEL_IP_SRC */
  255. + nla_total_size(1) /* LWTUNNEL_IP_TOS */
  256. + nla_total_size(1) /* LWTUNNEL_IP_TTL */
  257. + nla_total_size(2); /* LWTUNNEL_IP_FLAGS */
  258. }
  259. static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  260. {
  261. return memcmp(lwt_tun_info(a), lwt_tun_info(b),
  262. sizeof(struct ip_tunnel_info));
  263. }
  264. static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
  265. .build_state = ip_tun_build_state,
  266. .fill_encap = ip_tun_fill_encap_info,
  267. .get_encap_size = ip_tun_encap_nlsize,
  268. .cmp_encap = ip_tun_cmp_encap,
  269. };
  270. static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
  271. [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
  272. [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
  273. [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
  274. [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
  275. [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
  276. [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
  277. };
  278. static int ip6_tun_build_state(struct net_device *dev, struct nlattr *attr,
  279. unsigned int family, const void *cfg,
  280. struct lwtunnel_state **ts)
  281. {
  282. struct ip_tunnel_info *tun_info;
  283. struct lwtunnel_state *new_state;
  284. struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
  285. int err;
  286. err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy);
  287. if (err < 0)
  288. return err;
  289. new_state = lwtunnel_state_alloc(sizeof(*tun_info));
  290. if (!new_state)
  291. return -ENOMEM;
  292. new_state->type = LWTUNNEL_ENCAP_IP6;
  293. tun_info = lwt_tun_info(new_state);
  294. if (tb[LWTUNNEL_IP6_ID])
  295. tun_info->key.tun_id = nla_get_be64(tb[LWTUNNEL_IP6_ID]);
  296. if (tb[LWTUNNEL_IP6_DST])
  297. tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
  298. if (tb[LWTUNNEL_IP6_SRC])
  299. tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
  300. if (tb[LWTUNNEL_IP6_HOPLIMIT])
  301. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
  302. if (tb[LWTUNNEL_IP6_TC])
  303. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
  304. if (tb[LWTUNNEL_IP6_FLAGS])
  305. tun_info->key.tun_flags = nla_get_be16(tb[LWTUNNEL_IP6_FLAGS]);
  306. tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
  307. tun_info->options_len = 0;
  308. *ts = new_state;
  309. return 0;
  310. }
  311. static int ip6_tun_fill_encap_info(struct sk_buff *skb,
  312. struct lwtunnel_state *lwtstate)
  313. {
  314. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  315. if (nla_put_be64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id,
  316. LWTUNNEL_IP6_PAD) ||
  317. nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
  318. nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
  319. nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.tos) ||
  320. nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.ttl) ||
  321. nla_put_be16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags))
  322. return -ENOMEM;
  323. return 0;
  324. }
  325. static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  326. {
  327. return nla_total_size_64bit(8) /* LWTUNNEL_IP6_ID */
  328. + nla_total_size(16) /* LWTUNNEL_IP6_DST */
  329. + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
  330. + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
  331. + nla_total_size(1) /* LWTUNNEL_IP6_TC */
  332. + nla_total_size(2); /* LWTUNNEL_IP6_FLAGS */
  333. }
  334. static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
  335. .build_state = ip6_tun_build_state,
  336. .fill_encap = ip6_tun_fill_encap_info,
  337. .get_encap_size = ip6_tun_encap_nlsize,
  338. .cmp_encap = ip_tun_cmp_encap,
  339. };
  340. void __init ip_tunnel_core_init(void)
  341. {
  342. /* If you land here, make sure whether increasing ip_tunnel_info's
  343. * options_len is a reasonable choice with its usage in front ends
  344. * (f.e., it's part of flow keys, etc).
  345. */
  346. BUILD_BUG_ON(IP_TUNNEL_OPTS_MAX != 255);
  347. lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
  348. lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
  349. }
  350. struct static_key ip_tunnel_metadata_cnt = STATIC_KEY_INIT_FALSE;
  351. EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
  352. void ip_tunnel_need_metadata(void)
  353. {
  354. static_key_slow_inc(&ip_tunnel_metadata_cnt);
  355. }
  356. EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
  357. void ip_tunnel_unneed_metadata(void)
  358. {
  359. static_key_slow_dec(&ip_tunnel_metadata_cnt);
  360. }
  361. EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);