vport-gre.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2007-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/if.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/ip.h>
  22. #include <linux/if_tunnel.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/in.h>
  25. #include <linux/in_route.h>
  26. #include <linux/inetdevice.h>
  27. #include <linux/jhash.h>
  28. #include <linux/list.h>
  29. #include <linux/kernel.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/rculist.h>
  32. #include <net/route.h>
  33. #include <net/xfrm.h>
  34. #include <net/icmp.h>
  35. #include <net/ip.h>
  36. #include <net/ip_tunnels.h>
  37. #include <net/gre.h>
  38. #include <net/net_namespace.h>
  39. #include <net/netns/generic.h>
  40. #include <net/protocol.h>
  41. #include "datapath.h"
  42. #include "vport.h"
  43. /* Returns the least-significant 32 bits of a __be64. */
  44. static __be32 be64_get_low32(__be64 x)
  45. {
  46. #ifdef __BIG_ENDIAN
  47. return (__force __be32)x;
  48. #else
  49. return (__force __be32)((__force u64)x >> 32);
  50. #endif
  51. }
  52. static __be16 filter_tnl_flags(__be16 flags)
  53. {
  54. return flags & (TUNNEL_CSUM | TUNNEL_KEY);
  55. }
  56. static struct sk_buff *__build_header(struct sk_buff *skb,
  57. int tunnel_hlen)
  58. {
  59. const struct ovs_key_ipv4_tunnel *tun_key = OVS_CB(skb)->tun_key;
  60. struct tnl_ptk_info tpi;
  61. skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
  62. if (IS_ERR(skb))
  63. return NULL;
  64. tpi.flags = filter_tnl_flags(tun_key->tun_flags);
  65. tpi.proto = htons(ETH_P_TEB);
  66. tpi.key = be64_get_low32(tun_key->tun_id);
  67. tpi.seq = 0;
  68. gre_build_header(skb, &tpi, tunnel_hlen);
  69. return skb;
  70. }
  71. static __be64 key_to_tunnel_id(__be32 key, __be32 seq)
  72. {
  73. #ifdef __BIG_ENDIAN
  74. return (__force __be64)((__force u64)seq << 32 | (__force u32)key);
  75. #else
  76. return (__force __be64)((__force u64)key << 32 | (__force u32)seq);
  77. #endif
  78. }
  79. /* Called with rcu_read_lock and BH disabled. */
  80. static int gre_rcv(struct sk_buff *skb,
  81. const struct tnl_ptk_info *tpi)
  82. {
  83. struct ovs_key_ipv4_tunnel tun_key;
  84. struct ovs_net *ovs_net;
  85. struct vport *vport;
  86. __be64 key;
  87. ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
  88. vport = rcu_dereference(ovs_net->vport_net.gre_vport);
  89. if (unlikely(!vport))
  90. return PACKET_REJECT;
  91. key = key_to_tunnel_id(tpi->key, tpi->seq);
  92. ovs_flow_tun_key_init(&tun_key, ip_hdr(skb), key,
  93. filter_tnl_flags(tpi->flags));
  94. ovs_vport_receive(vport, skb, &tun_key);
  95. return PACKET_RCVD;
  96. }
  97. static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
  98. {
  99. struct net *net = ovs_dp_get_net(vport->dp);
  100. struct flowi4 fl;
  101. struct rtable *rt;
  102. int min_headroom;
  103. int tunnel_hlen;
  104. __be16 df;
  105. int err;
  106. if (unlikely(!OVS_CB(skb)->tun_key)) {
  107. err = -EINVAL;
  108. goto error;
  109. }
  110. /* Route lookup */
  111. memset(&fl, 0, sizeof(fl));
  112. fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
  113. fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
  114. fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
  115. fl.flowi4_mark = skb->mark;
  116. fl.flowi4_proto = IPPROTO_GRE;
  117. rt = ip_route_output_key(net, &fl);
  118. if (IS_ERR(rt))
  119. return PTR_ERR(rt);
  120. tunnel_hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
  121. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  122. + tunnel_hlen + sizeof(struct iphdr)
  123. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  124. if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
  125. int head_delta = SKB_DATA_ALIGN(min_headroom -
  126. skb_headroom(skb) +
  127. 16);
  128. err = pskb_expand_head(skb, max_t(int, head_delta, 0),
  129. 0, GFP_ATOMIC);
  130. if (unlikely(err))
  131. goto err_free_rt;
  132. }
  133. if (vlan_tx_tag_present(skb)) {
  134. if (unlikely(!__vlan_put_tag(skb,
  135. skb->vlan_proto,
  136. vlan_tx_tag_get(skb)))) {
  137. err = -ENOMEM;
  138. goto err_free_rt;
  139. }
  140. skb->vlan_tci = 0;
  141. }
  142. /* Push Tunnel header. */
  143. skb = __build_header(skb, tunnel_hlen);
  144. if (unlikely(!skb)) {
  145. err = 0;
  146. goto err_free_rt;
  147. }
  148. df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  149. htons(IP_DF) : 0;
  150. skb->local_df = 1;
  151. return iptunnel_xmit(rt, skb, fl.saddr,
  152. OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
  153. OVS_CB(skb)->tun_key->ipv4_tos,
  154. OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
  155. err_free_rt:
  156. ip_rt_put(rt);
  157. error:
  158. return err;
  159. }
  160. static struct gre_cisco_protocol gre_protocol = {
  161. .handler = gre_rcv,
  162. .priority = 1,
  163. };
  164. static int gre_ports;
  165. static int gre_init(void)
  166. {
  167. int err;
  168. gre_ports++;
  169. if (gre_ports > 1)
  170. return 0;
  171. err = gre_cisco_register(&gre_protocol);
  172. if (err)
  173. pr_warn("cannot register gre protocol handler\n");
  174. return err;
  175. }
  176. static void gre_exit(void)
  177. {
  178. gre_ports--;
  179. if (gre_ports > 0)
  180. return;
  181. gre_cisco_unregister(&gre_protocol);
  182. }
  183. static const char *gre_get_name(const struct vport *vport)
  184. {
  185. return vport_priv(vport);
  186. }
  187. static struct vport *gre_create(const struct vport_parms *parms)
  188. {
  189. struct net *net = ovs_dp_get_net(parms->dp);
  190. struct ovs_net *ovs_net;
  191. struct vport *vport;
  192. int err;
  193. err = gre_init();
  194. if (err)
  195. return ERR_PTR(err);
  196. ovs_net = net_generic(net, ovs_net_id);
  197. if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
  198. vport = ERR_PTR(-EEXIST);
  199. goto error;
  200. }
  201. vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
  202. if (IS_ERR(vport))
  203. goto error;
  204. strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
  205. rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
  206. return vport;
  207. error:
  208. gre_exit();
  209. return vport;
  210. }
  211. static void gre_tnl_destroy(struct vport *vport)
  212. {
  213. struct net *net = ovs_dp_get_net(vport->dp);
  214. struct ovs_net *ovs_net;
  215. ovs_net = net_generic(net, ovs_net_id);
  216. rcu_assign_pointer(ovs_net->vport_net.gre_vport, NULL);
  217. ovs_vport_deferred_free(vport);
  218. gre_exit();
  219. }
  220. const struct vport_ops ovs_gre_vport_ops = {
  221. .type = OVS_VPORT_TYPE_GRE,
  222. .create = gre_create,
  223. .destroy = gre_tnl_destroy,
  224. .get_name = gre_get_name,
  225. .send = gre_tnl_send,
  226. };