vport-gre.c 6.3 KB

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