GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / infiniband / sw / rxe / rxe_recv.c
1 /*
2  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/skbuff.h>
35
36 #include "rxe.h"
37 #include "rxe_loc.h"
38
39 /* check that QP matches packet opcode type and is in a valid state */
40 static int check_type_state(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
41                             struct rxe_qp *qp)
42 {
43         unsigned int pkt_type;
44
45         if (unlikely(!qp->valid))
46                 goto err1;
47
48         pkt_type = pkt->opcode & 0xe0;
49
50         switch (qp_type(qp)) {
51         case IB_QPT_RC:
52                 if (unlikely(pkt_type != IB_OPCODE_RC)) {
53                         pr_warn_ratelimited("bad qp type\n");
54                         goto err1;
55                 }
56                 break;
57         case IB_QPT_UC:
58                 if (unlikely(pkt_type != IB_OPCODE_UC)) {
59                         pr_warn_ratelimited("bad qp type\n");
60                         goto err1;
61                 }
62                 break;
63         case IB_QPT_UD:
64         case IB_QPT_SMI:
65         case IB_QPT_GSI:
66                 if (unlikely(pkt_type != IB_OPCODE_UD)) {
67                         pr_warn_ratelimited("bad qp type\n");
68                         goto err1;
69                 }
70                 break;
71         default:
72                 pr_warn_ratelimited("unsupported qp type\n");
73                 goto err1;
74         }
75
76         if (pkt->mask & RXE_REQ_MASK) {
77                 if (unlikely(qp->resp.state != QP_STATE_READY))
78                         goto err1;
79         } else if (unlikely(qp->req.state < QP_STATE_READY ||
80                                 qp->req.state > QP_STATE_DRAINED)) {
81                 goto err1;
82         }
83
84         return 0;
85
86 err1:
87         return -EINVAL;
88 }
89
90 static void set_bad_pkey_cntr(struct rxe_port *port)
91 {
92         spin_lock_bh(&port->port_lock);
93         port->attr.bad_pkey_cntr = min((u32)0xffff,
94                                        port->attr.bad_pkey_cntr + 1);
95         spin_unlock_bh(&port->port_lock);
96 }
97
98 static void set_qkey_viol_cntr(struct rxe_port *port)
99 {
100         spin_lock_bh(&port->port_lock);
101         port->attr.qkey_viol_cntr = min((u32)0xffff,
102                                         port->attr.qkey_viol_cntr + 1);
103         spin_unlock_bh(&port->port_lock);
104 }
105
106 static int check_keys(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
107                       u32 qpn, struct rxe_qp *qp)
108 {
109         int i;
110         int found_pkey = 0;
111         struct rxe_port *port = &rxe->port;
112         u16 pkey = bth_pkey(pkt);
113
114         pkt->pkey_index = 0;
115
116         if (qpn == 1) {
117                 for (i = 0; i < port->attr.pkey_tbl_len; i++) {
118                         if (pkey_match(pkey, port->pkey_tbl[i])) {
119                                 pkt->pkey_index = i;
120                                 found_pkey = 1;
121                                 break;
122                         }
123                 }
124
125                 if (!found_pkey) {
126                         pr_warn_ratelimited("bad pkey = 0x%x\n", pkey);
127                         set_bad_pkey_cntr(port);
128                         goto err1;
129                 }
130         } else if (qpn != 0) {
131                 if (unlikely(!pkey_match(pkey,
132                                          port->pkey_tbl[qp->attr.pkey_index]
133                                         ))) {
134                         pr_warn_ratelimited("bad pkey = 0x%0x\n", pkey);
135                         set_bad_pkey_cntr(port);
136                         goto err1;
137                 }
138                 pkt->pkey_index = qp->attr.pkey_index;
139         }
140
141         if ((qp_type(qp) == IB_QPT_UD || qp_type(qp) == IB_QPT_GSI) &&
142             qpn != 0 && pkt->mask) {
143                 u32 qkey = (qpn == 1) ? GSI_QKEY : qp->attr.qkey;
144
145                 if (unlikely(deth_qkey(pkt) != qkey)) {
146                         pr_warn_ratelimited("bad qkey, got 0x%x expected 0x%x for qpn 0x%x\n",
147                                             deth_qkey(pkt), qkey, qpn);
148                         set_qkey_viol_cntr(port);
149                         goto err1;
150                 }
151         }
152
153         return 0;
154
155 err1:
156         return -EINVAL;
157 }
158
159 static int check_addr(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
160                       struct rxe_qp *qp)
161 {
162         struct sk_buff *skb = PKT_TO_SKB(pkt);
163
164         if (qp_type(qp) != IB_QPT_RC && qp_type(qp) != IB_QPT_UC)
165                 goto done;
166
167         if (unlikely(pkt->port_num != qp->attr.port_num)) {
168                 pr_warn_ratelimited("port %d != qp port %d\n",
169                                     pkt->port_num, qp->attr.port_num);
170                 goto err1;
171         }
172
173         if (skb->protocol == htons(ETH_P_IP)) {
174                 struct in_addr *saddr =
175                         &qp->pri_av.sgid_addr._sockaddr_in.sin_addr;
176                 struct in_addr *daddr =
177                         &qp->pri_av.dgid_addr._sockaddr_in.sin_addr;
178
179                 if (ip_hdr(skb)->daddr != saddr->s_addr) {
180                         pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n",
181                                             &ip_hdr(skb)->daddr,
182                                             &saddr->s_addr);
183                         goto err1;
184                 }
185
186                 if (ip_hdr(skb)->saddr != daddr->s_addr) {
187                         pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n",
188                                             &ip_hdr(skb)->saddr,
189                                             &daddr->s_addr);
190                         goto err1;
191                 }
192
193         } else if (skb->protocol == htons(ETH_P_IPV6)) {
194                 struct in6_addr *saddr =
195                         &qp->pri_av.sgid_addr._sockaddr_in6.sin6_addr;
196                 struct in6_addr *daddr =
197                         &qp->pri_av.dgid_addr._sockaddr_in6.sin6_addr;
198
199                 if (memcmp(&ipv6_hdr(skb)->daddr, saddr, sizeof(*saddr))) {
200                         pr_warn_ratelimited("dst addr %pI6 != qp source addr %pI6\n",
201                                             &ipv6_hdr(skb)->daddr, saddr);
202                         goto err1;
203                 }
204
205                 if (memcmp(&ipv6_hdr(skb)->saddr, daddr, sizeof(*daddr))) {
206                         pr_warn_ratelimited("source addr %pI6 != qp dst addr %pI6\n",
207                                             &ipv6_hdr(skb)->saddr, daddr);
208                         goto err1;
209                 }
210         }
211
212 done:
213         return 0;
214
215 err1:
216         return -EINVAL;
217 }
218
219 static int hdr_check(struct rxe_pkt_info *pkt)
220 {
221         struct rxe_dev *rxe = pkt->rxe;
222         struct rxe_port *port = &rxe->port;
223         struct rxe_qp *qp = NULL;
224         u32 qpn = bth_qpn(pkt);
225         int index;
226         int err;
227
228         if (unlikely(bth_tver(pkt) != BTH_TVER)) {
229                 pr_warn_ratelimited("bad tver\n");
230                 goto err1;
231         }
232
233         if (unlikely(qpn == 0)) {
234                 pr_warn_once("QP 0 not supported");
235                 goto err1;
236         }
237
238         if (qpn != IB_MULTICAST_QPN) {
239                 index = (qpn == 1) ? port->qp_gsi_index : qpn;
240
241                 qp = rxe_pool_get_index(&rxe->qp_pool, index);
242                 if (unlikely(!qp)) {
243                         pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn);
244                         goto err1;
245                 }
246
247                 err = check_type_state(rxe, pkt, qp);
248                 if (unlikely(err))
249                         goto err2;
250
251                 err = check_addr(rxe, pkt, qp);
252                 if (unlikely(err))
253                         goto err2;
254
255                 err = check_keys(rxe, pkt, qpn, qp);
256                 if (unlikely(err))
257                         goto err2;
258         } else {
259                 if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) {
260                         pr_warn_ratelimited("no grh for mcast qpn\n");
261                         goto err1;
262                 }
263         }
264
265         pkt->qp = qp;
266         return 0;
267
268 err2:
269         rxe_drop_ref(qp);
270 err1:
271         return -EINVAL;
272 }
273
274 static inline void rxe_rcv_pkt(struct rxe_dev *rxe,
275                                struct rxe_pkt_info *pkt,
276                                struct sk_buff *skb)
277 {
278         if (pkt->mask & RXE_REQ_MASK)
279                 rxe_resp_queue_pkt(rxe, pkt->qp, skb);
280         else
281                 rxe_comp_queue_pkt(rxe, pkt->qp, skb);
282 }
283
284 static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb)
285 {
286         struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
287         struct rxe_mc_grp *mcg;
288         struct rxe_mc_elem *mce;
289         struct rxe_qp *qp;
290         union ib_gid dgid;
291         int err;
292
293         if (skb->protocol == htons(ETH_P_IP))
294                 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
295                                        (struct in6_addr *)&dgid);
296         else if (skb->protocol == htons(ETH_P_IPV6))
297                 memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
298
299         /* lookup mcast group corresponding to mgid, takes a ref */
300         mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
301         if (!mcg)
302                 goto err1;      /* mcast group not registered */
303
304         spin_lock_bh(&mcg->mcg_lock);
305
306         list_for_each_entry(mce, &mcg->qp_list, qp_list) {
307                 qp = mce->qp;
308                 pkt = SKB_TO_PKT(skb);
309
310                 /* validate qp for incoming packet */
311                 err = check_type_state(rxe, pkt, qp);
312                 if (err)
313                         continue;
314
315                 err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
316                 if (err)
317                         continue;
318
319                 /* if *not* the last qp in the list
320                  * increase the users of the skb then post to the next qp
321                  */
322                 if (mce->qp_list.next != &mcg->qp_list)
323                         skb_get(skb);
324
325                 pkt->qp = qp;
326                 rxe_add_ref(qp);
327                 rxe_rcv_pkt(rxe, pkt, skb);
328         }
329
330         spin_unlock_bh(&mcg->mcg_lock);
331
332         rxe_drop_ref(mcg);      /* drop ref from rxe_pool_get_key. */
333
334 err1:
335         kfree_skb(skb);
336 }
337
338 static int rxe_match_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
339 {
340         struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
341         const struct ib_gid_attr *gid_attr;
342         union ib_gid dgid;
343         union ib_gid *pdgid;
344
345         if (pkt->mask & RXE_LOOPBACK_MASK)
346                 return 0;
347
348         if (skb->protocol == htons(ETH_P_IP)) {
349                 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
350                                        (struct in6_addr *)&dgid);
351                 pdgid = &dgid;
352         } else {
353                 pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
354         }
355
356         gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid,
357                                          IB_GID_TYPE_ROCE_UDP_ENCAP,
358                                          1, skb->dev);
359         if (IS_ERR(gid_attr))
360                 return PTR_ERR(gid_attr);
361
362         rdma_put_gid_attr(gid_attr);
363         return 0;
364 }
365
366 /* rxe_rcv is called from the interface driver */
367 void rxe_rcv(struct sk_buff *skb)
368 {
369         int err;
370         struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
371         struct rxe_dev *rxe = pkt->rxe;
372         __be32 *icrcp;
373         u32 calc_icrc, pack_icrc;
374
375         pkt->offset = 0;
376
377         if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
378                 goto drop;
379
380         if (rxe_match_dgid(rxe, skb) < 0) {
381                 pr_warn_ratelimited("failed matching dgid\n");
382                 goto drop;
383         }
384
385         pkt->opcode = bth_opcode(pkt);
386         pkt->psn = bth_psn(pkt);
387         pkt->qp = NULL;
388         pkt->mask |= rxe_opcode[pkt->opcode].mask;
389
390         if (unlikely(skb->len < header_size(pkt)))
391                 goto drop;
392
393         err = hdr_check(pkt);
394         if (unlikely(err))
395                 goto drop;
396
397         /* Verify ICRC */
398         icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
399         pack_icrc = be32_to_cpu(*icrcp);
400
401         calc_icrc = rxe_icrc_hdr(pkt, skb);
402         calc_icrc = rxe_crc32(rxe, calc_icrc, (u8 *)payload_addr(pkt),
403                               payload_size(pkt) + bth_pad(pkt));
404         calc_icrc = (__force u32)cpu_to_be32(~calc_icrc);
405         if (unlikely(calc_icrc != pack_icrc)) {
406                 if (skb->protocol == htons(ETH_P_IPV6))
407                         pr_warn_ratelimited("bad ICRC from %pI6c\n",
408                                             &ipv6_hdr(skb)->saddr);
409                 else if (skb->protocol == htons(ETH_P_IP))
410                         pr_warn_ratelimited("bad ICRC from %pI4\n",
411                                             &ip_hdr(skb)->saddr);
412                 else
413                         pr_warn_ratelimited("bad ICRC from unknown\n");
414
415                 goto drop;
416         }
417
418         rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS);
419
420         if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
421                 rxe_rcv_mcast_pkt(rxe, skb);
422         else
423                 rxe_rcv_pkt(rxe, pkt, skb);
424
425         return;
426
427 drop:
428         if (pkt->qp)
429                 rxe_drop_ref(pkt->qp);
430
431         kfree_skb(skb);
432 }