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 #include <linux/if_arp.h> 9 #include <linux/netdevice.h> 10 #include <linux/if.h> 11 #include <linux/if_vlan.h> 12 #include <net/udp_tunnel.h> 13 #include <net/sch_generic.h> 14 #include <linux/netfilter.h> 15 #include <rdma/ib_addr.h> 16 17 #include "rxe.h" 18 #include "rxe_net.h" 19 #include "rxe_loc.h" 20 21 static struct rxe_recv_sockets recv_sockets; 22 23 static struct dst_entry *rxe_find_route4(struct rxe_qp *qp, 24 struct net_device *ndev, 25 struct in_addr *saddr, 26 struct in_addr *daddr) 27 { 28 struct rtable *rt; 29 struct flowi4 fl = { { 0 } }; 30 31 memset(&fl, 0, sizeof(fl)); 32 fl.flowi4_oif = ndev->ifindex; 33 memcpy(&fl.saddr, saddr, sizeof(*saddr)); 34 memcpy(&fl.daddr, daddr, sizeof(*daddr)); 35 fl.flowi4_proto = IPPROTO_UDP; 36 37 rt = ip_route_output_key(&init_net, &fl); 38 if (IS_ERR(rt)) { 39 rxe_dbg_qp(qp, "no route to %pI4\n", &daddr->s_addr); 40 return NULL; 41 } 42 43 return &rt->dst; 44 } 45 46 #if IS_ENABLED(CONFIG_IPV6) 47 static struct dst_entry *rxe_find_route6(struct rxe_qp *qp, 48 struct net_device *ndev, 49 struct in6_addr *saddr, 50 struct in6_addr *daddr) 51 { 52 struct dst_entry *ndst; 53 struct flowi6 fl6 = { { 0 } }; 54 55 memset(&fl6, 0, sizeof(fl6)); 56 fl6.flowi6_oif = ndev->ifindex; 57 memcpy(&fl6.saddr, saddr, sizeof(*saddr)); 58 memcpy(&fl6.daddr, daddr, sizeof(*daddr)); 59 fl6.flowi6_proto = IPPROTO_UDP; 60 61 ndst = ipv6_stub->ipv6_dst_lookup_flow(sock_net(recv_sockets.sk6->sk), 62 recv_sockets.sk6->sk, &fl6, 63 NULL); 64 if (IS_ERR(ndst)) { 65 rxe_dbg_qp(qp, "no route to %pI6\n", daddr); 66 return NULL; 67 } 68 69 if (unlikely(ndst->error)) { 70 rxe_dbg_qp(qp, "no route to %pI6\n", daddr); 71 goto put; 72 } 73 74 return ndst; 75 put: 76 dst_release(ndst); 77 return NULL; 78 } 79 80 #else 81 82 static struct dst_entry *rxe_find_route6(struct rxe_qp *qp, 83 struct net_device *ndev, 84 struct in6_addr *saddr, 85 struct in6_addr *daddr) 86 { 87 return NULL; 88 } 89 90 #endif 91 92 static struct dst_entry *rxe_find_route(struct net_device *ndev, 93 struct rxe_qp *qp, 94 struct rxe_av *av) 95 { 96 struct dst_entry *dst = NULL; 97 98 if (qp_type(qp) == IB_QPT_RC) 99 dst = sk_dst_get(qp->sk->sk); 100 101 if (!dst || !dst_check(dst, qp->dst_cookie)) { 102 if (dst) 103 dst_release(dst); 104 105 if (av->network_type == RXE_NETWORK_TYPE_IPV4) { 106 struct in_addr *saddr; 107 struct in_addr *daddr; 108 109 saddr = &av->sgid_addr._sockaddr_in.sin_addr; 110 daddr = &av->dgid_addr._sockaddr_in.sin_addr; 111 dst = rxe_find_route4(qp, ndev, saddr, daddr); 112 } else if (av->network_type == RXE_NETWORK_TYPE_IPV6) { 113 struct in6_addr *saddr6; 114 struct in6_addr *daddr6; 115 116 saddr6 = &av->sgid_addr._sockaddr_in6.sin6_addr; 117 daddr6 = &av->dgid_addr._sockaddr_in6.sin6_addr; 118 dst = rxe_find_route6(qp, ndev, saddr6, daddr6); 119 #if IS_ENABLED(CONFIG_IPV6) 120 if (dst) 121 qp->dst_cookie = 122 rt6_get_cookie((struct rt6_info *)dst); 123 #endif 124 } 125 126 if (dst && (qp_type(qp) == IB_QPT_RC)) { 127 dst_hold(dst); 128 sk_dst_set(qp->sk->sk, dst); 129 } 130 } 131 return dst; 132 } 133 134 static int rxe_udp_encap_recv(struct sock *sk, struct sk_buff *skb) 135 { 136 struct udphdr *udph; 137 struct rxe_dev *rxe; 138 struct net_device *ndev = skb->dev; 139 struct rxe_pkt_info *pkt = SKB_TO_PKT(skb); 140 141 /* takes a reference on rxe->ib_dev 142 * drop when skb is freed 143 */ 144 rxe = rxe_get_dev_from_net(ndev); 145 if (!rxe && is_vlan_dev(ndev)) 146 rxe = rxe_get_dev_from_net(vlan_dev_real_dev(ndev)); 147 if (!rxe) 148 goto drop; 149 150 if (skb_linearize(skb)) { 151 ib_device_put(&rxe->ib_dev); 152 goto drop; 153 } 154 155 udph = udp_hdr(skb); 156 pkt->rxe = rxe; 157 pkt->port_num = 1; 158 pkt->hdr = (u8 *)(udph + 1); 159 pkt->mask = RXE_GRH_MASK; 160 pkt->paylen = be16_to_cpu(udph->len) - sizeof(*udph); 161 162 /* remove udp header */ 163 skb_pull(skb, sizeof(struct udphdr)); 164 165 rxe_rcv(skb); 166 167 return 0; 168 drop: 169 kfree_skb(skb); 170 171 return 0; 172 } 173 174 static struct socket *rxe_setup_udp_tunnel(struct net *net, __be16 port, 175 bool ipv6) 176 { 177 int err; 178 struct socket *sock; 179 struct udp_port_cfg udp_cfg = { }; 180 struct udp_tunnel_sock_cfg tnl_cfg = { }; 181 182 if (ipv6) { 183 udp_cfg.family = AF_INET6; 184 udp_cfg.ipv6_v6only = 1; 185 } else { 186 udp_cfg.family = AF_INET; 187 } 188 189 udp_cfg.local_udp_port = port; 190 191 /* Create UDP socket */ 192 err = udp_sock_create(net, &udp_cfg, &sock); 193 if (err < 0) 194 return ERR_PTR(err); 195 196 tnl_cfg.encap_type = 1; 197 tnl_cfg.encap_rcv = rxe_udp_encap_recv; 198 199 /* Setup UDP tunnel */ 200 setup_udp_tunnel_sock(net, sock, &tnl_cfg); 201 202 return sock; 203 } 204 205 static void rxe_release_udp_tunnel(struct socket *sk) 206 { 207 if (sk) 208 udp_tunnel_sock_release(sk); 209 } 210 211 static void prepare_udp_hdr(struct sk_buff *skb, __be16 src_port, 212 __be16 dst_port) 213 { 214 struct udphdr *udph; 215 216 __skb_push(skb, sizeof(*udph)); 217 skb_reset_transport_header(skb); 218 udph = udp_hdr(skb); 219 220 udph->dest = dst_port; 221 udph->source = src_port; 222 udph->len = htons(skb->len); 223 udph->check = 0; 224 } 225 226 static void prepare_ipv4_hdr(struct dst_entry *dst, struct sk_buff *skb, 227 __be32 saddr, __be32 daddr, __u8 proto, 228 __u8 tos, __u8 ttl, __be16 df, bool xnet) 229 { 230 struct iphdr *iph; 231 232 skb_scrub_packet(skb, xnet); 233 234 skb_clear_hash(skb); 235 skb_dst_set(skb, dst_clone(dst)); 236 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 237 238 skb_push(skb, sizeof(struct iphdr)); 239 skb_reset_network_header(skb); 240 241 iph = ip_hdr(skb); 242 243 iph->version = IPVERSION; 244 iph->ihl = sizeof(struct iphdr) >> 2; 245 iph->tot_len = htons(skb->len); 246 iph->frag_off = df; 247 iph->protocol = proto; 248 iph->tos = tos; 249 iph->daddr = daddr; 250 iph->saddr = saddr; 251 iph->ttl = ttl; 252 __ip_select_ident(dev_net(dst->dev), iph, 253 skb_shinfo(skb)->gso_segs ?: 1); 254 } 255 256 static void prepare_ipv6_hdr(struct dst_entry *dst, struct sk_buff *skb, 257 struct in6_addr *saddr, struct in6_addr *daddr, 258 __u8 proto, __u8 prio, __u8 ttl) 259 { 260 struct ipv6hdr *ip6h; 261 262 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 263 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED 264 | IPSKB_REROUTED); 265 skb_dst_set(skb, dst_clone(dst)); 266 267 __skb_push(skb, sizeof(*ip6h)); 268 skb_reset_network_header(skb); 269 ip6h = ipv6_hdr(skb); 270 ip6_flow_hdr(ip6h, prio, htonl(0)); 271 ip6h->payload_len = htons(skb->len); 272 ip6h->nexthdr = proto; 273 ip6h->hop_limit = ttl; 274 ip6h->daddr = *daddr; 275 ip6h->saddr = *saddr; 276 ip6h->payload_len = htons(skb->len - sizeof(*ip6h)); 277 } 278 279 static int prepare4(struct rxe_av *av, struct rxe_pkt_info *pkt, 280 struct sk_buff *skb) 281 { 282 struct rxe_qp *qp = pkt->qp; 283 struct dst_entry *dst; 284 bool xnet = false; 285 __be16 df = htons(IP_DF); 286 struct in_addr *saddr = &av->sgid_addr._sockaddr_in.sin_addr; 287 struct in_addr *daddr = &av->dgid_addr._sockaddr_in.sin_addr; 288 289 dst = rxe_find_route(skb->dev, qp, av); 290 if (!dst) { 291 rxe_dbg_qp(qp, "Host not reachable\n"); 292 return -EHOSTUNREACH; 293 } 294 295 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port), 296 cpu_to_be16(ROCE_V2_UDP_DPORT)); 297 298 prepare_ipv4_hdr(dst, skb, saddr->s_addr, daddr->s_addr, IPPROTO_UDP, 299 av->grh.traffic_class, av->grh.hop_limit, df, xnet); 300 301 dst_release(dst); 302 return 0; 303 } 304 305 static int prepare6(struct rxe_av *av, struct rxe_pkt_info *pkt, 306 struct sk_buff *skb) 307 { 308 struct rxe_qp *qp = pkt->qp; 309 struct dst_entry *dst; 310 struct in6_addr *saddr = &av->sgid_addr._sockaddr_in6.sin6_addr; 311 struct in6_addr *daddr = &av->dgid_addr._sockaddr_in6.sin6_addr; 312 313 dst = rxe_find_route(skb->dev, qp, av); 314 if (!dst) { 315 rxe_dbg_qp(qp, "Host not reachable\n"); 316 return -EHOSTUNREACH; 317 } 318 319 prepare_udp_hdr(skb, cpu_to_be16(qp->src_port), 320 cpu_to_be16(ROCE_V2_UDP_DPORT)); 321 322 prepare_ipv6_hdr(dst, skb, saddr, daddr, IPPROTO_UDP, 323 av->grh.traffic_class, 324 av->grh.hop_limit); 325 326 dst_release(dst); 327 return 0; 328 } 329 330 int rxe_prepare(struct rxe_av *av, struct rxe_pkt_info *pkt, 331 struct sk_buff *skb) 332 { 333 int err = 0; 334 335 if (skb->protocol == htons(ETH_P_IP)) 336 err = prepare4(av, pkt, skb); 337 else if (skb->protocol == htons(ETH_P_IPV6)) 338 err = prepare6(av, pkt, skb); 339 340 if (ether_addr_equal(skb->dev->dev_addr, av->dmac)) 341 pkt->mask |= RXE_LOOPBACK_MASK; 342 343 return err; 344 } 345 346 static void rxe_skb_tx_dtor(struct sk_buff *skb) 347 { 348 struct sock *sk = skb->sk; 349 struct rxe_qp *qp = sk->sk_user_data; 350 int skb_out = atomic_dec_return(&qp->skb_out); 351 352 if (unlikely(qp->need_req_skb && 353 skb_out < RXE_INFLIGHT_SKBS_PER_QP_LOW)) 354 rxe_sched_task(&qp->req.task); 355 356 rxe_put(qp); 357 } 358 359 static int rxe_send(struct sk_buff *skb, struct rxe_pkt_info *pkt) 360 { 361 int err; 362 363 skb->destructor = rxe_skb_tx_dtor; 364 skb->sk = pkt->qp->sk->sk; 365 366 rxe_get(pkt->qp); 367 atomic_inc(&pkt->qp->skb_out); 368 369 if (skb->protocol == htons(ETH_P_IP)) 370 err = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 371 else 372 err = ip6_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 373 374 if (unlikely(net_xmit_eval(err))) { 375 rxe_dbg_qp(pkt->qp, "error sending packet: %d\n", err); 376 return -EAGAIN; 377 } 378 379 return 0; 380 } 381 382 /* fix up a send packet to match the packets 383 * received from UDP before looping them back 384 */ 385 static int rxe_loopback(struct sk_buff *skb, struct rxe_pkt_info *pkt) 386 { 387 memcpy(SKB_TO_PKT(skb), pkt, sizeof(*pkt)); 388 389 if (skb->protocol == htons(ETH_P_IP)) 390 skb_pull(skb, sizeof(struct iphdr)); 391 else 392 skb_pull(skb, sizeof(struct ipv6hdr)); 393 394 if (WARN_ON(!ib_device_try_get(&pkt->rxe->ib_dev))) { 395 kfree_skb(skb); 396 return -EIO; 397 } 398 399 /* remove udp header */ 400 skb_pull(skb, sizeof(struct udphdr)); 401 402 rxe_rcv(skb); 403 404 return 0; 405 } 406 407 int rxe_xmit_packet(struct rxe_qp *qp, struct rxe_pkt_info *pkt, 408 struct sk_buff *skb) 409 { 410 int err; 411 int is_request = pkt->mask & RXE_REQ_MASK; 412 struct rxe_dev *rxe = to_rdev(qp->ibqp.device); 413 unsigned long flags; 414 415 spin_lock_irqsave(&qp->state_lock, flags); 416 if ((is_request && (qp_state(qp) < IB_QPS_RTS)) || 417 (!is_request && (qp_state(qp) < IB_QPS_RTR))) { 418 spin_unlock_irqrestore(&qp->state_lock, flags); 419 rxe_dbg_qp(qp, "Packet dropped. QP is not in ready state\n"); 420 goto drop; 421 } 422 spin_unlock_irqrestore(&qp->state_lock, flags); 423 424 rxe_icrc_generate(skb, pkt); 425 426 if (pkt->mask & RXE_LOOPBACK_MASK) 427 err = rxe_loopback(skb, pkt); 428 else 429 err = rxe_send(skb, pkt); 430 if (err) { 431 rxe_counter_inc(rxe, RXE_CNT_SEND_ERR); 432 return err; 433 } 434 435 if ((qp_type(qp) != IB_QPT_RC) && 436 (pkt->mask & RXE_END_MASK)) { 437 pkt->wqe->state = wqe_state_done; 438 rxe_sched_task(&qp->comp.task); 439 } 440 441 rxe_counter_inc(rxe, RXE_CNT_SENT_PKTS); 442 goto done; 443 444 drop: 445 kfree_skb(skb); 446 err = 0; 447 done: 448 return err; 449 } 450 451 struct sk_buff *rxe_init_packet(struct rxe_dev *rxe, struct rxe_av *av, 452 int paylen, struct rxe_pkt_info *pkt) 453 { 454 unsigned int hdr_len; 455 struct sk_buff *skb = NULL; 456 struct net_device *ndev; 457 const struct ib_gid_attr *attr; 458 const int port_num = 1; 459 460 attr = rdma_get_gid_attr(&rxe->ib_dev, port_num, av->grh.sgid_index); 461 if (IS_ERR(attr)) 462 return NULL; 463 464 if (av->network_type == RXE_NETWORK_TYPE_IPV4) 465 hdr_len = ETH_HLEN + sizeof(struct udphdr) + 466 sizeof(struct iphdr); 467 else 468 hdr_len = ETH_HLEN + sizeof(struct udphdr) + 469 sizeof(struct ipv6hdr); 470 471 rcu_read_lock(); 472 ndev = rdma_read_gid_attr_ndev_rcu(attr); 473 if (IS_ERR(ndev)) { 474 rcu_read_unlock(); 475 goto out; 476 } 477 skb = alloc_skb(paylen + hdr_len + LL_RESERVED_SPACE(ndev), 478 GFP_ATOMIC); 479 480 if (unlikely(!skb)) { 481 rcu_read_unlock(); 482 goto out; 483 } 484 485 skb_reserve(skb, hdr_len + LL_RESERVED_SPACE(ndev)); 486 487 /* FIXME: hold reference to this netdev until life of this skb. */ 488 skb->dev = ndev; 489 rcu_read_unlock(); 490 491 if (av->network_type == RXE_NETWORK_TYPE_IPV4) 492 skb->protocol = htons(ETH_P_IP); 493 else 494 skb->protocol = htons(ETH_P_IPV6); 495 496 pkt->rxe = rxe; 497 pkt->port_num = port_num; 498 pkt->hdr = skb_put(skb, paylen); 499 pkt->mask |= RXE_GRH_MASK; 500 501 out: 502 rdma_put_gid_attr(attr); 503 return skb; 504 } 505 506 /* 507 * this is required by rxe_cfg to match rxe devices in 508 * /sys/class/infiniband up with their underlying ethernet devices 509 */ 510 const char *rxe_parent_name(struct rxe_dev *rxe, unsigned int port_num) 511 { 512 return rxe->ndev->name; 513 } 514 515 int rxe_net_add(const char *ibdev_name, struct net_device *ndev) 516 { 517 int err; 518 struct rxe_dev *rxe = NULL; 519 520 rxe = ib_alloc_device(rxe_dev, ib_dev); 521 if (!rxe) 522 return -ENOMEM; 523 524 rxe->ndev = ndev; 525 526 err = rxe_add(rxe, ndev->mtu, ibdev_name); 527 if (err) { 528 ib_dealloc_device(&rxe->ib_dev); 529 return err; 530 } 531 532 return 0; 533 } 534 535 static void rxe_port_event(struct rxe_dev *rxe, 536 enum ib_event_type event) 537 { 538 struct ib_event ev; 539 540 ev.device = &rxe->ib_dev; 541 ev.element.port_num = 1; 542 ev.event = event; 543 544 ib_dispatch_event(&ev); 545 } 546 547 /* Caller must hold net_info_lock */ 548 void rxe_port_up(struct rxe_dev *rxe) 549 { 550 struct rxe_port *port; 551 552 port = &rxe->port; 553 port->attr.state = IB_PORT_ACTIVE; 554 555 rxe_port_event(rxe, IB_EVENT_PORT_ACTIVE); 556 dev_info(&rxe->ib_dev.dev, "set active\n"); 557 } 558 559 /* Caller must hold net_info_lock */ 560 void rxe_port_down(struct rxe_dev *rxe) 561 { 562 struct rxe_port *port; 563 564 port = &rxe->port; 565 port->attr.state = IB_PORT_DOWN; 566 567 rxe_port_event(rxe, IB_EVENT_PORT_ERR); 568 rxe_counter_inc(rxe, RXE_CNT_LINK_DOWNED); 569 dev_info(&rxe->ib_dev.dev, "set down\n"); 570 } 571 572 void rxe_set_port_state(struct rxe_dev *rxe) 573 { 574 if (netif_running(rxe->ndev) && netif_carrier_ok(rxe->ndev)) 575 rxe_port_up(rxe); 576 else 577 rxe_port_down(rxe); 578 } 579 580 static int rxe_notify(struct notifier_block *not_blk, 581 unsigned long event, 582 void *arg) 583 { 584 struct net_device *ndev = netdev_notifier_info_to_dev(arg); 585 struct rxe_dev *rxe = rxe_get_dev_from_net(ndev); 586 587 if (!rxe) 588 return NOTIFY_OK; 589 590 switch (event) { 591 case NETDEV_UNREGISTER: 592 ib_unregister_device_queued(&rxe->ib_dev); 593 break; 594 case NETDEV_UP: 595 rxe_port_up(rxe); 596 break; 597 case NETDEV_DOWN: 598 rxe_port_down(rxe); 599 break; 600 case NETDEV_CHANGEMTU: 601 rxe_dbg_dev(rxe, "%s changed mtu to %d\n", ndev->name, ndev->mtu); 602 rxe_set_mtu(rxe, ndev->mtu); 603 break; 604 case NETDEV_CHANGE: 605 rxe_set_port_state(rxe); 606 break; 607 case NETDEV_REBOOT: 608 case NETDEV_GOING_DOWN: 609 case NETDEV_CHANGEADDR: 610 case NETDEV_CHANGENAME: 611 case NETDEV_FEAT_CHANGE: 612 default: 613 rxe_dbg_dev(rxe, "ignoring netdev event = %ld for %s\n", 614 event, ndev->name); 615 break; 616 } 617 618 ib_device_put(&rxe->ib_dev); 619 return NOTIFY_OK; 620 } 621 622 static struct notifier_block rxe_net_notifier = { 623 .notifier_call = rxe_notify, 624 }; 625 626 static int rxe_net_ipv4_init(void) 627 { 628 recv_sockets.sk4 = rxe_setup_udp_tunnel(&init_net, 629 htons(ROCE_V2_UDP_DPORT), false); 630 if (IS_ERR(recv_sockets.sk4)) { 631 recv_sockets.sk4 = NULL; 632 pr_err("Failed to create IPv4 UDP tunnel\n"); 633 return -1; 634 } 635 636 return 0; 637 } 638 639 static int rxe_net_ipv6_init(void) 640 { 641 #if IS_ENABLED(CONFIG_IPV6) 642 643 recv_sockets.sk6 = rxe_setup_udp_tunnel(&init_net, 644 htons(ROCE_V2_UDP_DPORT), true); 645 if (PTR_ERR(recv_sockets.sk6) == -EAFNOSUPPORT) { 646 recv_sockets.sk6 = NULL; 647 pr_warn("IPv6 is not supported, can not create a UDPv6 socket\n"); 648 return 0; 649 } 650 651 if (IS_ERR(recv_sockets.sk6)) { 652 recv_sockets.sk6 = NULL; 653 pr_err("Failed to create IPv6 UDP tunnel\n"); 654 return -1; 655 } 656 #endif 657 return 0; 658 } 659 660 void rxe_net_exit(void) 661 { 662 rxe_release_udp_tunnel(recv_sockets.sk6); 663 rxe_release_udp_tunnel(recv_sockets.sk4); 664 unregister_netdevice_notifier(&rxe_net_notifier); 665 } 666 667 int rxe_net_init(void) 668 { 669 int err; 670 671 recv_sockets.sk6 = NULL; 672 673 err = rxe_net_ipv4_init(); 674 if (err) 675 return err; 676 err = rxe_net_ipv6_init(); 677 if (err) 678 goto err_out; 679 err = register_netdevice_notifier(&rxe_net_notifier); 680 if (err) { 681 pr_err("Failed to register netdev notifier\n"); 682 goto err_out; 683 } 684 return 0; 685 err_out: 686 rxe_net_exit(); 687 return err; 688 } 689