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