udp_offload.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * IPV4 GSO/GRO offload support
  3. * Linux INET implementation
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the License, or (at your option) any later version.
  9. *
  10. * UDPv4 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <net/udp.h>
  14. #include <net/protocol.h>
  15. static int udp4_ufo_send_check(struct sk_buff *skb)
  16. {
  17. if (!pskb_may_pull(skb, sizeof(struct udphdr)))
  18. return -EINVAL;
  19. if (likely(!skb->encapsulation)) {
  20. const struct iphdr *iph;
  21. struct udphdr *uh;
  22. iph = ip_hdr(skb);
  23. uh = udp_hdr(skb);
  24. uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, skb->len,
  25. IPPROTO_UDP, 0);
  26. skb->csum_start = skb_transport_header(skb) - skb->head;
  27. skb->csum_offset = offsetof(struct udphdr, check);
  28. skb->ip_summed = CHECKSUM_PARTIAL;
  29. }
  30. return 0;
  31. }
  32. static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
  33. netdev_features_t features)
  34. {
  35. struct sk_buff *segs = ERR_PTR(-EINVAL);
  36. unsigned int mss;
  37. mss = skb_shinfo(skb)->gso_size;
  38. if (unlikely(skb->len <= mss))
  39. goto out;
  40. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  41. /* Packet is from an untrusted source, reset gso_segs. */
  42. int type = skb_shinfo(skb)->gso_type;
  43. if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
  44. SKB_GSO_UDP_TUNNEL |
  45. SKB_GSO_IPIP |
  46. SKB_GSO_GRE | SKB_GSO_MPLS) ||
  47. !(type & (SKB_GSO_UDP))))
  48. goto out;
  49. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  50. segs = NULL;
  51. goto out;
  52. }
  53. /* Fragment the skb. IP headers of the fragments are updated in
  54. * inet_gso_segment()
  55. */
  56. if (skb->encapsulation && skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL)
  57. segs = skb_udp_tunnel_segment(skb, features);
  58. else {
  59. int offset;
  60. __wsum csum;
  61. /* Do software UFO. Complete and fill in the UDP checksum as
  62. * HW cannot do checksum of UDP packets sent as multiple
  63. * IP fragments.
  64. */
  65. offset = skb_checksum_start_offset(skb);
  66. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  67. offset += skb->csum_offset;
  68. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  69. skb->ip_summed = CHECKSUM_NONE;
  70. segs = skb_segment(skb, features);
  71. }
  72. out:
  73. return segs;
  74. }
  75. static const struct net_offload udpv4_offload = {
  76. .callbacks = {
  77. .gso_send_check = udp4_ufo_send_check,
  78. .gso_segment = udp4_ufo_fragment,
  79. },
  80. };
  81. int __init udpv4_offload_init(void)
  82. {
  83. return inet_add_offload(&udpv4_offload, IPPROTO_UDP);
  84. }