vport-gre.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /* Called with rcu_read_lock and BH disabled. */
  98. static int gre_err(struct sk_buff *skb, u32 info,
  99. const struct tnl_ptk_info *tpi)
  100. {
  101. struct ovs_net *ovs_net;
  102. struct vport *vport;
  103. ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
  104. vport = rcu_dereference(ovs_net->vport_net.gre_vport);
  105. if (unlikely(!vport))
  106. return PACKET_REJECT;
  107. else
  108. return PACKET_RCVD;
  109. }
  110. static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
  111. {
  112. struct net *net = ovs_dp_get_net(vport->dp);
  113. struct flowi4 fl;
  114. struct rtable *rt;
  115. int min_headroom;
  116. int tunnel_hlen;
  117. __be16 df;
  118. int err;
  119. if (unlikely(!OVS_CB(skb)->tun_key)) {
  120. err = -EINVAL;
  121. goto error;
  122. }
  123. /* Route lookup */
  124. memset(&fl, 0, sizeof(fl));
  125. fl.daddr = OVS_CB(skb)->tun_key->ipv4_dst;
  126. fl.saddr = OVS_CB(skb)->tun_key->ipv4_src;
  127. fl.flowi4_tos = RT_TOS(OVS_CB(skb)->tun_key->ipv4_tos);
  128. fl.flowi4_mark = skb->mark;
  129. fl.flowi4_proto = IPPROTO_GRE;
  130. rt = ip_route_output_key(net, &fl);
  131. if (IS_ERR(rt))
  132. return PTR_ERR(rt);
  133. tunnel_hlen = ip_gre_calc_hlen(OVS_CB(skb)->tun_key->tun_flags);
  134. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  135. + tunnel_hlen + sizeof(struct iphdr)
  136. + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
  137. if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
  138. int head_delta = SKB_DATA_ALIGN(min_headroom -
  139. skb_headroom(skb) +
  140. 16);
  141. err = pskb_expand_head(skb, max_t(int, head_delta, 0),
  142. 0, GFP_ATOMIC);
  143. if (unlikely(err))
  144. goto err_free_rt;
  145. }
  146. if (vlan_tx_tag_present(skb)) {
  147. if (unlikely(!__vlan_put_tag(skb,
  148. skb->vlan_proto,
  149. vlan_tx_tag_get(skb)))) {
  150. err = -ENOMEM;
  151. goto err_free_rt;
  152. }
  153. skb->vlan_tci = 0;
  154. }
  155. /* Push Tunnel header. */
  156. skb = __build_header(skb, tunnel_hlen);
  157. if (unlikely(!skb)) {
  158. err = 0;
  159. goto err_free_rt;
  160. }
  161. df = OVS_CB(skb)->tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  162. htons(IP_DF) : 0;
  163. skb->ignore_df = 1;
  164. return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
  165. OVS_CB(skb)->tun_key->ipv4_dst, IPPROTO_GRE,
  166. OVS_CB(skb)->tun_key->ipv4_tos,
  167. OVS_CB(skb)->tun_key->ipv4_ttl, df, false);
  168. err_free_rt:
  169. ip_rt_put(rt);
  170. error:
  171. return err;
  172. }
  173. static struct gre_cisco_protocol gre_protocol = {
  174. .handler = gre_rcv,
  175. .err_handler = gre_err,
  176. .priority = 1,
  177. };
  178. static int gre_ports;
  179. static int gre_init(void)
  180. {
  181. int err;
  182. gre_ports++;
  183. if (gre_ports > 1)
  184. return 0;
  185. err = gre_cisco_register(&gre_protocol);
  186. if (err)
  187. pr_warn("cannot register gre protocol handler\n");
  188. return err;
  189. }
  190. static void gre_exit(void)
  191. {
  192. gre_ports--;
  193. if (gre_ports > 0)
  194. return;
  195. gre_cisco_unregister(&gre_protocol);
  196. }
  197. static const char *gre_get_name(const struct vport *vport)
  198. {
  199. return vport_priv(vport);
  200. }
  201. static struct vport *gre_create(const struct vport_parms *parms)
  202. {
  203. struct net *net = ovs_dp_get_net(parms->dp);
  204. struct ovs_net *ovs_net;
  205. struct vport *vport;
  206. int err;
  207. err = gre_init();
  208. if (err)
  209. return ERR_PTR(err);
  210. ovs_net = net_generic(net, ovs_net_id);
  211. if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
  212. vport = ERR_PTR(-EEXIST);
  213. goto error;
  214. }
  215. vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
  216. if (IS_ERR(vport))
  217. goto error;
  218. strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
  219. rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
  220. return vport;
  221. error:
  222. gre_exit();
  223. return vport;
  224. }
  225. static void gre_tnl_destroy(struct vport *vport)
  226. {
  227. struct net *net = ovs_dp_get_net(vport->dp);
  228. struct ovs_net *ovs_net;
  229. ovs_net = net_generic(net, ovs_net_id);
  230. RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
  231. ovs_vport_deferred_free(vport);
  232. gre_exit();
  233. }
  234. const struct vport_ops ovs_gre_vport_ops = {
  235. .type = OVS_VPORT_TYPE_GRE,
  236. .create = gre_create,
  237. .destroy = gre_tnl_destroy,
  238. .get_name = gre_get_name,
  239. .send = gre_tnl_send,
  240. };