vport-gre.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/module.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/rculist.h>
  33. #include <net/route.h>
  34. #include <net/xfrm.h>
  35. #include <net/icmp.h>
  36. #include <net/ip.h>
  37. #include <net/ip_tunnels.h>
  38. #include <net/gre.h>
  39. #include <net/net_namespace.h>
  40. #include <net/netns/generic.h>
  41. #include <net/protocol.h>
  42. #include "datapath.h"
  43. #include "vport.h"
  44. static struct vport_ops ovs_gre_vport_ops;
  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. struct tnl_ptk_info tpi;
  62. const struct ovs_key_ipv4_tunnel *tun_key;
  63. tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
  64. skb = gre_handle_offloads(skb, !!(tun_key->tun_flags & TUNNEL_CSUM));
  65. if (IS_ERR(skb))
  66. return skb;
  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_tunnel_info tun_info;
  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_info_init(&tun_info, ip_hdr(skb), 0, 0, key,
  96. filter_tnl_flags(tpi->flags), NULL, 0);
  97. ovs_vport_receive(vport, skb, &tun_info);
  98. return PACKET_RCVD;
  99. }
  100. /* Called with rcu_read_lock and BH disabled. */
  101. static int gre_err(struct sk_buff *skb, u32 info,
  102. const struct tnl_ptk_info *tpi)
  103. {
  104. struct ovs_net *ovs_net;
  105. struct vport *vport;
  106. ovs_net = net_generic(dev_net(skb->dev), ovs_net_id);
  107. vport = rcu_dereference(ovs_net->vport_net.gre_vport);
  108. if (unlikely(!vport))
  109. return PACKET_REJECT;
  110. else
  111. return PACKET_RCVD;
  112. }
  113. static int gre_tnl_send(struct vport *vport, struct sk_buff *skb)
  114. {
  115. struct net *net = ovs_dp_get_net(vport->dp);
  116. const struct ovs_key_ipv4_tunnel *tun_key;
  117. struct flowi4 fl;
  118. struct rtable *rt;
  119. int min_headroom;
  120. int tunnel_hlen;
  121. __be16 df;
  122. int err;
  123. if (unlikely(!OVS_CB(skb)->egress_tun_info)) {
  124. err = -EINVAL;
  125. goto err_free_skb;
  126. }
  127. tun_key = &OVS_CB(skb)->egress_tun_info->tunnel;
  128. rt = ovs_tunnel_route_lookup(net, tun_key, skb->mark, &fl, IPPROTO_GRE);
  129. if (IS_ERR(rt)) {
  130. err = PTR_ERR(rt);
  131. goto err_free_skb;
  132. }
  133. tunnel_hlen = ip_gre_calc_hlen(tun_key->tun_flags);
  134. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  135. + tunnel_hlen + sizeof(struct iphdr)
  136. + (skb_vlan_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. skb = vlan_hwaccel_push_inside(skb);
  147. if (unlikely(!skb)) {
  148. err = -ENOMEM;
  149. goto err_free_rt;
  150. }
  151. /* Push Tunnel header. */
  152. skb = __build_header(skb, tunnel_hlen);
  153. if (IS_ERR(skb)) {
  154. err = PTR_ERR(skb);
  155. skb = NULL;
  156. goto err_free_rt;
  157. }
  158. df = tun_key->tun_flags & TUNNEL_DONT_FRAGMENT ?
  159. htons(IP_DF) : 0;
  160. skb->ignore_df = 1;
  161. return iptunnel_xmit(skb->sk, rt, skb, fl.saddr,
  162. tun_key->ipv4_dst, IPPROTO_GRE,
  163. tun_key->ipv4_tos, tun_key->ipv4_ttl, df, false);
  164. err_free_rt:
  165. ip_rt_put(rt);
  166. err_free_skb:
  167. kfree_skb(skb);
  168. return err;
  169. }
  170. static struct gre_cisco_protocol gre_protocol = {
  171. .handler = gre_rcv,
  172. .err_handler = gre_err,
  173. .priority = 1,
  174. };
  175. static int gre_ports;
  176. static int gre_init(void)
  177. {
  178. int err;
  179. gre_ports++;
  180. if (gre_ports > 1)
  181. return 0;
  182. err = gre_cisco_register(&gre_protocol);
  183. if (err)
  184. pr_warn("cannot register gre protocol handler\n");
  185. return err;
  186. }
  187. static void gre_exit(void)
  188. {
  189. gre_ports--;
  190. if (gre_ports > 0)
  191. return;
  192. gre_cisco_unregister(&gre_protocol);
  193. }
  194. static const char *gre_get_name(const struct vport *vport)
  195. {
  196. return vport_priv(vport);
  197. }
  198. static struct vport *gre_create(const struct vport_parms *parms)
  199. {
  200. struct net *net = ovs_dp_get_net(parms->dp);
  201. struct ovs_net *ovs_net;
  202. struct vport *vport;
  203. int err;
  204. err = gre_init();
  205. if (err)
  206. return ERR_PTR(err);
  207. ovs_net = net_generic(net, ovs_net_id);
  208. if (ovsl_dereference(ovs_net->vport_net.gre_vport)) {
  209. vport = ERR_PTR(-EEXIST);
  210. goto error;
  211. }
  212. vport = ovs_vport_alloc(IFNAMSIZ, &ovs_gre_vport_ops, parms);
  213. if (IS_ERR(vport))
  214. goto error;
  215. strncpy(vport_priv(vport), parms->name, IFNAMSIZ);
  216. rcu_assign_pointer(ovs_net->vport_net.gre_vport, vport);
  217. return vport;
  218. error:
  219. gre_exit();
  220. return vport;
  221. }
  222. static void gre_tnl_destroy(struct vport *vport)
  223. {
  224. struct net *net = ovs_dp_get_net(vport->dp);
  225. struct ovs_net *ovs_net;
  226. ovs_net = net_generic(net, ovs_net_id);
  227. RCU_INIT_POINTER(ovs_net->vport_net.gre_vport, NULL);
  228. ovs_vport_deferred_free(vport);
  229. gre_exit();
  230. }
  231. static int gre_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
  232. struct ovs_tunnel_info *egress_tun_info)
  233. {
  234. return ovs_tunnel_get_egress_info(egress_tun_info,
  235. ovs_dp_get_net(vport->dp),
  236. OVS_CB(skb)->egress_tun_info,
  237. IPPROTO_GRE, skb->mark, 0, 0);
  238. }
  239. static struct vport_ops ovs_gre_vport_ops = {
  240. .type = OVS_VPORT_TYPE_GRE,
  241. .create = gre_create,
  242. .destroy = gre_tnl_destroy,
  243. .get_name = gre_get_name,
  244. .send = gre_tnl_send,
  245. .get_egress_tun_info = gre_get_egress_tun_info,
  246. .owner = THIS_MODULE,
  247. };
  248. static int __init ovs_gre_tnl_init(void)
  249. {
  250. return ovs_vport_ops_register(&ovs_gre_vport_ops);
  251. }
  252. static void __exit ovs_gre_tnl_exit(void)
  253. {
  254. ovs_vport_ops_unregister(&ovs_gre_vport_ops);
  255. }
  256. module_init(ovs_gre_tnl_init);
  257. module_exit(ovs_gre_tnl_exit);
  258. MODULE_DESCRIPTION("OVS: GRE switching port");
  259. MODULE_LICENSE("GPL");
  260. MODULE_ALIAS("vport-type-3");