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>
12 int rxe_icrc_init(struct rxe_dev *rxe)
14 struct crypto_shash *tfm;
16 tfm = crypto_alloc_shash("crc32", 0, 0);
18 pr_warn("failed to init crc32 algorithm err:%ld\n",
28 static u32 rxe_crc32(struct rxe_dev *rxe, u32 crc, void *next, size_t len)
33 SHASH_DESC_ON_STACK(shash, rxe->tfm);
35 shash->tfm = rxe->tfm;
36 *(u32 *)shash_desc_ctx(shash) = crc;
37 err = crypto_shash_update(shash, next, len);
39 pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
40 return crc32_le(crc, next, len);
43 icrc = *(u32 *)shash_desc_ctx(shash);
44 barrier_data(shash_desc_ctx(shash));
49 /* Compute a partial ICRC for all the IB transport headers. */
50 static u32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
52 unsigned int bth_offset = 0;
53 struct iphdr *ip4h = NULL;
54 struct ipv6hdr *ip6h = NULL;
59 int hdr_size = sizeof(struct udphdr) +
60 (skb->protocol == htons(ETH_P_IP) ?
61 sizeof(struct iphdr) : sizeof(struct ipv6hdr));
62 /* pseudo header buffer size is calculate using ipv6 header size since
63 * it is bigger than ipv4
65 u8 pshdr[sizeof(struct udphdr) +
66 sizeof(struct ipv6hdr) +
69 /* This seed is the result of computing a CRC with a seed of
70 * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
74 if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
75 memcpy(pshdr, ip_hdr(skb), hdr_size);
76 ip4h = (struct iphdr *)pshdr;
77 udph = (struct udphdr *)(ip4h + 1);
80 ip4h->check = CSUM_MANGLED_0;
83 memcpy(pshdr, ipv6_hdr(skb), hdr_size);
84 ip6h = (struct ipv6hdr *)pshdr;
85 udph = (struct udphdr *)(ip6h + 1);
87 memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
89 ip6h->hop_limit = 0xff;
91 udph->check = CSUM_MANGLED_0;
93 bth_offset += hdr_size;
95 memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
96 bth = (struct rxe_bth *)&pshdr[bth_offset];
98 /* exclude bth.resv8a */
99 bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
101 length = hdr_size + RXE_BTH_BYTES;
102 crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
104 /* And finish to compute the CRC on the remainder of the headers. */
105 crc = rxe_crc32(pkt->rxe, crc, pkt->hdr + RXE_BTH_BYTES,
106 rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
111 * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
112 * delivered in the packet.
113 * @skb: packet buffer
116 * Return: 0 if the values match else an error
118 int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
124 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
125 pkt_icrc = be32_to_cpu(*icrcp);
127 icrc = rxe_icrc_hdr(skb, pkt);
128 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
129 payload_size(pkt) + bth_pad(pkt));
130 icrc = (__force u32)cpu_to_be32(~icrc);
132 if (unlikely(icrc != pkt_icrc)) {
133 if (skb->protocol == htons(ETH_P_IPV6))
134 pr_warn_ratelimited("bad ICRC from %pI6c\n",
135 &ipv6_hdr(skb)->saddr);
136 else if (skb->protocol == htons(ETH_P_IP))
137 pr_warn_ratelimited("bad ICRC from %pI4\n",
138 &ip_hdr(skb)->saddr);
140 pr_warn_ratelimited("bad ICRC from unknown\n");
148 /* rxe_icrc_generate- compute ICRC for a packet. */
149 void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
154 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
155 icrc = rxe_icrc_hdr(skb, pkt);
156 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
157 payload_size(pkt) + bth_pad(pkt));
158 *icrcp = (__force __be32)~icrc;