777199517e9a49f9753a79699aad7301a9789d60
[linux-2.6-microblaze.git] / drivers / infiniband / sw / rxe / rxe_icrc.c
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
7 #include "rxe.h"
8 #include "rxe_loc.h"
9
10 static u32 rxe_crc32(struct rxe_dev *rxe, u32 crc, void *next, size_t len)
11 {
12         u32 icrc;
13         int err;
14
15         SHASH_DESC_ON_STACK(shash, rxe->tfm);
16
17         shash->tfm = rxe->tfm;
18         *(u32 *)shash_desc_ctx(shash) = crc;
19         err = crypto_shash_update(shash, next, len);
20         if (unlikely(err)) {
21                 pr_warn_ratelimited("failed crc calculation, err: %d\n", err);
22                 return crc32_le(crc, next, len);
23         }
24
25         icrc = *(u32 *)shash_desc_ctx(shash);
26         barrier_data(shash_desc_ctx(shash));
27
28         return icrc;
29 }
30
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)
33 {
34         unsigned int bth_offset = 0;
35         struct iphdr *ip4h = NULL;
36         struct ipv6hdr *ip6h = NULL;
37         struct udphdr *udph;
38         struct rxe_bth *bth;
39         int crc;
40         int length;
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
46          */
47         u8 pshdr[sizeof(struct udphdr) +
48                 sizeof(struct ipv6hdr) +
49                 RXE_BTH_BYTES];
50
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.
53          */
54         crc = 0xdebb20e3;
55
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);
60
61                 ip4h->ttl = 0xff;
62                 ip4h->check = CSUM_MANGLED_0;
63                 ip4h->tos = 0xff;
64         } else {                                /* IPv6 */
65                 memcpy(pshdr, ipv6_hdr(skb), hdr_size);
66                 ip6h = (struct ipv6hdr *)pshdr;
67                 udph = (struct udphdr *)(ip6h + 1);
68
69                 memset(ip6h->flow_lbl, 0xff, sizeof(ip6h->flow_lbl));
70                 ip6h->priority = 0xf;
71                 ip6h->hop_limit = 0xff;
72         }
73         udph->check = CSUM_MANGLED_0;
74
75         bth_offset += hdr_size;
76
77         memcpy(&pshdr[bth_offset], pkt->hdr, RXE_BTH_BYTES);
78         bth = (struct rxe_bth *)&pshdr[bth_offset];
79
80         /* exclude bth.resv8a */
81         bth->qpn |= cpu_to_be32(~BTH_QPN_MASK);
82
83         length = hdr_size + RXE_BTH_BYTES;
84         crc = rxe_crc32(pkt->rxe, crc, pshdr, length);
85
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);
89         return crc;
90 }
91
92 /**
93  * rxe_icrc_check() - Compute ICRC for a packet and compare to the ICRC
94  *                    delivered in the packet.
95  * @skb: packet buffer
96  * @pkt: packet info
97  *
98  * Return: 0 if the values match else an error
99  */
100 int rxe_icrc_check(struct sk_buff *skb, struct rxe_pkt_info *pkt)
101 {
102         __be32 *icrcp;
103         u32 pkt_icrc;
104         u32 icrc;
105
106         icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
107         pkt_icrc = be32_to_cpu(*icrcp);
108
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);
113
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);
121                 else
122                         pr_warn_ratelimited("bad ICRC from unknown\n");
123
124                 return -EINVAL;
125         }
126
127         return 0;
128 }
129
130 /* rxe_icrc_generate- compute ICRC for a packet. */
131 void rxe_icrc_generate(struct sk_buff *skb, struct rxe_pkt_info *pkt)
132 {
133         __be32 *icrcp;
134         u32 icrc;
135
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;
141 }