1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
7 #include <linux/crc32.h>
13 * rxe_icrc_init() - Initialize crypto function for computing crc32
14 * @rxe: rdma_rxe device object
16 * Return: 0 on success else an error
18 int rxe_icrc_init(struct rxe_dev *rxe)
20 struct crypto_shash *tfm;
22 tfm = crypto_alloc_shash("crc32", 0, 0);
24 rxe_dbg_dev(rxe, "failed to init crc32 algorithm err: %ld\n",
35 * rxe_crc32() - Compute cumulative crc32 for a contiguous segment
36 * @rxe: rdma_rxe device object
37 * @crc: starting crc32 value from previous segments
38 * @next: starting address of current segment
39 * @len: length of current segment
41 * Return: the cumulative crc32 checksum
43 static __be32 rxe_crc32(struct rxe_dev *rxe, __be32 crc, void *next, size_t len)
48 SHASH_DESC_ON_STACK(shash, rxe->tfm);
50 shash->tfm = rxe->tfm;
51 *(__be32 *)shash_desc_ctx(shash) = crc;
52 err = crypto_shash_update(shash, next, len);
54 rxe_dbg_dev(rxe, "failed crc calculation, err: %d\n", err);
55 return (__force __be32)crc32_le((__force u32)crc, next, len);
58 icrc = *(__be32 *)shash_desc_ctx(shash);
59 barrier_data(shash_desc_ctx(shash));
65 * rxe_icrc_hdr() - Compute the partial ICRC for the network and transport
66 * headers of a packet.
68 * @pkt: packet information
70 * Return: the partial ICRC
72 static __be32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
74 unsigned int bth_offset = 0;
75 struct iphdr *ip4h = NULL;
76 struct ipv6hdr *ip6h = NULL;
81 int hdr_size = sizeof(struct udphdr) +
82 (skb->protocol == htons(ETH_P_IP) ?
83 sizeof(struct iphdr) : sizeof(struct ipv6hdr));
84 /* pseudo header buffer size is calculate using ipv6 header size since
85 * it is bigger than ipv4
87 u8 pshdr[sizeof(struct udphdr) +
88 sizeof(struct ipv6hdr) +
91 /* This seed is the result of computing a CRC with a seed of
92 * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
94 crc = (__force __be32)0xdebb20e3;
96 if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
97 memcpy(pshdr, ip_hdr(skb), hdr_size);
98 ip4h = (struct iphdr *)pshdr;
99 udph = (struct udphdr *)(ip4h + 1);
102 ip4h->check = CSUM_MANGLED_0;
105 memcpy(pshdr, ipv6_hdr(skb), hdr_size);
106 ip6h = (struct ipv6hdr *)pshdr;
107 udph = (struct udphdr *)(ip6h + 1);
109 memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
110 ip6h->priority = 0xf;
111 ip6h->hop_limit = 0xff;
113 udph->check = CSUM_MANGLED_0;
115 bth_offset += hdr_size;
117 memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
118 bth = (struct rxe_bth *)&pshdr[bth_offset];
120 /* exclude bth.resv8a */
121 bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
123 length = hdr_size + RXE_BTH_BYTES;
124 crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
126 /* And finish to compute the CRC on the remainder of the headers. */
127 crc = rxe_crc32(pkt->rxe, crc, pkt->hdr + RXE_BTH_BYTES,
128 rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
133 * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
134 * delivered in the packet.
135 * @skb: packet buffer
136 * @pkt: packet information
138 * Return: 0 if the values match else an error
140 int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
146 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
149 icrc = rxe_icrc_hdr(skb, pkt);
150 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
151 payload_size(pkt) + bth_pad(pkt));
154 if (unlikely(icrc != pkt_icrc))
161 * rxe_icrc_generate() - compute ICRC for a packet.
162 * @skb: packet buffer
163 * @pkt: packet information
165 void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
170 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
171 icrc = rxe_icrc_hdr(skb, pkt);
172 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
173 payload_size(pkt) + bth_pad(pkt));