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