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 <linux/skbuff.h> 8 9 #include "rxe.h" 10 #include "rxe_loc.h" 11 12 static int check_type_state(struct rxe_dev *rxe, struct rxe_pkt_info *pkt, 13 struct rxe_qp *qp) 14 { 15 if (unlikely(!qp->valid)) 16 goto err1; 17 18 switch (qp_type(qp)) { 19 case IB_QPT_RC: 20 if (unlikely((pkt->opcode & IB_OPCODE_RC) != 0)) { 21 pr_warn_ratelimited("bad qp type\n"); 22 goto err1; 23 } 24 break; 25 case IB_QPT_UC: 26 if (unlikely(!(pkt->opcode & IB_OPCODE_UC))) { 27 pr_warn_ratelimited("bad qp type\n"); 28 goto err1; 29 } 30 break; 31 case IB_QPT_UD: 32 case IB_QPT_SMI: 33 case IB_QPT_GSI: 34 if (unlikely(!(pkt->opcode & IB_OPCODE_UD))) { 35 pr_warn_ratelimited("bad qp type\n"); 36 goto err1; 37 } 38 break; 39 default: 40 pr_warn_ratelimited("unsupported qp type\n"); 41 goto err1; 42 } 43 44 if (pkt->mask & RXE_REQ_MASK) { 45 if (unlikely(qp->resp.state != QP_STATE_READY)) 46 goto err1; 47 } else if (unlikely(qp->req.state < QP_STATE_READY || 48 qp->req.state > QP_STATE_DRAINED)) { 49 goto err1; 50 } 51 52 return 0; 53 54 err1: 55 return -EINVAL; 56 } 57 58 static void set_bad_pkey_cntr(struct rxe_port *port) 59 { 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); 64 } 65 66 static void set_qkey_viol_cntr(struct rxe_port *port) 67 { 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); 72 } 73 74 static int check_keys(struct rxe_dev *rxe, struct rxe_pkt_info *pkt, 75 u32 qpn, struct rxe_qp *qp) 76 { 77 struct rxe_port *port = &rxe->port; 78 u16 pkey = bth_pkey(pkt); 79 80 pkt->pkey_index = 0; 81 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); 85 goto err1; 86 } 87 88 if ((qp_type(qp) == IB_QPT_UD || qp_type(qp) == IB_QPT_GSI) && 89 pkt->mask) { 90 u32 qkey = (qpn == 1) ? GSI_QKEY : qp->attr.qkey; 91 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); 96 goto err1; 97 } 98 } 99 100 return 0; 101 102 err1: 103 return -EINVAL; 104 } 105 106 static int check_addr(struct rxe_dev *rxe, struct rxe_pkt_info *pkt, 107 struct rxe_qp *qp) 108 { 109 struct sk_buff *skb = PKT_TO_SKB(pkt); 110 111 if (qp_type(qp) != IB_QPT_RC && qp_type(qp) != IB_QPT_UC) 112 goto done; 113 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); 117 goto err1; 118 } 119 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; 125 126 if (ip_hdr(skb)->daddr != saddr->s_addr) { 127 pr_warn_ratelimited("dst addr %pI4 != qp source addr %pI4\n", 128 &ip_hdr(skb)->daddr, 129 &saddr->s_addr); 130 goto err1; 131 } 132 133 if (ip_hdr(skb)->saddr != daddr->s_addr) { 134 pr_warn_ratelimited("source addr %pI4 != qp dst addr %pI4\n", 135 &ip_hdr(skb)->saddr, 136 &daddr->s_addr); 137 goto err1; 138 } 139 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; 145 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); 149 goto err1; 150 } 151 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); 155 goto err1; 156 } 157 } 158 159 done: 160 return 0; 161 162 err1: 163 return -EINVAL; 164 } 165 166 static int hdr_check(struct rxe_pkt_info *pkt) 167 { 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); 172 int index; 173 int err; 174 175 if (unlikely(bth_tver(pkt) != BTH_TVER)) { 176 pr_warn_ratelimited("bad tver\n"); 177 goto err1; 178 } 179 180 if (unlikely(qpn == 0)) { 181 pr_warn_once("QP 0 not supported"); 182 goto err1; 183 } 184 185 if (qpn != IB_MULTICAST_QPN) { 186 index = (qpn == 1) ? port->qp_gsi_index : qpn; 187 188 qp = rxe_pool_get_index(&rxe->qp_pool, index); 189 if (unlikely(!qp)) { 190 pr_warn_ratelimited("no qp matches qpn 0x%x\n", qpn); 191 goto err1; 192 } 193 194 err = check_type_state(rxe, pkt, qp); 195 if (unlikely(err)) 196 goto err2; 197 198 err = check_addr(rxe, pkt, qp); 199 if (unlikely(err)) 200 goto err2; 201 202 err = check_keys(rxe, pkt, qpn, qp); 203 if (unlikely(err)) 204 goto err2; 205 } else { 206 if (unlikely((pkt->mask & RXE_GRH_MASK) == 0)) { 207 pr_warn_ratelimited("no grh for mcast qpn\n"); 208 goto err1; 209 } 210 } 211 212 pkt->qp = qp; 213 return 0; 214 215 err2: 216 rxe_drop_ref(qp); 217 err1: 218 return -EINVAL; 219 } 220 221 static inline void rxe_rcv_pkt(struct rxe_pkt_info *pkt, struct sk_buff *skb) 222 { 223 if (pkt->mask & RXE_REQ_MASK) 224 rxe_resp_queue_pkt(pkt->qp, skb); 225 else 226 rxe_comp_queue_pkt(pkt->qp, skb); 227 } 228 229 static void rxe_rcv_mcast_pkt(struct rxe_dev *rxe, struct sk_buff *skb) 230 { 231 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb); 232 struct rxe_mc_grp *mcg; 233 struct rxe_mc_elem *mce; 234 struct rxe_qp *qp; 235 union ib_gid dgid; 236 struct sk_buff *per_qp_skb; 237 struct rxe_pkt_info *per_qp_pkt; 238 int err; 239 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)); 245 246 /* lookup mcast group corresponding to mgid, takes a ref */ 247 mcg = rxe_pool_get_key(&rxe->mc_grp_pool, &dgid); 248 if (!mcg) 249 goto err1; /* mcast group not registered */ 250 251 spin_lock_bh(&mcg->mcg_lock); 252 253 list_for_each_entry(mce, &mcg->qp_list, qp_list) { 254 qp = mce->qp; 255 pkt = SKB_TO_PKT(skb); 256 257 /* validate qp for incoming packet */ 258 err = check_type_state(rxe, pkt, qp); 259 if (err) 260 continue; 261 262 err = check_keys(rxe, pkt, bth_qpn(pkt), qp); 263 if (err) 264 continue; 265 266 /* for all but the last qp create a new clone of the 267 * skb and pass to the qp. 268 */ 269 if (mce->qp_list.next != &mcg->qp_list) 270 per_qp_skb = skb_clone(skb, GFP_ATOMIC); 271 else 272 per_qp_skb = skb; 273 274 if (unlikely(!per_qp_skb)) 275 continue; 276 277 per_qp_pkt = SKB_TO_PKT(per_qp_skb); 278 per_qp_pkt->qp = qp; 279 rxe_add_ref(qp); 280 rxe_rcv_pkt(per_qp_pkt, per_qp_skb); 281 } 282 283 spin_unlock_bh(&mcg->mcg_lock); 284 285 rxe_drop_ref(mcg); /* drop ref from rxe_pool_get_key. */ 286 287 return; 288 289 err1: 290 kfree_skb(skb); 291 } 292 293 /** 294 * rxe_chk_dgid - validate destination IP address 295 * @rxe: rxe device that received packet 296 * @skb: the received packet buffer 297 * 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 302 */ 303 static int rxe_chk_dgid(struct rxe_dev *rxe, struct sk_buff *skb) 304 { 305 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb); 306 const struct ib_gid_attr *gid_attr; 307 union ib_gid dgid; 308 union ib_gid *pdgid; 309 310 if (pkt->mask & RXE_LOOPBACK_MASK) 311 return 0; 312 313 if (skb->protocol == htons(ETH_P_IP)) { 314 ipv6_addr_set_v4mapped(ip_hdr(skb)->daddr, 315 (struct in6_addr *)&dgid); 316 pdgid = &dgid; 317 } else { 318 pdgid = (union ib_gid *)&ipv6_hdr(skb)->daddr; 319 } 320 321 if (rdma_is_multicast_addr((struct in6_addr *)pdgid)) 322 return 0; 323 324 gid_attr = rdma_find_gid_by_port(&rxe->ib_dev, pdgid, 325 IB_GID_TYPE_ROCE_UDP_ENCAP, 326 1, skb->dev); 327 if (IS_ERR(gid_attr)) 328 return PTR_ERR(gid_attr); 329 330 rdma_put_gid_attr(gid_attr); 331 return 0; 332 } 333 334 /* rxe_rcv is called from the interface driver */ 335 void rxe_rcv(struct sk_buff *skb) 336 { 337 int err; 338 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb); 339 struct rxe_dev *rxe = pkt->rxe; 340 __be32 *icrcp; 341 u32 calc_icrc, pack_icrc; 342 343 pkt->offset = 0; 344 345 if (unlikely(skb->len < pkt->offset + RXE_BTH_BYTES)) 346 goto drop; 347 348 if (rxe_chk_dgid(rxe, skb) < 0) { 349 pr_warn_ratelimited("failed checking dgid\n"); 350 goto drop; 351 } 352 353 pkt->opcode = bth_opcode(pkt); 354 pkt->psn = bth_psn(pkt); 355 pkt->qp = NULL; 356 pkt->mask |= rxe_opcode[pkt->opcode].mask; 357 358 if (unlikely(skb->len < header_size(pkt))) 359 goto drop; 360 361 err = hdr_check(pkt); 362 if (unlikely(err)) 363 goto drop; 364 365 /* Verify ICRC */ 366 icrcp = (__be32 *)(pkt->hdr + pkt->paylen - RXE_ICRC_SIZE); 367 pack_icrc = be32_to_cpu(*icrcp); 368 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); 380 else 381 pr_warn_ratelimited("bad ICRC from unknown\n"); 382 383 goto drop; 384 } 385 386 rxe_counter_inc(rxe, RXE_CNT_RCVD_PKTS); 387 388 if (unlikely(bth_qpn(pkt) == IB_MULTICAST_QPN)) 389 rxe_rcv_mcast_pkt(rxe, skb); 390 else 391 rxe_rcv_pkt(pkt, skb); 392 393 return; 394 395 drop: 396 if (pkt->qp) 397 rxe_drop_ref(pkt->qp); 398 399 kfree_skb(skb); 400 } 401