ip6_checksum.c 3.1 KB

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