1 /*- 2 * Copyright (c) 2020 Mellanox Technologies. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include "opt_inet.h" 27 #include "opt_inet6.h" 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/devctl.h> 35 #include <sys/eventhandler.h> 36 #include <sys/kernel.h> 37 #include <sys/mbuf.h> 38 #include <sys/module.h> 39 #include <sys/socket.h> 40 #include <sys/sysctl.h> 41 42 #include <net/bpf.h> 43 #include <net/ethernet.h> 44 #include <net/infiniband.h> 45 #include <net/if.h> 46 #include <net/if_var.h> 47 #include <net/if_private.h> 48 #include <net/if_dl.h> 49 #include <net/if_media.h> 50 #include <net/if_lagg.h> 51 #include <net/if_llatbl.h> 52 #include <net/if_types.h> 53 #include <net/netisr.h> 54 #include <net/route.h> 55 #include <netinet/if_ether.h> 56 #include <netinet/in.h> 57 #include <netinet/ip6.h> 58 #include <netinet6/in6_var.h> 59 #include <netinet6/nd6.h> 60 61 #include <security/mac/mac_framework.h> 62 63 /* if_lagg(4) support */ 64 struct mbuf *(*lagg_input_infiniband_p)(struct ifnet *, struct mbuf *); 65 66 #ifdef INET 67 static inline void 68 infiniband_ipv4_multicast_map(uint32_t addr, 69 const uint8_t *broadcast, uint8_t *buf) 70 { 71 uint8_t scope; 72 73 addr = ntohl(addr); 74 scope = broadcast[5] & 0xF; 75 76 buf[0] = 0; 77 buf[1] = 0xff; 78 buf[2] = 0xff; 79 buf[3] = 0xff; 80 buf[4] = 0xff; 81 buf[5] = 0x10 | scope; 82 buf[6] = 0x40; 83 buf[7] = 0x1b; 84 buf[8] = broadcast[8]; 85 buf[9] = broadcast[9]; 86 buf[10] = 0; 87 buf[11] = 0; 88 buf[12] = 0; 89 buf[13] = 0; 90 buf[14] = 0; 91 buf[15] = 0; 92 buf[16] = (addr >> 24) & 0xff; 93 buf[17] = (addr >> 16) & 0xff; 94 buf[18] = (addr >> 8) & 0xff; 95 buf[19] = addr & 0xff; 96 } 97 #endif 98 99 #ifdef INET6 100 static inline void 101 infiniband_ipv6_multicast_map(const struct in6_addr *addr, 102 const uint8_t *broadcast, uint8_t *buf) 103 { 104 uint8_t scope; 105 106 scope = broadcast[5] & 0xF; 107 108 buf[0] = 0; 109 buf[1] = 0xff; 110 buf[2] = 0xff; 111 buf[3] = 0xff; 112 buf[4] = 0xff; 113 buf[5] = 0x10 | scope; 114 buf[6] = 0x60; 115 buf[7] = 0x1b; 116 buf[8] = broadcast[8]; 117 buf[9] = broadcast[9]; 118 memcpy(&buf[10], &addr->s6_addr[6], 10); 119 } 120 #endif 121 122 /* 123 * This is for clients that have an infiniband_header in the mbuf. 124 */ 125 void 126 infiniband_bpf_mtap(struct ifnet *ifp, struct mbuf *mb) 127 { 128 struct infiniband_header *ibh; 129 struct ether_header eh; 130 131 if (!bpf_peers_present(ifp->if_bpf)) 132 return; 133 134 if (mb->m_len < sizeof(*ibh)) 135 return; 136 137 ibh = mtod(mb, struct infiniband_header *); 138 eh.ether_type = ibh->ib_protocol; 139 memset(eh.ether_shost, 0, ETHER_ADDR_LEN); 140 memcpy(eh.ether_dhost, ibh->ib_hwaddr + 4, ETHER_ADDR_LEN); 141 mb->m_data += sizeof(*ibh); 142 mb->m_len -= sizeof(*ibh); 143 mb->m_pkthdr.len -= sizeof(*ibh); 144 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb); 145 mb->m_data -= sizeof(*ibh); 146 mb->m_len += sizeof(*ibh); 147 mb->m_pkthdr.len += sizeof(*ibh); 148 } 149 150 static void 151 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 152 { 153 int csum_flags = 0; 154 155 if (src->m_pkthdr.csum_flags & CSUM_IP) 156 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 157 if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 158 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 159 if (src->m_pkthdr.csum_flags & CSUM_SCTP) 160 csum_flags |= CSUM_SCTP_VALID; 161 dst->m_pkthdr.csum_flags |= csum_flags; 162 if (csum_flags & CSUM_DATA_VALID) 163 dst->m_pkthdr.csum_data = 0xffff; 164 } 165 166 /* 167 * Handle link-layer encapsulation requests. 168 */ 169 static int 170 infiniband_requestencap(struct ifnet *ifp, struct if_encap_req *req) 171 { 172 struct infiniband_header *ih; 173 struct arphdr *ah; 174 uint16_t etype; 175 const uint8_t *lladdr; 176 177 if (req->rtype != IFENCAP_LL) 178 return (EOPNOTSUPP); 179 180 if (req->bufsize < INFINIBAND_HDR_LEN) 181 return (ENOMEM); 182 183 ih = (struct infiniband_header *)req->buf; 184 lladdr = req->lladdr; 185 req->lladdr_off = 0; 186 187 switch (req->family) { 188 case AF_INET: 189 etype = htons(ETHERTYPE_IP); 190 break; 191 case AF_INET6: 192 etype = htons(ETHERTYPE_IPV6); 193 break; 194 case AF_ARP: 195 ah = (struct arphdr *)req->hdata; 196 ah->ar_hrd = htons(ARPHRD_INFINIBAND); 197 198 switch (ntohs(ah->ar_op)) { 199 case ARPOP_REVREQUEST: 200 case ARPOP_REVREPLY: 201 etype = htons(ETHERTYPE_REVARP); 202 break; 203 case ARPOP_REQUEST: 204 case ARPOP_REPLY: 205 default: 206 etype = htons(ETHERTYPE_ARP); 207 break; 208 } 209 210 if (req->flags & IFENCAP_FLAG_BROADCAST) 211 lladdr = ifp->if_broadcastaddr; 212 break; 213 default: 214 return (EAFNOSUPPORT); 215 } 216 217 ih->ib_protocol = etype; 218 ih->ib_reserved = 0; 219 memcpy(ih->ib_hwaddr, lladdr, INFINIBAND_ADDR_LEN); 220 req->bufsize = sizeof(struct infiniband_header); 221 222 return (0); 223 } 224 225 static int 226 infiniband_resolve_addr(struct ifnet *ifp, struct mbuf *m, 227 const struct sockaddr *dst, struct route *ro, uint8_t *phdr, 228 uint32_t *pflags, struct llentry **plle) 229 { 230 #if defined(INET) || defined(INET6) 231 struct infiniband_header *ih = (struct infiniband_header *)phdr; 232 #endif 233 uint32_t lleflags = 0; 234 int error = 0; 235 236 if (plle) 237 *plle = NULL; 238 239 switch (dst->sa_family) { 240 #ifdef INET 241 case AF_INET: 242 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) { 243 error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, plle); 244 } else { 245 if (m->m_flags & M_BCAST) { 246 memcpy(ih->ib_hwaddr, ifp->if_broadcastaddr, 247 INFINIBAND_ADDR_LEN); 248 } else { 249 infiniband_ipv4_multicast_map( 250 ((const struct sockaddr_in *)dst)->sin_addr.s_addr, 251 ifp->if_broadcastaddr, ih->ib_hwaddr); 252 } 253 ih->ib_protocol = htons(ETHERTYPE_IP); 254 ih->ib_reserved = 0; 255 } 256 break; 257 #endif 258 #ifdef INET6 259 case AF_INET6: 260 if ((m->m_flags & M_MCAST) == 0) { 261 int af = RO_GET_FAMILY(ro, dst); 262 error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr, 263 &lleflags, plle); 264 } else { 265 infiniband_ipv6_multicast_map( 266 &((const struct sockaddr_in6 *)dst)->sin6_addr, 267 ifp->if_broadcastaddr, ih->ib_hwaddr); 268 ih->ib_protocol = htons(ETHERTYPE_IPV6); 269 ih->ib_reserved = 0; 270 } 271 break; 272 #endif 273 default: 274 if_printf(ifp, "can't handle af%d\n", dst->sa_family); 275 if (m != NULL) 276 m_freem(m); 277 return (EAFNOSUPPORT); 278 } 279 280 if (error == EHOSTDOWN) { 281 if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 282 error = EHOSTUNREACH; 283 } 284 285 if (error != 0) 286 return (error); 287 288 *pflags = RT_MAY_LOOP; 289 if (lleflags & LLE_IFADDR) 290 *pflags |= RT_L2_ME; 291 292 return (0); 293 } 294 295 /* 296 * Infiniband output routine. 297 */ 298 static int 299 infiniband_output(struct ifnet *ifp, struct mbuf *m, 300 const struct sockaddr *dst, struct route *ro) 301 { 302 uint8_t linkhdr[INFINIBAND_HDR_LEN]; 303 uint8_t *phdr; 304 struct llentry *lle = NULL; 305 struct infiniband_header *ih; 306 int error = 0; 307 int hlen; /* link layer header length */ 308 uint32_t pflags; 309 bool addref; 310 311 NET_EPOCH_ASSERT(); 312 313 addref = false; 314 phdr = NULL; 315 pflags = 0; 316 if (ro != NULL) { 317 /* XXX BPF uses ro_prepend */ 318 if (ro->ro_prepend != NULL) { 319 phdr = ro->ro_prepend; 320 hlen = ro->ro_plen; 321 } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 322 if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 323 lle = ro->ro_lle; 324 if (lle != NULL && 325 (lle->la_flags & LLE_VALID) == 0) { 326 LLE_FREE(lle); 327 lle = NULL; /* redundant */ 328 ro->ro_lle = NULL; 329 } 330 if (lle == NULL) { 331 /* if we lookup, keep cache */ 332 addref = 1; 333 } else 334 /* 335 * Notify LLE code that 336 * the entry was used 337 * by datapath. 338 */ 339 llentry_provide_feedback(lle); 340 } 341 if (lle != NULL) { 342 phdr = lle->r_linkdata; 343 hlen = lle->r_hdrlen; 344 pflags = lle->r_flags; 345 } 346 } 347 } 348 349 #ifdef MAC 350 error = mac_ifnet_check_transmit(ifp, m); 351 if (error) 352 goto bad; 353 #endif 354 355 M_PROFILE(m); 356 if (ifp->if_flags & IFF_MONITOR) { 357 error = ENETDOWN; 358 goto bad; 359 } 360 if (!((ifp->if_flags & IFF_UP) && 361 (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 362 error = ENETDOWN; 363 goto bad; 364 } 365 366 if (phdr == NULL) { 367 /* No prepend data supplied. Try to calculate ourselves. */ 368 phdr = linkhdr; 369 hlen = INFINIBAND_HDR_LEN; 370 error = infiniband_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 371 addref ? &lle : NULL); 372 if (addref && lle != NULL) 373 ro->ro_lle = lle; 374 if (error != 0) 375 return (error == EWOULDBLOCK ? 0 : error); 376 } 377 378 if ((pflags & RT_L2_ME) != 0) { 379 update_mbuf_csumflags(m, m); 380 return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0)); 381 } 382 383 /* 384 * Add local infiniband header. If no space in first mbuf, 385 * allocate another. 386 */ 387 M_PREPEND(m, INFINIBAND_HDR_LEN, M_NOWAIT); 388 if (m == NULL) { 389 error = ENOBUFS; 390 goto bad; 391 } 392 if ((pflags & RT_HAS_HEADER) == 0) { 393 ih = mtod(m, struct infiniband_header *); 394 memcpy(ih, phdr, hlen); 395 } 396 397 /* 398 * Queue message on interface, update output statistics if 399 * successful, and start output if interface not yet active. 400 */ 401 return (ifp->if_transmit(ifp, m)); 402 bad: 403 if (m != NULL) 404 m_freem(m); 405 return (error); 406 } 407 408 /* 409 * Process a received Infiniband packet. 410 */ 411 static void 412 infiniband_input(struct ifnet *ifp, struct mbuf *m) 413 { 414 struct infiniband_header *ibh; 415 struct epoch_tracker et; 416 int isr; 417 418 CURVNET_SET_QUIET(ifp->if_vnet); 419 420 if ((ifp->if_flags & IFF_UP) == 0) { 421 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 422 m_freem(m); 423 goto done; 424 } 425 426 ibh = mtod(m, struct infiniband_header *); 427 428 /* 429 * Reset layer specific mbuf flags to avoid confusing upper 430 * layers: 431 */ 432 m->m_flags &= ~M_VLANTAG; 433 m_clrprotoflags(m); 434 435 if (INFINIBAND_IS_MULTICAST(ibh->ib_hwaddr)) { 436 if (memcmp(ibh->ib_hwaddr, ifp->if_broadcastaddr, 437 ifp->if_addrlen) == 0) 438 m->m_flags |= M_BCAST; 439 else 440 m->m_flags |= M_MCAST; 441 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 442 } 443 444 /* Let BPF have it before we strip the header. */ 445 infiniband_bpf_mtap(ifp, m); 446 447 /* Allow monitor mode to claim this frame, after stats are updated. */ 448 if (ifp->if_flags & IFF_MONITOR) { 449 m_freem(m); 450 goto done; 451 } 452 453 /* Direct packet to correct FIB based on interface config. */ 454 M_SETFIB(m, ifp->if_fib); 455 456 /* Handle input from a lagg<N> port */ 457 if (ifp->if_type == IFT_INFINIBANDLAG) { 458 KASSERT(lagg_input_infiniband_p != NULL, 459 ("%s: if_lagg not loaded!", __func__)); 460 m = (*lagg_input_infiniband_p)(ifp, m); 461 if (__predict_false(m == NULL)) 462 goto done; 463 ifp = m->m_pkthdr.rcvif; 464 } 465 466 /* 467 * Dispatch frame to upper layer. 468 */ 469 switch (ibh->ib_protocol) { 470 #ifdef INET 471 case htons(ETHERTYPE_IP): 472 isr = NETISR_IP; 473 break; 474 475 case htons(ETHERTYPE_ARP): 476 if (ifp->if_flags & IFF_NOARP) { 477 /* Discard packet if ARP is disabled on interface */ 478 m_freem(m); 479 goto done; 480 } 481 isr = NETISR_ARP; 482 break; 483 #endif 484 #ifdef INET6 485 case htons(ETHERTYPE_IPV6): 486 isr = NETISR_IPV6; 487 break; 488 #endif 489 default: 490 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 491 m_freem(m); 492 goto done; 493 } 494 495 /* Strip off the Infiniband header. */ 496 m_adj(m, INFINIBAND_HDR_LEN); 497 498 #ifdef MAC 499 /* 500 * Tag the mbuf with an appropriate MAC label before any other 501 * consumers can get to it. 502 */ 503 mac_ifnet_create_mbuf(ifp, m); 504 #endif 505 /* Allow monitor mode to claim this frame, after stats are updated. */ 506 NET_EPOCH_ENTER(et); 507 netisr_dispatch(isr, m); 508 NET_EPOCH_EXIT(et); 509 done: 510 CURVNET_RESTORE(); 511 } 512 513 static int 514 infiniband_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 515 struct sockaddr *sa) 516 { 517 struct sockaddr_dl *sdl; 518 #ifdef INET 519 struct sockaddr_in *sin; 520 #endif 521 #ifdef INET6 522 struct sockaddr_in6 *sin6; 523 #endif 524 uint8_t *e_addr; 525 526 switch (sa->sa_family) { 527 case AF_LINK: 528 /* 529 * No mapping needed. Just check that it's a valid MC address. 530 */ 531 sdl = (struct sockaddr_dl *)sa; 532 e_addr = LLADDR(sdl); 533 if (!INFINIBAND_IS_MULTICAST(e_addr)) 534 return (EADDRNOTAVAIL); 535 *llsa = NULL; 536 return 0; 537 538 #ifdef INET 539 case AF_INET: 540 sin = (struct sockaddr_in *)sa; 541 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 542 return (EADDRNOTAVAIL); 543 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 544 sdl->sdl_alen = INFINIBAND_ADDR_LEN; 545 e_addr = LLADDR(sdl); 546 infiniband_ipv4_multicast_map( 547 sin->sin_addr.s_addr, ifp->if_broadcastaddr, e_addr); 548 *llsa = (struct sockaddr *)sdl; 549 return (0); 550 #endif 551 #ifdef INET6 552 case AF_INET6: 553 sin6 = (struct sockaddr_in6 *)sa; 554 /* 555 * An IP6 address of 0 means listen to all of the 556 * multicast address used for IP6. This has no meaning 557 * in infiniband. 558 */ 559 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 560 return (EADDRNOTAVAIL); 561 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 562 return (EADDRNOTAVAIL); 563 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 564 sdl->sdl_alen = INFINIBAND_ADDR_LEN; 565 e_addr = LLADDR(sdl); 566 infiniband_ipv6_multicast_map( 567 &sin6->sin6_addr, ifp->if_broadcastaddr, e_addr); 568 *llsa = (struct sockaddr *)sdl; 569 return (0); 570 #endif 571 default: 572 return (EAFNOSUPPORT); 573 } 574 } 575 576 void 577 infiniband_ifattach(struct ifnet *ifp, const uint8_t *lla, const uint8_t *llb) 578 { 579 struct sockaddr_dl *sdl; 580 struct ifaddr *ifa; 581 int i; 582 583 ifp->if_addrlen = INFINIBAND_ADDR_LEN; 584 ifp->if_hdrlen = INFINIBAND_HDR_LEN; 585 ifp->if_mtu = INFINIBAND_MTU; 586 if_attach(ifp); 587 ifp->if_output = infiniband_output; 588 ifp->if_input = infiniband_input; 589 ifp->if_resolvemulti = infiniband_resolvemulti; 590 ifp->if_requestencap = infiniband_requestencap; 591 592 if (ifp->if_baudrate == 0) 593 ifp->if_baudrate = IF_Gbps(10); /* default value */ 594 if (llb != NULL) 595 ifp->if_broadcastaddr = llb; 596 597 ifa = ifp->if_addr; 598 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 599 sdl = (struct sockaddr_dl *)ifa->ifa_addr; 600 sdl->sdl_type = IFT_INFINIBAND; 601 sdl->sdl_alen = ifp->if_addrlen; 602 603 if (lla != NULL) { 604 memcpy(LLADDR(sdl), lla, ifp->if_addrlen); 605 606 if (ifp->if_hw_addr != NULL) 607 memcpy(ifp->if_hw_addr, lla, ifp->if_addrlen); 608 } else { 609 lla = LLADDR(sdl); 610 } 611 612 /* Attach ethernet compatible network device */ 613 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 614 615 /* Announce Infiniband MAC address if non-zero. */ 616 for (i = 0; i < ifp->if_addrlen; i++) 617 if (lla[i] != 0) 618 break; 619 if (i != ifp->if_addrlen) 620 if_printf(ifp, "Infiniband address: %20D\n", lla, ":"); 621 622 /* Add necessary bits are setup; announce it now. */ 623 EVENTHANDLER_INVOKE(infiniband_ifattach_event, ifp); 624 625 if (IS_DEFAULT_VNET(curvnet)) 626 devctl_notify("INFINIBAND", ifp->if_xname, "IFATTACH", NULL); 627 } 628 629 /* 630 * Perform common duties while detaching an Infiniband interface 631 */ 632 void 633 infiniband_ifdetach(struct ifnet *ifp) 634 { 635 bpfdetach(ifp); 636 if_detach(ifp); 637 } 638 639 static int 640 infiniband_modevent(module_t mod, int type, void *data) 641 { 642 switch (type) { 643 case MOD_LOAD: 644 case MOD_UNLOAD: 645 return (0); 646 default: 647 return (EOPNOTSUPP); 648 } 649 } 650 651 static moduledata_t infiniband_mod = { 652 .name = "if_infiniband", 653 .evhand = &infiniband_modevent, 654 }; 655 656 DECLARE_MODULE(if_infiniband, infiniband_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 657 MODULE_VERSION(if_infiniband, 1); 658