1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <linux/skbuff.h>
35
36#include "rxe.h"
37#include "rxe_loc.h"
38
39static int check_type_state(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
40 struct rxe_qp *qp)
41{
42 if (unlikely(!qp->valid))
43 goto err1;
44
45 switch (qp_type(qp)) {
46 case IB_QPT_RC:
47 if (unlikely((pkt->opcode & IB_OPCODE_RC) != 0)) {
48 pr_warn_ratelimited("bad qp type\n");
49 goto err1;
50 }
51 break;
52 case IB_QPT_UC:
53 if (unlikely(!(pkt->opcode & IB_OPCODE_UC))) {
54 pr_warn_ratelimited("bad qp type\n");
55 goto err1;
56 }
57 break;
58 case IB_QPT_UD:
59 case IB_QPT_SMI:
60 case IB_QPT_GSI:
61 if (unlikely(!(pkt->opcode & IB_OPCODE_UD))) {
62 pr_warn_ratelimited("bad qp type\n");
63 goto err1;
64 }
65 break;
66 default:
67 pr_warn_ratelimited("unsupported qp type\n");
68 goto err1;
69 }
70
71 if (pkt->mask & RXE_REQ_MASK) {
72 if (unlikely(qp->resp.state != QP_STATE_READY))
73 goto err1;
74 } else if (unlikely(qp->req.state < QP_STATE_READY ||
75 qp->req.state > QP_STATE_DRAINED)) {
76 goto err1;
77 }
78
79 return 0;
80
81err1:
82 return -EINVAL;
83}
84
85static void set_bad_pkey_cntr(struct rxe_port *port)
86{
87 spin_lock_bh(&port->port_lock);
88 port->attr.bad_pkey_cntr = min((u32)0xffff,
89 port->attr.bad_pkey_cntr + 1);
90 spin_unlock_bh(&port->port_lock);
91}
92
93static void set_qkey_viol_cntr(struct rxe_port *port)
94{
95 spin_lock_bh(&port->port_lock);
96 port->attr.qkey_viol_cntr = min((u32)0xffff,
97 port->attr.qkey_viol_cntr + 1);
98 spin_unlock_bh(&port->port_lock);
99}
100
101static int check_keys(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
102 u32 qpn, struct rxe_qp *qp)
103{
104 struct rxe_port *port = &rxe->port;
105 u16 pkey = bth_pkey(pkt);
106
107 pkt->pkey_index = 0;
108
109 if (!pkey_match(pkey, IB_DEFAULT_PKEY_FULL)) {
110 pr_warn_ratelimited("bad pkey = 0x%x\n", pkey);
111 set_bad_pkey_cntr(port);
112 goto err1;
113 }
114
115 if ((qp_type(qp) == IB_QPT_UD || qp_type(qp) == IB_QPT_GSI) &&
116 pkt->mask) {
117 u32 qkey = (qpn == 1) ? GSI_QKEY : qp->attr.qkey;
118
119 if (unlikely(deth_qkey(pkt) != qkey)) {
120 pr_warn_ratelimited("bad qkey, got 0x%x expected 0x%x for qpn 0x%x\n",
121 deth_qkey(pkt), qkey, qpn);
122 set_qkey_viol_cntr(port);
123 goto err1;
124 }
125 }
126
127 return 0;
128
129err1:
130 return -EINVAL;
131}
132
133static int check_addr(struct rxe_dev *rxe, struct rxe_pkt_info *pkt,
134 struct rxe_qp *qp)
135{
136 struct sk_buff *skb = PKT_TO_SKB(pkt);
137
138 if (qp_type(qp) != IB_QPT_RC && qp_type(qp) != IB_QPT_UC)
139 goto done;
140
141 if (unlikely(pkt->port_num != qp->attr.port_num)) {
142 pr_warn_ratelimited("port %d != qp port %d\n",
143 pkt->port_num, qp->attr.port_num);
144 goto err1;
145 }
146
147 if (skb->protocol == htons(ETH_P_IP)) {
148 struct in_addr *saddr =
149 &qp->pri_av.sgid_addr._sockaddr_in.sin_addr;
150 struct in_addr *daddr =
151 &qp->pri_av.dgid_addr._sockaddr_in.sin_addr;
152
153 if (ip_hdr(skb)->daddr != saddr->s_addr) {
154 pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n",
155 &ip_hdr(skb)->daddr,
156 &saddr->s_addr);
157 goto err1;
158 }
159
160 if (ip_hdr(skb)->saddr != daddr->s_addr) {
161 pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n",
162 &ip_hdr(skb)->saddr,
163 &daddr->s_addr);
164 goto err1;
165 }
166
167 } else if (skb->protocol == htons(ETH_P_IPV6)) {
168 struct in6_addr *saddr =
169 &qp->pri_av.sgid_addr._sockaddr_in6.sin6_addr;
170 struct in6_addr *daddr =
171 &qp->pri_av.dgid_addr._sockaddr_in6.sin6_addr;
172
173 if (memcmp(&ipv6_hdr(skb)->daddr, saddr, sizeof(*saddr))) {
174 pr_warn_ratelimited("dst addr %pI6 != qp source addr %pI6\n",
175 &ipv6_hdr(skb)->daddr, saddr);
176 goto err1;
177 }
178
179 if (memcmp(&ipv6_hdr(skb)->saddr, daddr, sizeof(*daddr))) {
180 pr_warn_ratelimited("source addr %pI6 != qp dst addr %pI6\n",
181 &ipv6_hdr(skb)->saddr, daddr);
182 goto err1;
183 }
184 }
185
186done:
187 return 0;
188
189err1:
190 return -EINVAL;
191}
192
193static int hdr_check(struct rxe_pkt_info *pkt)
194{
195 struct rxe_dev *rxe = pkt->rxe;
196 struct rxe_port *port = &rxe->port;
197 struct rxe_qp *qp = NULL;
198 u32 qpn = bth_qpn(pkt);
199 int index;
200 int err;
201
202 if (unlikely(bth_tver(pkt) != BTH_TVER)) {
203 pr_warn_ratelimited("bad tver\n");
204 goto err1;
205 }
206
207 if (unlikely(qpn == 0)) {
208 pr_warn_once("QP 0 not supported");
209 goto err1;
210 }
211
212 if (qpn != IB_MULTICAST_QPN) {
213 index = (qpn == 1) ? port->qp_gsi_index : qpn;
214
215 qp = rxe_pool_get_index(&rxe->qp_pool, index);
216 if (unlikely(!qp)) {
217 pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn);
218 goto err1;
219 }
220
221 err = check_type_state(rxe, pkt, qp);
222 if (unlikely(err))
223 goto err2;
224
225 err = check_addr(rxe, pkt, qp);
226 if (unlikely(err))
227 goto err2;
228
229 err = check_keys(rxe, pkt, qpn, qp);
230 if (unlikely(err))
231 goto err2;
232 } else {
233 if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) {
234 pr_warn_ratelimited("no grh for mcast qpn\n");
235 goto err1;
236 }
237 }
238
239 pkt->qp = qp;
240 return 0;
241
242err2:
243 rxe_drop_ref(qp);
244err1:
245 return -EINVAL;
246}
247
248static inline void rxe_rcv_pkt(struct rxe_pkt_info *pkt, struct sk_buff *skb)
249{
250 if (pkt->mask & RXE_REQ_MASK)
251 rxe_resp_queue_pkt(pkt->qp, skb);
252 else
253 rxe_comp_queue_pkt(pkt->qp, skb);
254}
255
256static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb)
257{
258 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
259 struct rxe_mc_grp *mcg;
260 struct rxe_mc_elem *mce;
261 struct rxe_qp *qp;
262 union ib_gid dgid;
263 struct sk_buff *per_qp_skb;
264 struct rxe_pkt_info *per_qp_pkt;
265 int err;
266
267 if (skb->protocol == htons(ETH_P_IP))
268 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
269 (struct in6_addr *)&dgid);
270 else if (skb->protocol == htons(ETH_P_IPV6))
271 memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
272
273
274 mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
275 if (!mcg)
276 goto err1;
277
278 spin_lock_bh(&mcg->mcg_lock);
279
280 list_for_each_entry(mce, &mcg->qp_list, qp_list) {
281 qp = mce->qp;
282 pkt = SKB_TO_PKT(skb);
283
284
285 err = check_type_state(rxe, pkt, qp);
286 if (err)
287 continue;
288
289 err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
290 if (err)
291 continue;
292
293
294
295
296 if (mce->qp_list.next != &mcg->qp_list)
297 per_qp_skb = skb_clone(skb, GFP_ATOMIC);
298 else
299 per_qp_skb = skb;
300
301 if (unlikely(!per_qp_skb))
302 continue;
303
304 per_qp_pkt = SKB_TO_PKT(per_qp_skb);
305 per_qp_pkt->qp = qp;
306 rxe_add_ref(qp);
307 rxe_rcv_pkt(per_qp_pkt, per_qp_skb);
308 }
309
310 spin_unlock_bh(&mcg->mcg_lock);
311
312 rxe_drop_ref(mcg);
313
314 return;
315
316err1:
317 kfree_skb(skb);
318}
319
320
321
322
323
324
325
326
327
328
329
330static int rxe_chk_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
331{
332 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
333 const struct ib_gid_attr *gid_attr;
334 union ib_gid dgid;
335 union ib_gid *pdgid;
336
337 if (pkt->mask & RXE_LOOPBACK_MASK)
338 return 0;
339
340 if (skb->protocol == htons(ETH_P_IP)) {
341 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
342 (struct in6_addr *)&dgid);
343 pdgid = &dgid;
344 } else {
345 pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
346 }
347
348 if (rdma_is_multicast_addr((struct in6_addr *)pdgid))
349 return 0;
350
351 gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid,
352 IB_GID_TYPE_ROCE_UDP_ENCAP,
353 1, skb->dev);
354 if (IS_ERR(gid_attr))
355 return PTR_ERR(gid_attr);
356
357 rdma_put_gid_attr(gid_attr);
358 return 0;
359}
360
361
362void rxe_rcv(struct sk_buff *skb)
363{
364 int err;
365 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
366 struct rxe_dev *rxe = pkt->rxe;
367 __be32 *icrcp;
368 u32 calc_icrc, pack_icrc;
369
370 pkt->offset = 0;
371
372 if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
373 goto drop;
374
375 if (rxe_chk_dgid(rxe, skb) < 0) {
376 pr_warn_ratelimited("failed checking dgid\n");
377 goto drop;
378 }
379
380 pkt->opcode = bth_opcode(pkt);
381 pkt->psn = bth_psn(pkt);
382 pkt->qp = NULL;
383 pkt->mask |= rxe_opcode[pkt->opcode].mask;
384
385 if (unlikely(skb->len < header_size(pkt)))
386 goto drop;
387
388 err = hdr_check(pkt);
389 if (unlikely(err))
390 goto drop;
391
392
393 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
394 pack_icrc = be32_to_cpu(*icrcp);
395
396 calc_icrc = rxe_icrc_hdr(pkt, skb);
397 calc_icrc = rxe_crc32(rxe, calc_icrc, (u8 *)payload_addr(pkt),
398 payload_size(pkt) + bth_pad(pkt));
399 calc_icrc = (__force u32)cpu_to_be32(~calc_icrc);
400 if (unlikely(calc_icrc != pack_icrc)) {
401 if (skb->protocol == htons(ETH_P_IPV6))
402 pr_warn_ratelimited("bad ICRC from %pI6c\n",
403 &ipv6_hdr(skb)->saddr);
404 else if (skb->protocol == htons(ETH_P_IP))
405 pr_warn_ratelimited("bad ICRC from %pI4\n",
406 &ip_hdr(skb)->saddr);
407 else
408 pr_warn_ratelimited("bad ICRC from unknown\n");
409
410 goto drop;
411 }
412
413 rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS);
414
415 if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
416 rxe_rcv_mcast_pkt(rxe, skb);
417 else
418 rxe_rcv_pkt(pkt, skb);
419
420 return;
421
422drop:
423 if (pkt->qp)
424 rxe_drop_ref(pkt->qp);
425
426 kfree_skb(skb);
427}
428