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