udp_offload.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * IPV6 GSO/GRO offload support
  3. * Linux INET6 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. * UDPv6 GSO support
  11. */
  12. #include <linux/skbuff.h>
  13. #include <linux/netdevice.h>
  14. #include <net/protocol.h>
  15. #include <net/ipv6.h>
  16. #include <net/udp.h>
  17. #include <net/ip6_checksum.h>
  18. #include "ip6_offload.h"
  19. static int udp6_ufo_send_check(struct sk_buff *skb)
  20. {
  21. const struct ipv6hdr *ipv6h;
  22. struct udphdr *uh;
  23. if (!pskb_may_pull(skb, sizeof(*uh)))
  24. return -EINVAL;
  25. if (likely(!skb->encapsulation)) {
  26. ipv6h = ipv6_hdr(skb);
  27. uh = udp_hdr(skb);
  28. uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
  29. IPPROTO_UDP, 0);
  30. skb->csum_start = skb_transport_header(skb) - skb->head;
  31. skb->csum_offset = offsetof(struct udphdr, check);
  32. skb->ip_summed = CHECKSUM_PARTIAL;
  33. }
  34. return 0;
  35. }
  36. static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
  37. netdev_features_t features)
  38. {
  39. struct sk_buff *segs = ERR_PTR(-EINVAL);
  40. unsigned int mss;
  41. unsigned int unfrag_ip6hlen, unfrag_len;
  42. struct frag_hdr *fptr;
  43. u8 *packet_start, *prevhdr;
  44. u8 nexthdr;
  45. u8 frag_hdr_sz = sizeof(struct frag_hdr);
  46. int offset;
  47. __wsum csum;
  48. int tnl_hlen;
  49. mss = skb_shinfo(skb)->gso_size;
  50. if (unlikely(skb->len <= mss))
  51. goto out;
  52. if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
  53. /* Packet is from an untrusted source, reset gso_segs. */
  54. int type = skb_shinfo(skb)->gso_type;
  55. if (unlikely(type & ~(SKB_GSO_UDP |
  56. SKB_GSO_DODGY |
  57. SKB_GSO_UDP_TUNNEL |
  58. SKB_GSO_UDP_TUNNEL_CSUM |
  59. SKB_GSO_GRE |
  60. SKB_GSO_GRE_CSUM |
  61. SKB_GSO_IPIP |
  62. SKB_GSO_SIT |
  63. SKB_GSO_MPLS) ||
  64. !(type & (SKB_GSO_UDP))))
  65. goto out;
  66. skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
  67. segs = NULL;
  68. goto out;
  69. }
  70. if (skb->encapsulation && skb_shinfo(skb)->gso_type &
  71. (SKB_GSO_UDP_TUNNEL|SKB_GSO_UDP_TUNNEL_CSUM))
  72. segs = skb_udp_tunnel_segment(skb, features);
  73. else {
  74. /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
  75. * do checksum of UDP packets sent as multiple IP fragments.
  76. */
  77. offset = skb_checksum_start_offset(skb);
  78. csum = skb_checksum(skb, offset, skb->len - offset, 0);
  79. offset += skb->csum_offset;
  80. *(__sum16 *)(skb->data + offset) = csum_fold(csum);
  81. skb->ip_summed = CHECKSUM_NONE;
  82. /* Check if there is enough headroom to insert fragment header. */
  83. tnl_hlen = skb_tnl_header_len(skb);
  84. if (skb->mac_header < (tnl_hlen + frag_hdr_sz)) {
  85. if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
  86. goto out;
  87. }
  88. /* Find the unfragmentable header and shift it left by frag_hdr_sz
  89. * bytes to insert fragment header.
  90. */
  91. unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
  92. nexthdr = *prevhdr;
  93. *prevhdr = NEXTHDR_FRAGMENT;
  94. unfrag_len = (skb_network_header(skb) - skb_mac_header(skb)) +
  95. unfrag_ip6hlen + tnl_hlen;
  96. packet_start = (u8 *) skb->head + SKB_GSO_CB(skb)->mac_offset;
  97. memmove(packet_start-frag_hdr_sz, packet_start, unfrag_len);
  98. SKB_GSO_CB(skb)->mac_offset -= frag_hdr_sz;
  99. skb->mac_header -= frag_hdr_sz;
  100. skb->network_header -= frag_hdr_sz;
  101. fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
  102. fptr->nexthdr = nexthdr;
  103. fptr->reserved = 0;
  104. fptr->identification = skb_shinfo(skb)->ip6_frag_id;
  105. /* Fragment the skb. ipv6 header and the remaining fields of the
  106. * fragment header are updated in ipv6_gso_segment()
  107. */
  108. segs = skb_segment(skb, features);
  109. }
  110. out:
  111. return segs;
  112. }
  113. static struct sk_buff **udp6_gro_receive(struct sk_buff **head,
  114. struct sk_buff *skb)
  115. {
  116. struct udphdr *uh = udp_gro_udphdr(skb);
  117. /* Don't bother verifying checksum if we're going to flush anyway. */
  118. if (unlikely(!uh) ||
  119. (!NAPI_GRO_CB(skb)->flush &&
  120. skb_gro_checksum_validate_zero_check(skb, IPPROTO_UDP, uh->check,
  121. ip6_gro_compute_pseudo))) {
  122. NAPI_GRO_CB(skb)->flush = 1;
  123. return NULL;
  124. }
  125. return udp_gro_receive(head, skb, uh);
  126. }
  127. int udp6_gro_complete(struct sk_buff *skb, int nhoff)
  128. {
  129. const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  130. struct udphdr *uh = (struct udphdr *)(skb->data + nhoff);
  131. if (uh->check)
  132. uh->check = ~udp_v6_check(skb->len - nhoff, &ipv6h->saddr,
  133. &ipv6h->daddr, 0);
  134. return udp_gro_complete(skb, nhoff);
  135. }
  136. static const struct net_offload udpv6_offload = {
  137. .callbacks = {
  138. .gso_send_check = udp6_ufo_send_check,
  139. .gso_segment = udp6_ufo_fragment,
  140. .gro_receive = udp6_gro_receive,
  141. .gro_complete = udp6_gro_complete,
  142. },
  143. };
  144. int __init udp_offload_init(void)
  145. {
  146. return inet6_add_offload(&udpv6_offload, IPPROTO_UDP);
  147. }