esp4_offload.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * IPV4 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/udp.h>
  29. static struct sk_buff **esp4_gro_receive(struct sk_buff **head,
  30. struct sk_buff *skb)
  31. {
  32. int offset = skb_gro_offset(skb);
  33. struct xfrm_offload *xo;
  34. struct xfrm_state *x;
  35. __be32 seq;
  36. __be32 spi;
  37. int err;
  38. skb_pull(skb, offset);
  39. if ((err = xfrm_parse_spi(skb, IPPROTO_ESP, &spi, &seq)) != 0)
  40. goto out;
  41. err = secpath_set(skb);
  42. if (err)
  43. goto out;
  44. if (skb->sp->len == XFRM_MAX_DEPTH)
  45. goto out;
  46. x = xfrm_state_lookup(dev_net(skb->dev), skb->mark,
  47. (xfrm_address_t *)&ip_hdr(skb)->daddr,
  48. spi, IPPROTO_ESP, AF_INET);
  49. if (!x)
  50. goto out;
  51. skb->sp->xvec[skb->sp->len++] = x;
  52. skb->sp->olen++;
  53. xo = xfrm_offload(skb);
  54. if (!xo) {
  55. xfrm_state_put(x);
  56. goto out;
  57. }
  58. xo->flags |= XFRM_GRO;
  59. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  60. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  61. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  62. XFRM_SPI_SKB_CB(skb)->seq = seq;
  63. /* We don't need to handle errors from xfrm_input, it does all
  64. * the error handling and frees the resources on error. */
  65. xfrm_input(skb, IPPROTO_ESP, spi, -2);
  66. return ERR_PTR(-EINPROGRESS);
  67. out:
  68. skb_push(skb, offset);
  69. NAPI_GRO_CB(skb)->same_flow = 0;
  70. NAPI_GRO_CB(skb)->flush = 1;
  71. return NULL;
  72. }
  73. static const struct net_offload esp4_offload = {
  74. .callbacks = {
  75. .gro_receive = esp4_gro_receive,
  76. },
  77. };
  78. static int __init esp4_offload_init(void)
  79. {
  80. return inet_add_offload(&esp4_offload, IPPROTO_ESP);
  81. }
  82. static void __exit esp4_offload_exit(void)
  83. {
  84. inet_del_offload(&esp4_offload, IPPROTO_ESP);
  85. }
  86. module_init(esp4_offload_init);
  87. module_exit(esp4_offload_exit);
  88. MODULE_LICENSE("GPL");
  89. MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");