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 static 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 
81 err1:
82 	return -EINVAL;
83 }
84 
85 static 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 
93 static 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 
101 static 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 
129 err1:
130 	return -EINVAL;
131 }
132 
133 static 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 
186 done:
187 	return 0;
188 
189 err1:
190 	return -EINVAL;
191 }
192 
193 static 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 
242 err2:
243 	rxe_drop_ref(qp);
244 err1:
245 	return -EINVAL;
246 }
247 
248 static 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 
256 static 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 	int err;
264 
265 	if (skb->protocol == htons(ETH_P_IP))
266 		ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
267 				       (struct in6_addr *)&dgid);
268 	else if (skb->protocol == htons(ETH_P_IPV6))
269 		memcpy(&dgid, &ipv6_hdr(skb)->daddr, sizeof(dgid));
270 
271 	/* lookup mcast group corresponding to mgid, takes a ref */
272 	mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid);
273 	if (!mcg)
274 		goto err1;	/* mcast group not registered */
275 
276 	spin_lock_bh(&mcg->mcg_lock);
277 
278 	list_for_each_entry(mce, &mcg->qp_list, qp_list) {
279 		qp = mce->qp;
280 		pkt = SKB_TO_PKT(skb);
281 
282 		/* validate qp for incoming packet */
283 		err = check_type_state(rxe, pkt, qp);
284 		if (err)
285 			continue;
286 
287 		err = check_keys(rxe, pkt, bth_qpn(pkt), qp);
288 		if (err)
289 			continue;
290 
291 		/* if *not* the last qp in the list
292 		 * increase the users of the skb then post to the next qp
293 		 */
294 		if (mce->qp_list.next != &mcg->qp_list)
295 			skb_get(skb);
296 
297 		pkt->qp = qp;
298 		rxe_add_ref(qp);
299 		rxe_rcv_pkt(pkt, skb);
300 	}
301 
302 	spin_unlock_bh(&mcg->mcg_lock);
303 
304 	rxe_drop_ref(mcg);	/* drop ref from rxe_pool_get_key. */
305 
306 err1:
307 	kfree_skb(skb);
308 }
309 
310 static int rxe_match_dgid(struct rxe_dev *rxe, struct sk_buff *skb)
311 {
312 	struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
313 	const struct ib_gid_attr *gid_attr;
314 	union ib_gid dgid;
315 	union ib_gid *pdgid;
316 
317 	if (pkt->mask & RXE_LOOPBACK_MASK)
318 		return 0;
319 
320 	if (skb->protocol == htons(ETH_P_IP)) {
321 		ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr,
322 				       (struct in6_addr *)&dgid);
323 		pdgid = &dgid;
324 	} else {
325 		pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr;
326 	}
327 
328 	gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid,
329 					 IB_GID_TYPE_ROCE_UDP_ENCAP,
330 					 1, skb->dev);
331 	if (IS_ERR(gid_attr))
332 		return PTR_ERR(gid_attr);
333 
334 	rdma_put_gid_attr(gid_attr);
335 	return 0;
336 }
337 
338 /* rxe_rcv is called from the interface driver */
339 void rxe_rcv(struct sk_buff *skb)
340 {
341 	int err;
342 	struct rxe_pkt_info *pkt = SKB_TO_PKT(skb);
343 	struct rxe_dev *rxe = pkt->rxe;
344 	__be32 *icrcp;
345 	u32 calc_icrc, pack_icrc;
346 
347 	pkt->offset = 0;
348 
349 	if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES))
350 		goto drop;
351 
352 	if (rxe_match_dgid(rxe, skb) < 0) {
353 		pr_warn_ratelimited("failed matching dgid\n");
354 		goto drop;
355 	}
356 
357 	pkt->opcode = bth_opcode(pkt);
358 	pkt->psn = bth_psn(pkt);
359 	pkt->qp = NULL;
360 	pkt->mask |= rxe_opcode[pkt->opcode].mask;
361 
362 	if (unlikely(skb->len < header_size(pkt)))
363 		goto drop;
364 
365 	err = hdr_check(pkt);
366 	if (unlikely(err))
367 		goto drop;
368 
369 	/* Verify ICRC */
370 	icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE);
371 	pack_icrc = be32_to_cpu(*icrcp);
372 
373 	calc_icrc = rxe_icrc_hdr(pkt, skb);
374 	calc_icrc = rxe_crc32(rxe, calc_icrc, (u8 *)payload_addr(pkt),
375 			      payload_size(pkt) + bth_pad(pkt));
376 	calc_icrc = (__force u32)cpu_to_be32(~calc_icrc);
377 	if (unlikely(calc_icrc != pack_icrc)) {
378 		if (skb->protocol == htons(ETH_P_IPV6))
379 			pr_warn_ratelimited("bad ICRC from %pI6c\n",
380 					    &ipv6_hdr(skb)->saddr);
381 		else if (skb->protocol == htons(ETH_P_IP))
382 			pr_warn_ratelimited("bad ICRC from %pI4\n",
383 					    &ip_hdr(skb)->saddr);
384 		else
385 			pr_warn_ratelimited("bad ICRC from unknown\n");
386 
387 		goto drop;
388 	}
389 
390 	rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS);
391 
392 	if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN))
393 		rxe_rcv_mcast_pkt(rxe, skb);
394 	else
395 		rxe_rcv_pkt(pkt, skb);
396 
397 	return;
398 
399 drop:
400 	if (pkt->qp)
401 		rxe_drop_ref(pkt->qp);
402 
403 	kfree_skb(skb);
404 }
405