esp6_offload.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * IPV6 GSO/GRO offload support
  3. * Linux INET implementation
  4. *
  5. * Copyright (C) 2016 secunet Security Networks AG
  6. * Author: Steffen Klassert <steffen.klassert@secunet.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * ESP GRO support
  13. */
  14. #include <linux/skbuff.h>
  15. #include <linux/init.h>
  16. #include <net/protocol.h>
  17. #include <crypto/aead.h>
  18. #include <crypto/authenc.h>
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include <net/ip.h>
  22. #include <net/xfrm.h>
  23. #include <net/esp.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/spinlock.h>
  28. #include <net/ip6_route.h>
  29. #include <net/ipv6.h>
  30. #include <linux/icmpv6.h>
  31. static __u16 esp6_nexthdr_esp_offset(struct ipv6hdr *ipv6_hdr, int nhlen)
  32. {
  33. int off = sizeof(struct ipv6hdr);
  34. struct ipv6_opt_hdr *exthdr;
  35. if (likely(ipv6_hdr->nexthdr == NEXTHDR_ESP))
  36. return offsetof(struct ipv6hdr, nexthdr);
  37. while (off < nhlen) {
  38. exthdr = (void *)ipv6_hdr + off;
  39. if (exthdr->nexthdr == NEXTHDR_ESP)
  40. return off;
  41. off += ipv6_optlen(exthdr);
  42. }
  43. return 0;
  44. }
  45. static struct sk_buff *esp6_gro_receive(struct list_head *head,
  46. struct sk_buff *skb)
  47. {
  48. int offset = skb_gro_offset(skb);
  49. struct xfrm_offload *xo;
  50. struct xfrm_state *x;
  51. __be32 seq;
  52. __be32 spi;
  53. int nhoff;
  54. int err;
  55. if (!pskb_pull(skb, offset))
  56. return NULL;
  57. if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
  58. goto out;
  59. xo = xfrm_offload(skb);
  60. if (!xo || !(xo->flags & CRYPTO_DONE)) {
  61. err = secpath_set(skb);
  62. if (err)
  63. goto out;
  64. if (skb->sp->len == XFRM_MAX_DEPTH)
  65. goto out;
  66. x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
  67. (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
  68. spi, IPPROTO_ESP, AF_INET6);
  69. if (!x)
  70. goto out;
  71. skb->sp->xvec[skb->sp->len++] = x;
  72. skb->sp->olen++;
  73. xo = xfrm_offload(skb);
  74. if (!xo) {
  75. xfrm_state_put(x);
  76. goto out;
  77. }
  78. }
  79. xo->flags |= XFRM_GRO;
  80. nhoff = esp6_nexthdr_esp_offset(ipv6_hdr(skb), offset);
  81. if (!nhoff)
  82. goto out;
  83. IP6CB(skb)->nhoff = nhoff;
  84. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6 = NULL;
  85. XFRM_SPI_SKB_CB(skb)->family = AF_INET6;
  86. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct ipv6hdr, daddr);
  87. XFRM_SPI_SKB_CB(skb)->seq = seq;
  88. /* We don't need to handle errors from xfrm_input, it does all
  89. * the error handling and frees the resources on error. */
  90. xfrm_input(skb, IPPROTO_ESP, spi, -2);
  91. return ERR_PTR(-EINPROGRESS);
  92. out:
  93. skb_push(skb, offset);
  94. NAPI_GRO_CB(skb)->same_flow = 0;
  95. NAPI_GRO_CB(skb)->flush = 1;
  96. return NULL;
  97. }
  98. static void esp6_gso_encap(struct xfrm_state *x, struct sk_buff *skb)
  99. {
  100. struct ip_esp_hdr *esph;
  101. struct ipv6hdr *iph = ipv6_hdr(skb);
  102. struct xfrm_offload *xo = xfrm_offload(skb);
  103. int proto = iph->nexthdr;
  104. skb_push(skb, -skb_network_offset(skb));
  105. esph = ip_esp_hdr(skb);
  106. *skb_mac_header(skb) = IPPROTO_ESP;
  107. esph->spi = x->id.spi;
  108. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  109. xo->proto = proto;
  110. }
  111. static struct sk_buff *esp6_gso_segment(struct sk_buff *skb,
  112. netdev_features_t features)
  113. {
  114. struct xfrm_state *x;
  115. struct ip_esp_hdr *esph;
  116. struct crypto_aead *aead;
  117. netdev_features_t esp_features = features;
  118. struct xfrm_offload *xo = xfrm_offload(skb);
  119. if (!xo)
  120. return ERR_PTR(-EINVAL);
  121. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_ESP))
  122. return ERR_PTR(-EINVAL);
  123. x = skb->sp->xvec[skb->sp->len - 1];
  124. aead = x->data;
  125. esph = ip_esp_hdr(skb);
  126. if (esph->spi != x->id.spi)
  127. return ERR_PTR(-EINVAL);
  128. if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
  129. return ERR_PTR(-EINVAL);
  130. __skb_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead));
  131. skb->encap_hdr_csum = 1;
  132. if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev)
  133. esp_features = features & ~(NETIF_F_SG | NETIF_F_CSUM_MASK);
  134. else if (!(features & NETIF_F_HW_ESP_TX_CSUM))
  135. esp_features = features & ~NETIF_F_CSUM_MASK;
  136. xo->flags |= XFRM_GSO_SEGMENT;
  137. return x->outer_mode->gso_segment(x, skb, esp_features);
  138. }
  139. static int esp6_input_tail(struct xfrm_state *x, struct sk_buff *skb)
  140. {
  141. struct crypto_aead *aead = x->data;
  142. struct xfrm_offload *xo = xfrm_offload(skb);
  143. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead)))
  144. return -EINVAL;
  145. if (!(xo->flags & CRYPTO_DONE))
  146. skb->ip_summed = CHECKSUM_NONE;
  147. return esp6_input_done2(skb, 0);
  148. }
  149. static int esp6_xmit(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features)
  150. {
  151. int len;
  152. int err;
  153. int alen;
  154. int blksize;
  155. struct xfrm_offload *xo;
  156. struct ip_esp_hdr *esph;
  157. struct crypto_aead *aead;
  158. struct esp_info esp;
  159. bool hw_offload = true;
  160. __u32 seq;
  161. esp.inplace = true;
  162. xo = xfrm_offload(skb);
  163. if (!xo)
  164. return -EINVAL;
  165. if (!(features & NETIF_F_HW_ESP) || x->xso.dev != skb->dev) {
  166. xo->flags |= CRYPTO_FALLBACK;
  167. hw_offload = false;
  168. }
  169. esp.proto = xo->proto;
  170. /* skb is pure payload to encrypt */
  171. aead = x->data;
  172. alen = crypto_aead_authsize(aead);
  173. esp.tfclen = 0;
  174. /* XXX: Add support for tfc padding here. */
  175. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  176. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  177. esp.plen = esp.clen - skb->len - esp.tfclen;
  178. esp.tailen = esp.tfclen + esp.plen + alen;
  179. if (!hw_offload || (hw_offload && !skb_is_gso(skb))) {
  180. esp.nfrags = esp6_output_head(x, skb, &esp);
  181. if (esp.nfrags < 0)
  182. return esp.nfrags;
  183. }
  184. seq = xo->seq.low;
  185. esph = ip_esp_hdr(skb);
  186. esph->spi = x->id.spi;
  187. skb_push(skb, -skb_network_offset(skb));
  188. if (xo->flags & XFRM_GSO_SEGMENT) {
  189. esph->seq_no = htonl(seq);
  190. if (!skb_is_gso(skb))
  191. xo->seq.low++;
  192. else
  193. xo->seq.low += skb_shinfo(skb)->gso_segs;
  194. }
  195. esp.seqno = cpu_to_be64(xo->seq.low + ((u64)xo->seq.hi << 32));
  196. len = skb->len - sizeof(struct ipv6hdr);
  197. if (len > IPV6_MAXPLEN)
  198. len = 0;
  199. ipv6_hdr(skb)->payload_len = htons(len);
  200. if (hw_offload)
  201. return 0;
  202. err = esp6_output_tail(x, skb, &esp);
  203. if (err)
  204. return err;
  205. secpath_reset(skb);
  206. return 0;
  207. }
  208. static const struct net_offload esp6_offload = {
  209. .callbacks = {
  210. .gro_receive = esp6_gro_receive,
  211. .gso_segment = esp6_gso_segment,
  212. },
  213. };
  214. static const struct xfrm_type_offload esp6_type_offload = {
  215. .description = "ESP6 OFFLOAD",
  216. .owner = THIS_MODULE,
  217. .proto = IPPROTO_ESP,
  218. .input_tail = esp6_input_tail,
  219. .xmit = esp6_xmit,
  220. .encap = esp6_gso_encap,
  221. };
  222. static int __init esp6_offload_init(void)
  223. {
  224. if (xfrm_register_type_offload(&esp6_type_offload, AF_INET6) < 0) {
  225. pr_info("%s: can't add xfrm type offload\n", __func__);
  226. return -EAGAIN;
  227. }
  228. return inet6_add_offload(&esp6_offload, IPPROTO_ESP);
  229. }
  230. static void __exit esp6_offload_exit(void)
  231. {
  232. if (xfrm_unregister_type_offload(&esp6_type_offload, AF_INET6) < 0)
  233. pr_info("%s: can't remove xfrm type offload\n", __func__);
  234. inet6_del_offload(&esp6_offload, IPPROTO_ESP);
  235. }
  236. module_init(esp6_offload_init);
  237. module_exit(esp6_offload_exit);
  238. MODULE_LICENSE("GPL");
  239. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
  240. MODULE_ALIAS_XFRM_OFFLOAD_TYPE(AF_INET6, XFRM_PROTO_ESP);