ip6_checksum.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <net/ip.h>
  3. #include <net/udp.h>
  4. #include <net/udplite.h>
  5. #include <asm/checksum.h>
  6. #ifndef _HAVE_ARCH_IPV6_CSUM
  7. __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  8. const struct in6_addr *daddr,
  9. __u32 len, __u8 proto, __wsum csum)
  10. {
  11. int carry;
  12. __u32 ulen;
  13. __u32 uproto;
  14. __u32 sum = (__force u32)csum;
  15. sum += (__force u32)saddr->s6_addr32[0];
  16. carry = (sum < (__force u32)saddr->s6_addr32[0]);
  17. sum += carry;
  18. sum += (__force u32)saddr->s6_addr32[1];
  19. carry = (sum < (__force u32)saddr->s6_addr32[1]);
  20. sum += carry;
  21. sum += (__force u32)saddr->s6_addr32[2];
  22. carry = (sum < (__force u32)saddr->s6_addr32[2]);
  23. sum += carry;
  24. sum += (__force u32)saddr->s6_addr32[3];
  25. carry = (sum < (__force u32)saddr->s6_addr32[3]);
  26. sum += carry;
  27. sum += (__force u32)daddr->s6_addr32[0];
  28. carry = (sum < (__force u32)daddr->s6_addr32[0]);
  29. sum += carry;
  30. sum += (__force u32)daddr->s6_addr32[1];
  31. carry = (sum < (__force u32)daddr->s6_addr32[1]);
  32. sum += carry;
  33. sum += (__force u32)daddr->s6_addr32[2];
  34. carry = (sum < (__force u32)daddr->s6_addr32[2]);
  35. sum += carry;
  36. sum += (__force u32)daddr->s6_addr32[3];
  37. carry = (sum < (__force u32)daddr->s6_addr32[3]);
  38. sum += carry;
  39. ulen = (__force u32)htonl((__u32) len);
  40. sum += ulen;
  41. carry = (sum < ulen);
  42. sum += carry;
  43. uproto = (__force u32)htonl(proto);
  44. sum += uproto;
  45. carry = (sum < uproto);
  46. sum += carry;
  47. return csum_fold((__force __wsum)sum);
  48. }
  49. EXPORT_SYMBOL(csum_ipv6_magic);
  50. #endif
  51. int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh, int proto)
  52. {
  53. int err;
  54. UDP_SKB_CB(skb)->partial_cov = 0;
  55. UDP_SKB_CB(skb)->cscov = skb->len;
  56. if (proto == IPPROTO_UDPLITE) {
  57. err = udplite_checksum_init(skb, uh);
  58. if (err)
  59. return err;
  60. if (UDP_SKB_CB(skb)->partial_cov) {
  61. skb->csum = ip6_compute_pseudo(skb, proto);
  62. return 0;
  63. }
  64. }
  65. /* To support RFC 6936 (allow zero checksum in UDP/IPV6 for tunnels)
  66. * we accept a checksum of zero here. When we find the socket
  67. * for the UDP packet we'll check if that socket allows zero checksum
  68. * for IPv6 (set by socket option).
  69. *
  70. * Note, we are only interested in != 0 or == 0, thus the
  71. * force to int.
  72. */
  73. err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
  74. ip6_compute_pseudo);
  75. if (err)
  76. return err;
  77. if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
  78. /* If SW calculated the value, we know it's bad */
  79. if (skb->csum_complete_sw)
  80. return 1;
  81. /* HW says the value is bad. Let's validate that.
  82. * skb->csum is no longer the full packet checksum,
  83. * so don't treat is as such.
  84. */
  85. skb_checksum_complete_unset(skb);
  86. }
  87. return 0;
  88. }
  89. EXPORT_SYMBOL(udp6_csum_init);
  90. /* Function to set UDP checksum for an IPv6 UDP packet. This is intended
  91. * for the simple case like when setting the checksum for a UDP tunnel.
  92. */
  93. void udp6_set_csum(bool nocheck, struct sk_buff *skb,
  94. const struct in6_addr *saddr,
  95. const struct in6_addr *daddr, int len)
  96. {
  97. struct udphdr *uh = udp_hdr(skb);
  98. if (nocheck)
  99. uh->check = 0;
  100. else if (skb_is_gso(skb))
  101. uh->check = ~udp_v6_check(len, saddr, daddr, 0);
  102. else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  103. uh->check = 0;
  104. uh->check = udp_v6_check(len, saddr, daddr, lco_csum(skb));
  105. if (uh->check == 0)
  106. uh->check = CSUM_MANGLED_0;
  107. } else {
  108. skb->ip_summed = CHECKSUM_PARTIAL;
  109. skb->csum_start = skb_transport_header(skb) - skb->head;
  110. skb->csum_offset = offsetof(struct udphdr, check);
  111. uh->check = ~udp_v6_check(len, saddr, daddr, 0);
  112. }
  113. }
  114. EXPORT_SYMBOL(udp6_set_csum);