offload.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * sctp_offload - GRO/GSO Offloading for SCTP
  3. *
  4. * Copyright (C) 2015, Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
  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. return sctp_compute_cksum(skb, skb_transport_offset(skb));
  35. }
  36. static struct sk_buff *sctp_gso_segment(struct sk_buff *skb,
  37. netdev_features_t features)
  38. {
  39. struct sk_buff *segs = ERR_PTR(-EINVAL);
  40. struct sctphdr *sh;
  41. sh = sctp_hdr(skb);
  42. if (!pskb_may_pull(skb, sizeof(*sh)))
  43. goto out;
  44. __skb_pull(skb, sizeof(*sh));
  45. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  46. /* Packet is from an untrusted source, reset gso_segs. */
  47. struct skb_shared_info *pinfo = skb_shinfo(skb);
  48. struct sk_buff *frag_iter;
  49. pinfo->gso_segs = 0;
  50. if (skb->len != skb->data_len) {
  51. /* Means we have chunks in here too */
  52. pinfo->gso_segs++;
  53. }
  54. skb_walk_frags(skb, frag_iter)
  55. pinfo->gso_segs++;
  56. segs = NULL;
  57. goto out;
  58. }
  59. segs = skb_segment(skb, features | NETIF_F_HW_CSUM | NETIF_F_SG);
  60. if (IS_ERR(segs))
  61. goto out;
  62. /* All that is left is update SCTP CRC if necessary */
  63. if (!(features & NETIF_F_SCTP_CRC)) {
  64. for (skb = segs; skb; skb = skb->next) {
  65. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  66. sh = sctp_hdr(skb);
  67. sh->checksum = sctp_gso_make_checksum(skb);
  68. }
  69. }
  70. }
  71. out:
  72. return segs;
  73. }
  74. static const struct net_offload sctp_offload = {
  75. .callbacks = {
  76. .gso_segment = sctp_gso_segment,
  77. },
  78. };
  79. static const struct net_offload sctp6_offload = {
  80. .callbacks = {
  81. .gso_segment = sctp_gso_segment,
  82. },
  83. };
  84. int __init sctp_offload_init(void)
  85. {
  86. int ret;
  87. ret = inet_add_offload(&sctp_offload, IPPROTO_SCTP);
  88. if (ret)
  89. goto out;
  90. ret = inet6_add_offload(&sctp6_offload, IPPROTO_SCTP);
  91. if (ret)
  92. goto ipv4;
  93. return ret;
  94. ipv4:
  95. inet_del_offload(&sctp_offload, IPPROTO_SCTP);
  96. out:
  97. return ret;
  98. }