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