xfrm_device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * xfrm_device.c - IPsec device offloading code.
  3. *
  4. * Copyright (c) 2015 secunet Security Networks AG
  5. *
  6. * Author:
  7. * Steffen Klassert <steffen.klassert@secunet.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <net/dst.h>
  21. #include <net/xfrm.h>
  22. #include <linux/notifier.h>
  23. #ifdef CONFIG_XFRM_OFFLOAD
  24. int validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features)
  25. {
  26. int err;
  27. struct xfrm_state *x;
  28. struct xfrm_offload *xo = xfrm_offload(skb);
  29. if (skb_is_gso(skb))
  30. return 0;
  31. if (xo) {
  32. x = skb->sp->xvec[skb->sp->len - 1];
  33. if (xo->flags & XFRM_GRO || x->xso.flags & XFRM_OFFLOAD_INBOUND)
  34. return 0;
  35. x->outer_mode->xmit(x, skb);
  36. err = x->type_offload->xmit(x, skb, features);
  37. if (err) {
  38. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  39. return err;
  40. }
  41. skb_push(skb, skb->data - skb_mac_header(skb));
  42. }
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(validate_xmit_xfrm);
  46. int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
  47. struct xfrm_user_offload *xuo)
  48. {
  49. int err;
  50. struct dst_entry *dst;
  51. struct net_device *dev;
  52. struct xfrm_state_offload *xso = &x->xso;
  53. xfrm_address_t *saddr;
  54. xfrm_address_t *daddr;
  55. if (!x->type_offload)
  56. return -EINVAL;
  57. /* We don't yet support UDP encapsulation, TFC padding and ESN. */
  58. if (x->encap || x->tfcpad || (x->props.flags & XFRM_STATE_ESN))
  59. return 0;
  60. dev = dev_get_by_index(net, xuo->ifindex);
  61. if (!dev) {
  62. if (!(xuo->flags & XFRM_OFFLOAD_INBOUND)) {
  63. saddr = &x->props.saddr;
  64. daddr = &x->id.daddr;
  65. } else {
  66. saddr = &x->id.daddr;
  67. daddr = &x->props.saddr;
  68. }
  69. dst = __xfrm_dst_lookup(net, 0, 0, saddr, daddr,
  70. x->props.family, x->props.output_mark);
  71. if (IS_ERR(dst))
  72. return 0;
  73. dev = dst->dev;
  74. dev_hold(dev);
  75. dst_release(dst);
  76. }
  77. if (!dev->xfrmdev_ops || !dev->xfrmdev_ops->xdo_dev_state_add) {
  78. xso->dev = NULL;
  79. dev_put(dev);
  80. return 0;
  81. }
  82. xso->dev = dev;
  83. xso->num_exthdrs = 1;
  84. xso->flags = xuo->flags;
  85. err = dev->xfrmdev_ops->xdo_dev_state_add(x);
  86. if (err) {
  87. dev_put(dev);
  88. return err;
  89. }
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(xfrm_dev_state_add);
  93. bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
  94. {
  95. int mtu;
  96. struct dst_entry *dst = skb_dst(skb);
  97. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  98. struct net_device *dev = x->xso.dev;
  99. if (!x->type_offload || x->encap)
  100. return false;
  101. if ((x->xso.offload_handle && (dev == dst->path->dev)) &&
  102. !dst->child->xfrm && x->type->get_mtu) {
  103. mtu = x->type->get_mtu(x, xdst->child_mtu_cached);
  104. if (skb->len <= mtu)
  105. goto ok;
  106. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  107. goto ok;
  108. }
  109. return false;
  110. ok:
  111. if (dev && dev->xfrmdev_ops && dev->xfrmdev_ops->xdo_dev_offload_ok)
  112. return x->xso.dev->xfrmdev_ops->xdo_dev_offload_ok(skb, x);
  113. return true;
  114. }
  115. EXPORT_SYMBOL_GPL(xfrm_dev_offload_ok);
  116. #endif
  117. static int xfrm_dev_register(struct net_device *dev)
  118. {
  119. if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
  120. return NOTIFY_BAD;
  121. if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
  122. !(dev->features & NETIF_F_HW_ESP))
  123. return NOTIFY_BAD;
  124. return NOTIFY_DONE;
  125. }
  126. static int xfrm_dev_unregister(struct net_device *dev)
  127. {
  128. xfrm_policy_cache_flush();
  129. return NOTIFY_DONE;
  130. }
  131. static int xfrm_dev_feat_change(struct net_device *dev)
  132. {
  133. if ((dev->features & NETIF_F_HW_ESP) && !dev->xfrmdev_ops)
  134. return NOTIFY_BAD;
  135. else if (!(dev->features & NETIF_F_HW_ESP))
  136. dev->xfrmdev_ops = NULL;
  137. if ((dev->features & NETIF_F_HW_ESP_TX_CSUM) &&
  138. !(dev->features & NETIF_F_HW_ESP))
  139. return NOTIFY_BAD;
  140. return NOTIFY_DONE;
  141. }
  142. static int xfrm_dev_down(struct net_device *dev)
  143. {
  144. if (dev->features & NETIF_F_HW_ESP)
  145. xfrm_dev_state_flush(dev_net(dev), dev, true);
  146. xfrm_policy_cache_flush();
  147. return NOTIFY_DONE;
  148. }
  149. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  150. {
  151. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  152. switch (event) {
  153. case NETDEV_REGISTER:
  154. return xfrm_dev_register(dev);
  155. case NETDEV_UNREGISTER:
  156. return xfrm_dev_unregister(dev);
  157. case NETDEV_FEAT_CHANGE:
  158. return xfrm_dev_feat_change(dev);
  159. case NETDEV_DOWN:
  160. return xfrm_dev_down(dev);
  161. }
  162. return NOTIFY_DONE;
  163. }
  164. static struct notifier_block xfrm_dev_notifier = {
  165. .notifier_call = xfrm_dev_event,
  166. };
  167. void __net_init xfrm_dev_init(void)
  168. {
  169. register_netdevice_notifier(&xfrm_dev_notifier);
  170. }