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/skbuff.h>
12 static int check_type_state(struct rxe_dev
*rxe
, struct rxe_pkt_info
*pkt
,
15 if (unlikely(!qp
->valid
))
18 switch (qp_type(qp
)) {
20 if (unlikely((pkt
->opcode
& IB_OPCODE_RC
) != 0)) {
21 pr_warn_ratelimited("bad qp type\n");
26 if (unlikely(!(pkt
->opcode
& IB_OPCODE_UC
))) {
27 pr_warn_ratelimited("bad qp type\n");
34 if (unlikely(!(pkt
->opcode
& IB_OPCODE_UD
))) {
35 pr_warn_ratelimited("bad qp type\n");
40 pr_warn_ratelimited("unsupported qp type\n");
44 if (pkt
->mask
& RXE_REQ_MASK
) {
45 if (unlikely(qp
->resp
.state
!= QP_STATE_READY
))
47 } else if (unlikely(qp
->req
.state
< QP_STATE_READY
||
48 qp
->req
.state
> QP_STATE_DRAINED
)) {
58 static void set_bad_pkey_cntr(struct rxe_port
*port
)
60 spin_lock_bh(&port
->port_lock
);
61 port
->attr
.bad_pkey_cntr
= min((u32
)0xffff,
62 port
->attr
.bad_pkey_cntr
+ 1);
63 spin_unlock_bh(&port
->port_lock
);
66 static void set_qkey_viol_cntr(struct rxe_port
*port
)
68 spin_lock_bh(&port
->port_lock
);
69 port
->attr
.qkey_viol_cntr
= min((u32
)0xffff,
70 port
->attr
.qkey_viol_cntr
+ 1);
71 spin_unlock_bh(&port
->port_lock
);
74 static int check_keys(struct rxe_dev
*rxe
, struct rxe_pkt_info
*pkt
,
75 u32 qpn
, struct rxe_qp
*qp
)
77 struct rxe_port
*port
= &rxe
->port
;
78 u16 pkey
= bth_pkey(pkt
);
82 if (!pkey_match(pkey
, IB_DEFAULT_PKEY_FULL
)) {
83 pr_warn_ratelimited("bad pkey = 0x%x\n", pkey
);
84 set_bad_pkey_cntr(port
);
88 if ((qp_type(qp
) == IB_QPT_UD
|| qp_type(qp
) == IB_QPT_GSI
) &&
90 u32 qkey
= (qpn
== 1) ? GSI_QKEY
: qp
->attr
.qkey
;
92 if (unlikely(deth_qkey(pkt
) != qkey
)) {
93 pr_warn_ratelimited("bad qkey, got 0x%x expected 0x%x for qpn 0x%x\n",
94 deth_qkey(pkt
), qkey
, qpn
);
95 set_qkey_viol_cntr(port
);
106 static int check_addr(struct rxe_dev
*rxe
, struct rxe_pkt_info
*pkt
,
109 struct sk_buff
*skb
= PKT_TO_SKB(pkt
);
111 if (qp_type(qp
) != IB_QPT_RC
&& qp_type(qp
) != IB_QPT_UC
)
114 if (unlikely(pkt
->port_num
!= qp
->attr
.port_num
)) {
115 pr_warn_ratelimited("port %d != qp port %d\n",
116 pkt
->port_num
, qp
->attr
.port_num
);
120 if (skb
->protocol
== htons(ETH_P_IP
)) {
121 struct in_addr
*saddr
=
122 &qp
->pri_av
.sgid_addr
._sockaddr_in
.sin_addr
;
123 struct in_addr
*daddr
=
124 &qp
->pri_av
.dgid_addr
._sockaddr_in
.sin_addr
;
126 if (ip_hdr(skb
)->daddr
!= saddr
->s_addr
) {
127 pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n",
133 if (ip_hdr(skb
)->saddr
!= daddr
->s_addr
) {
134 pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n",
140 } else if (skb
->protocol
== htons(ETH_P_IPV6
)) {
141 struct in6_addr
*saddr
=
142 &qp
->pri_av
.sgid_addr
._sockaddr_in6
.sin6_addr
;
143 struct in6_addr
*daddr
=
144 &qp
->pri_av
.dgid_addr
._sockaddr_in6
.sin6_addr
;
146 if (memcmp(&ipv6_hdr(skb
)->daddr
, saddr
, sizeof(*saddr
))) {
147 pr_warn_ratelimited("dst addr %pI6 != qp source addr %pI6\n",
148 &ipv6_hdr(skb
)->daddr
, saddr
);
152 if (memcmp(&ipv6_hdr(skb
)->saddr
, daddr
, sizeof(*daddr
))) {
153 pr_warn_ratelimited("source addr %pI6 != qp dst addr %pI6\n",
154 &ipv6_hdr(skb
)->saddr
, daddr
);
166 static int hdr_check(struct rxe_pkt_info
*pkt
)
168 struct rxe_dev
*rxe
= pkt
->rxe
;
169 struct rxe_port
*port
= &rxe
->port
;
170 struct rxe_qp
*qp
= NULL
;
171 u32 qpn
= bth_qpn(pkt
);
175 if (unlikely(bth_tver(pkt
) != BTH_TVER
)) {
176 pr_warn_ratelimited("bad tver\n");
180 if (unlikely(qpn
== 0)) {
181 pr_warn_once("QP 0 not supported");
185 if (qpn
!= IB_MULTICAST_QPN
) {
186 index
= (qpn
== 1) ? port
->qp_gsi_index
: qpn
;
188 qp
= rxe_pool_get_index(&rxe
->qp_pool
, index
);
190 pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn
);
194 err
= check_type_state(rxe
, pkt
, qp
);
198 err
= check_addr(rxe
, pkt
, qp
);
202 err
= check_keys(rxe
, pkt
, qpn
, qp
);
206 if (unlikely((pkt
->mask
& RXE_GRH_MASK
) == 0)) {
207 pr_warn_ratelimited("no grh for mcast qpn\n");
221 static inline void rxe_rcv_pkt(struct rxe_pkt_info
*pkt
, struct sk_buff
*skb
)
223 if (pkt
->mask
& RXE_REQ_MASK
)
224 rxe_resp_queue_pkt(pkt
->qp
, skb
);
226 rxe_comp_queue_pkt(pkt
->qp
, skb
);
229 static void rxe_rcv_mcast_pkt(struct rxe_dev
*rxe
, struct sk_buff
*skb
)
231 struct rxe_pkt_info
*pkt
= SKB_TO_PKT(skb
);
232 struct rxe_mc_grp
*mcg
;
233 struct rxe_mc_elem
*mce
;
236 struct sk_buff
*per_qp_skb
;
237 struct rxe_pkt_info
*per_qp_pkt
;
240 if (skb
->protocol
== htons(ETH_P_IP
))
241 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
242 (struct in6_addr
*)&dgid
);
243 else if (skb
->protocol
== htons(ETH_P_IPV6
))
244 memcpy(&dgid
, &ipv6_hdr(skb
)->daddr
, sizeof(dgid
));
246 /* lookup mcast group corresponding to mgid, takes a ref */
247 mcg
= rxe_pool_get_key(&rxe
->mc_grp_pool
, &dgid
);
249 goto err1
; /* mcast group not registered */
251 spin_lock_bh(&mcg
->mcg_lock
);
253 list_for_each_entry(mce
, &mcg
->qp_list
, qp_list
) {
255 pkt
= SKB_TO_PKT(skb
);
257 /* validate qp for incoming packet */
258 err
= check_type_state(rxe
, pkt
, qp
);
262 err
= check_keys(rxe
, pkt
, bth_qpn(pkt
), qp
);
266 /* for all but the last qp create a new clone of the
267 * skb and pass to the qp.
269 if (mce
->qp_list
.next
!= &mcg
->qp_list
)
270 per_qp_skb
= skb_clone(skb
, GFP_ATOMIC
);
274 if (unlikely(!per_qp_skb
))
277 per_qp_pkt
= SKB_TO_PKT(per_qp_skb
);
280 rxe_rcv_pkt(per_qp_pkt
, per_qp_skb
);
283 spin_unlock_bh(&mcg
->mcg_lock
);
285 rxe_drop_ref(mcg
); /* drop ref from rxe_pool_get_key. */
294 * rxe_chk_dgid - validate destination IP address
295 * @rxe: rxe device that received packet
296 * @skb: the received packet buffer
298 * Accept any loopback packets
299 * Extract IP address from packet and
300 * Accept if multicast packet
301 * Accept if matches an SGID table entry
303 static int rxe_chk_dgid(struct rxe_dev
*rxe
, struct sk_buff
*skb
)
305 struct rxe_pkt_info
*pkt
= SKB_TO_PKT(skb
);
306 const struct ib_gid_attr
*gid_attr
;
310 if (pkt
->mask
& RXE_LOOPBACK_MASK
)
313 if (skb
->protocol
== htons(ETH_P_IP
)) {
314 ipv6_addr_set_v4mapped(ip_hdr(skb
)->daddr
,
315 (struct in6_addr
*)&dgid
);
318 pdgid
= (union ib_gid
*)&ipv6_hdr(skb
)->daddr
;
321 if (rdma_is_multicast_addr((struct in6_addr
*)pdgid
))
324 gid_attr
= rdma_find_gid_by_port(&rxe
->ib_dev
, pdgid
,
325 IB_GID_TYPE_ROCE_UDP_ENCAP
,
327 if (IS_ERR(gid_attr
))
328 return PTR_ERR(gid_attr
);
330 rdma_put_gid_attr(gid_attr
);
334 /* rxe_rcv is called from the interface driver */
335 void rxe_rcv(struct sk_buff
*skb
)
338 struct rxe_pkt_info
*pkt
= SKB_TO_PKT(skb
);
339 struct rxe_dev
*rxe
= pkt
->rxe
;
341 u32 calc_icrc
, pack_icrc
;
345 if (unlikely(skb
->len
< pkt
->offset
+ RXE_BTH_BYTES
))
348 if (rxe_chk_dgid(rxe
, skb
) < 0) {
349 pr_warn_ratelimited("failed checking dgid\n");
353 pkt
->opcode
= bth_opcode(pkt
);
354 pkt
->psn
= bth_psn(pkt
);
356 pkt
->mask
|= rxe_opcode
[pkt
->opcode
].mask
;
358 if (unlikely(skb
->len
< header_size(pkt
)))
361 err
= hdr_check(pkt
);
366 icrcp
= (__be32
*)(pkt
->hdr
+ pkt
->paylen
- RXE_ICRC_SIZE
);
367 pack_icrc
= be32_to_cpu(*icrcp
);
369 calc_icrc
= rxe_icrc_hdr(pkt
, skb
);
370 calc_icrc
= rxe_crc32(rxe
, calc_icrc
, (u8
*)payload_addr(pkt
),
371 payload_size(pkt
) + bth_pad(pkt
));
372 calc_icrc
= (__force u32
)cpu_to_be32(~calc_icrc
);
373 if (unlikely(calc_icrc
!= pack_icrc
)) {
374 if (skb
->protocol
== htons(ETH_P_IPV6
))
375 pr_warn_ratelimited("bad ICRC from %pI6c\n",
376 &ipv6_hdr(skb
)->saddr
);
377 else if (skb
->protocol
== htons(ETH_P_IP
))
378 pr_warn_ratelimited("bad ICRC from %pI4\n",
379 &ip_hdr(skb
)->saddr
);
381 pr_warn_ratelimited("bad ICRC from unknown\n");
386 rxe_counter_inc(rxe
, RXE_CNT_RCVD_PKTS
);
388 if (unlikely(bth_qpn(pkt
) == IB_MULTICAST_QPN
))
389 rxe_rcv_mcast_pkt(rxe
, skb
);
391 rxe_rcv_pkt(pkt
, skb
);
397 rxe_drop_ref(pkt
->qp
);