vport-gre.c 6.3 KB

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