vport-gre.c 6.7 KB

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