ip_tunnel_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 <linux/static_key.h>
  34. #include <net/ip.h>
  35. #include <net/icmp.h>
  36. #include <net/protocol.h>
  37. #include <net/ip_tunnels.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. int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
  47. __be32 src, __be32 dst, __u8 proto,
  48. __u8 tos, __u8 ttl, __be16 df, bool xnet)
  49. {
  50. int pkt_len = skb->len;
  51. struct iphdr *iph;
  52. int err;
  53. skb_scrub_packet(skb, xnet);
  54. skb_clear_hash(skb);
  55. skb_dst_set(skb, &rt->dst);
  56. memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
  57. /* Push down and install the IP header. */
  58. skb_push(skb, sizeof(struct iphdr));
  59. skb_reset_network_header(skb);
  60. iph = ip_hdr(skb);
  61. iph->version = 4;
  62. iph->ihl = sizeof(struct iphdr) >> 2;
  63. iph->frag_off = df;
  64. iph->protocol = proto;
  65. iph->tos = tos;
  66. iph->daddr = dst;
  67. iph->saddr = src;
  68. iph->ttl = ttl;
  69. __ip_select_ident(dev_net(rt->dst.dev), iph,
  70. skb_shinfo(skb)->gso_segs ?: 1);
  71. err = ip_local_out_sk(sk, skb);
  72. if (unlikely(net_xmit_eval(err)))
  73. pkt_len = 0;
  74. return pkt_len;
  75. }
  76. EXPORT_SYMBOL_GPL(iptunnel_xmit);
  77. int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
  78. {
  79. if (unlikely(!pskb_may_pull(skb, hdr_len)))
  80. return -ENOMEM;
  81. skb_pull_rcsum(skb, hdr_len);
  82. if (inner_proto == htons(ETH_P_TEB)) {
  83. struct ethhdr *eh;
  84. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  85. return -ENOMEM;
  86. eh = (struct ethhdr *)skb->data;
  87. if (likely(eth_proto_is_802_3(eh->h_proto)))
  88. skb->protocol = eh->h_proto;
  89. else
  90. skb->protocol = htons(ETH_P_802_2);
  91. } else {
  92. skb->protocol = inner_proto;
  93. }
  94. nf_reset(skb);
  95. secpath_reset(skb);
  96. skb_clear_hash_if_not_l4(skb);
  97. skb_dst_drop(skb);
  98. skb->vlan_tci = 0;
  99. skb_set_queue_mapping(skb, 0);
  100. skb->pkt_type = PACKET_HOST;
  101. return 0;
  102. }
  103. EXPORT_SYMBOL_GPL(iptunnel_pull_header);
  104. struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
  105. bool csum_help,
  106. int gso_type_mask)
  107. {
  108. int err;
  109. if (likely(!skb->encapsulation)) {
  110. skb_reset_inner_headers(skb);
  111. skb->encapsulation = 1;
  112. }
  113. if (skb_is_gso(skb)) {
  114. err = skb_unclone(skb, GFP_ATOMIC);
  115. if (unlikely(err))
  116. goto error;
  117. skb_shinfo(skb)->gso_type |= gso_type_mask;
  118. return skb;
  119. }
  120. /* If packet is not gso and we are resolving any partial checksum,
  121. * clear encapsulation flag. This allows setting CHECKSUM_PARTIAL
  122. * on the outer header without confusing devices that implement
  123. * NETIF_F_IP_CSUM with encapsulation.
  124. */
  125. if (csum_help)
  126. skb->encapsulation = 0;
  127. if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
  128. err = skb_checksum_help(skb);
  129. if (unlikely(err))
  130. goto error;
  131. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  132. skb->ip_summed = CHECKSUM_NONE;
  133. return skb;
  134. error:
  135. kfree_skb(skb);
  136. return ERR_PTR(err);
  137. }
  138. EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
  139. /* Often modified stats are per cpu, other are shared (netdev->stats) */
  140. struct rtnl_link_stats64 *ip_tunnel_get_stats64(struct net_device *dev,
  141. struct rtnl_link_stats64 *tot)
  142. {
  143. int i;
  144. netdev_stats_to_stats64(tot, &dev->stats);
  145. for_each_possible_cpu(i) {
  146. const struct pcpu_sw_netstats *tstats =
  147. per_cpu_ptr(dev->tstats, i);
  148. u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
  149. unsigned int start;
  150. do {
  151. start = u64_stats_fetch_begin_irq(&tstats->syncp);
  152. rx_packets = tstats->rx_packets;
  153. tx_packets = tstats->tx_packets;
  154. rx_bytes = tstats->rx_bytes;
  155. tx_bytes = tstats->tx_bytes;
  156. } while (u64_stats_fetch_retry_irq(&tstats->syncp, start));
  157. tot->rx_packets += rx_packets;
  158. tot->tx_packets += tx_packets;
  159. tot->rx_bytes += rx_bytes;
  160. tot->tx_bytes += tx_bytes;
  161. }
  162. return tot;
  163. }
  164. EXPORT_SYMBOL_GPL(ip_tunnel_get_stats64);
  165. static const struct nla_policy ip_tun_policy[LWTUNNEL_IP_MAX + 1] = {
  166. [LWTUNNEL_IP_ID] = { .type = NLA_U64 },
  167. [LWTUNNEL_IP_DST] = { .type = NLA_U32 },
  168. [LWTUNNEL_IP_SRC] = { .type = NLA_U32 },
  169. [LWTUNNEL_IP_TTL] = { .type = NLA_U8 },
  170. [LWTUNNEL_IP_TOS] = { .type = NLA_U8 },
  171. [LWTUNNEL_IP_SPORT] = { .type = NLA_U16 },
  172. [LWTUNNEL_IP_DPORT] = { .type = NLA_U16 },
  173. [LWTUNNEL_IP_FLAGS] = { .type = NLA_U16 },
  174. };
  175. static int ip_tun_build_state(struct net_device *dev, struct nlattr *attr,
  176. unsigned int family, const void *cfg,
  177. struct lwtunnel_state **ts)
  178. {
  179. struct ip_tunnel_info *tun_info;
  180. struct lwtunnel_state *new_state;
  181. struct nlattr *tb[LWTUNNEL_IP_MAX + 1];
  182. int err;
  183. err = nla_parse_nested(tb, LWTUNNEL_IP_MAX, attr, ip_tun_policy);
  184. if (err < 0)
  185. return err;
  186. new_state = lwtunnel_state_alloc(sizeof(*tun_info));
  187. if (!new_state)
  188. return -ENOMEM;
  189. new_state->type = LWTUNNEL_ENCAP_IP;
  190. tun_info = lwt_tun_info(new_state);
  191. if (tb[LWTUNNEL_IP_ID])
  192. tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP_ID]);
  193. if (tb[LWTUNNEL_IP_DST])
  194. tun_info->key.u.ipv4.dst = nla_get_be32(tb[LWTUNNEL_IP_DST]);
  195. if (tb[LWTUNNEL_IP_SRC])
  196. tun_info->key.u.ipv4.src = nla_get_be32(tb[LWTUNNEL_IP_SRC]);
  197. if (tb[LWTUNNEL_IP_TTL])
  198. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP_TTL]);
  199. if (tb[LWTUNNEL_IP_TOS])
  200. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP_TOS]);
  201. if (tb[LWTUNNEL_IP_SPORT])
  202. tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP_SPORT]);
  203. if (tb[LWTUNNEL_IP_DPORT])
  204. tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP_DPORT]);
  205. if (tb[LWTUNNEL_IP_FLAGS])
  206. tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP_FLAGS]);
  207. tun_info->mode = IP_TUNNEL_INFO_TX;
  208. tun_info->options_len = 0;
  209. *ts = new_state;
  210. return 0;
  211. }
  212. static int ip_tun_fill_encap_info(struct sk_buff *skb,
  213. struct lwtunnel_state *lwtstate)
  214. {
  215. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  216. if (nla_put_u64(skb, LWTUNNEL_IP_ID, tun_info->key.tun_id) ||
  217. nla_put_be32(skb, LWTUNNEL_IP_DST, tun_info->key.u.ipv4.dst) ||
  218. nla_put_be32(skb, LWTUNNEL_IP_SRC, tun_info->key.u.ipv4.src) ||
  219. nla_put_u8(skb, LWTUNNEL_IP_TOS, tun_info->key.tos) ||
  220. nla_put_u8(skb, LWTUNNEL_IP_TTL, tun_info->key.ttl) ||
  221. nla_put_u16(skb, LWTUNNEL_IP_SPORT, tun_info->key.tp_src) ||
  222. nla_put_u16(skb, LWTUNNEL_IP_DPORT, tun_info->key.tp_dst) ||
  223. nla_put_u16(skb, LWTUNNEL_IP_FLAGS, tun_info->key.tun_flags))
  224. return -ENOMEM;
  225. return 0;
  226. }
  227. static int ip_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  228. {
  229. return nla_total_size(8) /* LWTUNNEL_IP_ID */
  230. + nla_total_size(4) /* LWTUNNEL_IP_DST */
  231. + nla_total_size(4) /* LWTUNNEL_IP_SRC */
  232. + nla_total_size(1) /* LWTUNNEL_IP_TOS */
  233. + nla_total_size(1) /* LWTUNNEL_IP_TTL */
  234. + nla_total_size(2) /* LWTUNNEL_IP_SPORT */
  235. + nla_total_size(2) /* LWTUNNEL_IP_DPORT */
  236. + nla_total_size(2); /* LWTUNNEL_IP_FLAGS */
  237. }
  238. static int ip_tun_cmp_encap(struct lwtunnel_state *a, struct lwtunnel_state *b)
  239. {
  240. return memcmp(lwt_tun_info(a), lwt_tun_info(b),
  241. sizeof(struct ip_tunnel_info));
  242. }
  243. static const struct lwtunnel_encap_ops ip_tun_lwt_ops = {
  244. .build_state = ip_tun_build_state,
  245. .fill_encap = ip_tun_fill_encap_info,
  246. .get_encap_size = ip_tun_encap_nlsize,
  247. .cmp_encap = ip_tun_cmp_encap,
  248. };
  249. static const struct nla_policy ip6_tun_policy[LWTUNNEL_IP6_MAX + 1] = {
  250. [LWTUNNEL_IP6_ID] = { .type = NLA_U64 },
  251. [LWTUNNEL_IP6_DST] = { .len = sizeof(struct in6_addr) },
  252. [LWTUNNEL_IP6_SRC] = { .len = sizeof(struct in6_addr) },
  253. [LWTUNNEL_IP6_HOPLIMIT] = { .type = NLA_U8 },
  254. [LWTUNNEL_IP6_TC] = { .type = NLA_U8 },
  255. [LWTUNNEL_IP6_SPORT] = { .type = NLA_U16 },
  256. [LWTUNNEL_IP6_DPORT] = { .type = NLA_U16 },
  257. [LWTUNNEL_IP6_FLAGS] = { .type = NLA_U16 },
  258. };
  259. static int ip6_tun_build_state(struct net_device *dev, struct nlattr *attr,
  260. unsigned int family, const void *cfg,
  261. struct lwtunnel_state **ts)
  262. {
  263. struct ip_tunnel_info *tun_info;
  264. struct lwtunnel_state *new_state;
  265. struct nlattr *tb[LWTUNNEL_IP6_MAX + 1];
  266. int err;
  267. err = nla_parse_nested(tb, LWTUNNEL_IP6_MAX, attr, ip6_tun_policy);
  268. if (err < 0)
  269. return err;
  270. new_state = lwtunnel_state_alloc(sizeof(*tun_info));
  271. if (!new_state)
  272. return -ENOMEM;
  273. new_state->type = LWTUNNEL_ENCAP_IP6;
  274. tun_info = lwt_tun_info(new_state);
  275. if (tb[LWTUNNEL_IP6_ID])
  276. tun_info->key.tun_id = nla_get_u64(tb[LWTUNNEL_IP6_ID]);
  277. if (tb[LWTUNNEL_IP6_DST])
  278. tun_info->key.u.ipv6.dst = nla_get_in6_addr(tb[LWTUNNEL_IP6_DST]);
  279. if (tb[LWTUNNEL_IP6_SRC])
  280. tun_info->key.u.ipv6.src = nla_get_in6_addr(tb[LWTUNNEL_IP6_SRC]);
  281. if (tb[LWTUNNEL_IP6_HOPLIMIT])
  282. tun_info->key.ttl = nla_get_u8(tb[LWTUNNEL_IP6_HOPLIMIT]);
  283. if (tb[LWTUNNEL_IP6_TC])
  284. tun_info->key.tos = nla_get_u8(tb[LWTUNNEL_IP6_TC]);
  285. if (tb[LWTUNNEL_IP6_SPORT])
  286. tun_info->key.tp_src = nla_get_be16(tb[LWTUNNEL_IP6_SPORT]);
  287. if (tb[LWTUNNEL_IP6_DPORT])
  288. tun_info->key.tp_dst = nla_get_be16(tb[LWTUNNEL_IP6_DPORT]);
  289. if (tb[LWTUNNEL_IP6_FLAGS])
  290. tun_info->key.tun_flags = nla_get_u16(tb[LWTUNNEL_IP6_FLAGS]);
  291. tun_info->mode = IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_IPV6;
  292. tun_info->options_len = 0;
  293. *ts = new_state;
  294. return 0;
  295. }
  296. static int ip6_tun_fill_encap_info(struct sk_buff *skb,
  297. struct lwtunnel_state *lwtstate)
  298. {
  299. struct ip_tunnel_info *tun_info = lwt_tun_info(lwtstate);
  300. if (nla_put_u64(skb, LWTUNNEL_IP6_ID, tun_info->key.tun_id) ||
  301. nla_put_in6_addr(skb, LWTUNNEL_IP6_DST, &tun_info->key.u.ipv6.dst) ||
  302. nla_put_in6_addr(skb, LWTUNNEL_IP6_SRC, &tun_info->key.u.ipv6.src) ||
  303. nla_put_u8(skb, LWTUNNEL_IP6_HOPLIMIT, tun_info->key.tos) ||
  304. nla_put_u8(skb, LWTUNNEL_IP6_TC, tun_info->key.ttl) ||
  305. nla_put_u16(skb, LWTUNNEL_IP6_SPORT, tun_info->key.tp_src) ||
  306. nla_put_u16(skb, LWTUNNEL_IP6_DPORT, tun_info->key.tp_dst) ||
  307. nla_put_u16(skb, LWTUNNEL_IP6_FLAGS, tun_info->key.tun_flags))
  308. return -ENOMEM;
  309. return 0;
  310. }
  311. static int ip6_tun_encap_nlsize(struct lwtunnel_state *lwtstate)
  312. {
  313. return nla_total_size(8) /* LWTUNNEL_IP6_ID */
  314. + nla_total_size(16) /* LWTUNNEL_IP6_DST */
  315. + nla_total_size(16) /* LWTUNNEL_IP6_SRC */
  316. + nla_total_size(1) /* LWTUNNEL_IP6_HOPLIMIT */
  317. + nla_total_size(1) /* LWTUNNEL_IP6_TC */
  318. + nla_total_size(2) /* LWTUNNEL_IP6_SPORT */
  319. + nla_total_size(2) /* LWTUNNEL_IP6_DPORT */
  320. + nla_total_size(2); /* LWTUNNEL_IP6_FLAGS */
  321. }
  322. static const struct lwtunnel_encap_ops ip6_tun_lwt_ops = {
  323. .build_state = ip6_tun_build_state,
  324. .fill_encap = ip6_tun_fill_encap_info,
  325. .get_encap_size = ip6_tun_encap_nlsize,
  326. .cmp_encap = ip_tun_cmp_encap,
  327. };
  328. void __init ip_tunnel_core_init(void)
  329. {
  330. lwtunnel_encap_add_ops(&ip_tun_lwt_ops, LWTUNNEL_ENCAP_IP);
  331. lwtunnel_encap_add_ops(&ip6_tun_lwt_ops, LWTUNNEL_ENCAP_IP6);
  332. }
  333. struct static_key ip_tunnel_metadata_cnt = STATIC_KEY_INIT_FALSE;
  334. EXPORT_SYMBOL(ip_tunnel_metadata_cnt);
  335. void ip_tunnel_need_metadata(void)
  336. {
  337. static_key_slow_inc(&ip_tunnel_metadata_cnt);
  338. }
  339. EXPORT_SYMBOL_GPL(ip_tunnel_need_metadata);
  340. void ip_tunnel_unneed_metadata(void)
  341. {
  342. static_key_slow_dec(&ip_tunnel_metadata_cnt);
  343. }
  344. EXPORT_SYMBOL_GPL(ip_tunnel_unneed_metadata);