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