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