rxe_icrc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /*
  3. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
  5. */
  6. #include <linux/crc32.h>
  7. #include "rxe.h"
  8. #include "rxe_loc.h"
  9. /**
  10. * rxe_icrc_init() - Initialize crypto function for computing crc32
  11. * @rxe: rdma_rxe device object
  12. *
  13. * Return: 0 on success else an error
  14. */
  15. int rxe_icrc_init(struct rxe_dev *rxe)
  16. {
  17. struct crypto_shash *tfm;
  18. tfm = crypto_alloc_shash("crc32", 0, 0);
  19. if (IS_ERR(tfm)) {
  20. pr_warn("failed to init crc32 algorithm err:%ld\n",
  21. PTR_ERR(tfm));
  22. return PTR_ERR(tfm);
  23. }
  24. rxe->tfm = tfm;
  25. return 0;
  26. }
  27. /**
  28. * rxe_crc32() - Compute cumulative crc32 for a contiguous segment
  29. * @rxe: rdma_rxe device object
  30. * @crc: starting crc32 value from previous segments
  31. * @next: starting address of current segment
  32. * @len: length of current segment
  33. *
  34. * Return: the cumulative crc32 checksum
  35. */
  36. static __be32 rxe_crc32(struct rxe_dev *rxe, __be32 crc, void *next, size_t len)
  37. {
  38. __be32 icrc;
  39. int err;
  40. SHASH_DESC_ON_STACK(shash, rxe->tfm);
  41. shash->tfm = rxe->tfm;
  42. *(__be32 *)shash_desc_ctx(shash) = crc;
  43. err = crypto_shash_update(shash, next, len);
  44. if (unlikely(err)) {
  45. pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
  46. return (__force __be32)crc32_le((__force u32)crc, next, len);
  47. }
  48. icrc = *(__be32 *)shash_desc_ctx(shash);
  49. barrier_data(shash_desc_ctx(shash));
  50. return icrc;
  51. }
  52. /**
  53. * rxe_icrc_hdr() - Compute the partial ICRC for the network and transport
  54. * headers of a packet.
  55. * @skb: packet buffer
  56. * @pkt: packet information
  57. *
  58. * Return: the partial ICRC
  59. */
  60. static __be32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
  61. {
  62. unsigned int bth_offset = 0;
  63. struct iphdr *ip4h = NULL;
  64. struct ipv6hdr *ip6h = NULL;
  65. struct udphdr *udph;
  66. struct rxe_bth *bth;
  67. __be32 crc;
  68. int length;
  69. int hdr_size = sizeof(struct udphdr) +
  70. (skb->protocol == htons(ETH_P_IP) ?
  71. sizeof(struct iphdr) : sizeof(struct ipv6hdr));
  72. /* pseudo header buffer size is calculate using ipv6 header size since
  73. * it is bigger than ipv4
  74. */
  75. u8 pshdr[sizeof(struct udphdr) +
  76. sizeof(struct ipv6hdr) +
  77. RXE_BTH_BYTES];
  78. /* This seed is the result of computing a CRC with a seed of
  79. * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
  80. */
  81. crc = (__force __be32)0xdebb20e3;
  82. if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
  83. memcpy(pshdr, ip_hdr(skb), hdr_size);
  84. ip4h = (struct iphdr *)pshdr;
  85. udph = (struct udphdr *)(ip4h + 1);
  86. ip4h->ttl = 0xff;
  87. ip4h->check = CSUM_MANGLED_0;
  88. ip4h->tos = 0xff;
  89. } else { /* IPv6 */
  90. memcpy(pshdr, ipv6_hdr(skb), hdr_size);
  91. ip6h = (struct ipv6hdr *)pshdr;
  92. udph = (struct udphdr *)(ip6h + 1);
  93. memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
  94. ip6h->priority = 0xf;
  95. ip6h->hop_limit = 0xff;
  96. }
  97. udph->check = CSUM_MANGLED_0;
  98. bth_offset += hdr_size;
  99. memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
  100. bth = (struct rxe_bth *)&pshdr[bth_offset];
  101. /* exclude bth.resv8a */
  102. bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
  103. length = hdr_size + RXE_BTH_BYTES;
  104. crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
  105. /* And finish to compute the CRC on the remainder of the headers. */
  106. crc = rxe_crc32(pkt->rxe, crc, pkt->hdr + RXE_BTH_BYTES,
  107. rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
  108. return crc;
  109. }
  110. /**
  111. * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
  112. * delivered in the packet.
  113. * @skb: packet buffer
  114. * @pkt: packet information
  115. *
  116. * Return: 0 if the values match else an error
  117. */
  118. int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
  119. {
  120. __be32 *icrcp;
  121. __be32 pkt_icrc;
  122. __be32 icrc;
  123. icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
  124. pkt_icrc = *icrcp;
  125. icrc = rxe_icrc_hdr(skb, pkt);
  126. icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
  127. payload_size(pkt) + bth_pad(pkt));
  128. icrc = ~icrc;
  129. if (unlikely(icrc != pkt_icrc))
  130. return -EINVAL;
  131. return 0;
  132. }
  133. /**
  134. * rxe_icrc_generate() - compute ICRC for a packet.
  135. * @skb: packet buffer
  136. * @pkt: packet information
  137. */
  138. void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
  139. {
  140. __be32 *icrcp;
  141. __be32 icrc;
  142. icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
  143. icrc = rxe_icrc_hdr(skb, pkt);
  144. icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
  145. payload_size(pkt) + bth_pad(pkt));
  146. *icrcp = ~icrc;
  147. }