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.
10 static u32 rxe_crc32(struct rxe_dev *rxe, u32 crc, void *next, size_t len)
15 SHASH_DESC_ON_STACK(shash, rxe->tfm);
17 shash->tfm = rxe->tfm;
18 *(u32 *)shash_desc_ctx(shash) = crc;
19 err = crypto_shash_update(shash, next, len);
21 pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
22 return crc32_le(crc, next, len);
25 icrc = *(u32 *)shash_desc_ctx(shash);
26 barrier_data(shash_desc_ctx(shash));
31 /* Compute a partial ICRC for all the IB transport headers. */
32 static u32 rxe_icrc_hdr(struct sk_buff *skb, struct rxe_pkt_info *pkt)
34 unsigned int bth_offset = 0;
35 struct iphdr *ip4h = NULL;
36 struct ipv6hdr *ip6h = NULL;
41 int hdr_size = sizeof(struct udphdr) +
42 (skb->protocol == htons(ETH_P_IP) ?
43 sizeof(struct iphdr) : sizeof(struct ipv6hdr));
44 /* pseudo header buffer size is calculate using ipv6 header size since
45 * it is bigger than ipv4
47 u8 pshdr[sizeof(struct udphdr) +
48 sizeof(struct ipv6hdr) +
51 /* This seed is the result of computing a CRC with a seed of
52 * 0xfffffff and 8 bytes of 0xff representing a masked LRH.
56 if (skb->protocol == htons(ETH_P_IP)) { /* IPv4 */
57 memcpy(pshdr, ip_hdr(skb), hdr_size);
58 ip4h = (struct iphdr *)pshdr;
59 udph = (struct udphdr *)(ip4h + 1);
62 ip4h->check = CSUM_MANGLED_0;
65 memcpy(pshdr, ipv6_hdr(skb), hdr_size);
66 ip6h = (struct ipv6hdr *)pshdr;
67 udph = (struct udphdr *)(ip6h + 1);
69 memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
71 ip6h->hop_limit = 0xff;
73 udph->check = CSUM_MANGLED_0;
75 bth_offset += hdr_size;
77 memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
78 bth = (struct rxe_bth *)&pshdr[bth_offset];
80 /* exclude bth.resv8a */
81 bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
83 length = hdr_size + RXE_BTH_BYTES;
84 crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
86 /* And finish to compute the CRC on the remainder of the headers. */
87 crc = rxe_crc32(pkt->rxe, crc, pkt->hdr + RXE_BTH_BYTES,
88 rxe_opcode[pkt->opcode].length - RXE_BTH_BYTES);
93 * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
94 * delivered in the packet.
98 * Return: 0 if the values match else an error
100 int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
106 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
107 pkt_icrc = be32_to_cpu(*icrcp);
109 icrc = rxe_icrc_hdr(skb, pkt);
110 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
111 payload_size(pkt) + bth_pad(pkt));
112 icrc = (__force u32)cpu_to_be32(~icrc);
114 if (unlikely(icrc != pkt_icrc)) {
115 if (skb->protocol == htons(ETH_P_IPV6))
116 pr_warn_ratelimited("bad ICRC from %pI6c\n",
117 &ipv6_hdr(skb)->saddr);
118 else if (skb->protocol == htons(ETH_P_IP))
119 pr_warn_ratelimited("bad ICRC from %pI4\n",
120 &ip_hdr(skb)->saddr);
122 pr_warn_ratelimited("bad ICRC from unknown\n");
130 /* rxe_icrc_generate- compute ICRC for a packet. */
131 void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
136 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
137 icrc = rxe_icrc_hdr(skb, pkt);
138 icrc = rxe_crc32(pkt->rxe, icrc, (u8 *)payload_addr(pkt),
139 payload_size(pkt) + bth_pad(pkt));
140 *icrcp = (__force __be32)~icrc;