offload.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * sctp_offload - GRO/GSO Offloading for SCTP
  3. *
  4. * Copyright (C) 2015, Marcelo Ricardo Leitner <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/kernel.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/socket.h>
  20. #include <linux/sctp.h>
  21. #include <linux/proc_fs.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/module.h>
  24. #include <linux/kfifo.h>
  25. #include <linux/time.h>
  26. #include <net/net_namespace.h>
  27. #include <linux/skbuff.h>
  28. #include <net/sctp/sctp.h>
  29. #include <net/sctp/checksum.h>
  30. #include <net/protocol.h>
  31. static __le32 sctp_gso_make_checksum(struct sk_buff *skb)
  32. {
  33. skb->ip_summed = CHECKSUM_NONE;
  34. gso_reset_checksum(skb, ~0);
  35. return sctp_compute_cksum(skb, skb_transport_offset(skb));
  36. }
  37. static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
  38. netdev_features_t features)
  39. {
  40. struct sk_buff *segs = ERR_PTR(-EINVAL);
  41. struct sctphdr *sh;
  42. if (!(skb_shinfo(skb)->gso_type & SKB_GSO_SCTP))
  43. goto out;
  44. sh = sctp_hdr(skb);
  45. if (!pskb_may_pull(skb, sizeof(*sh)))
  46. goto out;
  47. __skb_pull(skb, sizeof(*sh));
  48. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  49. /* Packet is from an untrusted source, reset gso_segs. */
  50. struct skb_shared_info *pinfo = skb_shinfo(skb);
  51. struct sk_buff *frag_iter;
  52. pinfo->gso_segs = 0;
  53. if (skb->len != skb->data_len) {
  54. /* Means we have chunks in here too */
  55. pinfo->gso_segs++;
  56. }
  57. skb_walk_frags(skb, frag_iter)
  58. pinfo->gso_segs++;
  59. segs = NULL;
  60. goto out;
  61. }
  62. segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
  63. if (IS_ERR(segs))
  64. goto out;
  65. /* All that is left is update SCTP CRC if necessary */
  66. if (!(features & NETIF_F_SCTP_CRC)) {
  67. for (skb = segs; skb; skb = skb->next) {
  68. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  69. sh = sctp_hdr(skb);
  70. sh->checksum = sctp_gso_make_checksum(skb);
  71. }
  72. }
  73. }
  74. out:
  75. return segs;
  76. }
  77. static const struct net_offload sctp_offload = {
  78. .callbacks = {
  79. .gso_segment = sctp_gso_segment,
  80. },
  81. };
  82. static const struct net_offload sctp6_offload = {
  83. .callbacks = {
  84. .gso_segment = sctp_gso_segment,
  85. },
  86. };
  87. int __init sctp_offload_init(void)
  88. {
  89. int ret;
  90. ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
  91. if (ret)
  92. goto out;
  93. ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
  94. if (ret)
  95. goto ipv4;
  96. return ret;
  97. ipv4:
  98. inet_del_offload(&sctp_offload, IPPROTO_SCTP);
  99. out:
  100. return ret;
  101. }