19d40cf60SHans Petter Selasky /*- 29d40cf60SHans Petter Selasky * Copyright (c) 2020 Mellanox Technologies. All rights reserved. 39d40cf60SHans Petter Selasky * 49d40cf60SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 59d40cf60SHans Petter Selasky * modification, are permitted provided that the following conditions 69d40cf60SHans Petter Selasky * are met: 79d40cf60SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 89d40cf60SHans Petter Selasky * notice, this list of conditions and the following disclaimer. 99d40cf60SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 109d40cf60SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 119d40cf60SHans Petter Selasky * documentation and/or other materials provided with the distribution. 129d40cf60SHans Petter Selasky * 139d40cf60SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 149d40cf60SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 159d40cf60SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 169d40cf60SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 179d40cf60SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 189d40cf60SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 199d40cf60SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 209d40cf60SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 219d40cf60SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 229d40cf60SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 239d40cf60SHans Petter Selasky * SUCH DAMAGE. 249d40cf60SHans Petter Selasky */ 259d40cf60SHans Petter Selasky 269d40cf60SHans Petter Selasky #include "opt_inet.h" 279d40cf60SHans Petter Selasky #include "opt_inet6.h" 289d40cf60SHans Petter Selasky 299d40cf60SHans Petter Selasky #include <sys/cdefs.h> 309d40cf60SHans Petter Selasky __FBSDID("$FreeBSD$"); 319d40cf60SHans Petter Selasky 32ce329aa2SHans Petter Selasky #include <sys/param.h> 331d3a22e7SHans Petter Selasky #include <sys/systm.h> 3401630a49SHans Petter Selasky #include <sys/devctl.h> 359d40cf60SHans Petter Selasky #include <sys/eventhandler.h> 3601630a49SHans Petter Selasky #include <sys/kernel.h> 3701630a49SHans Petter Selasky #include <sys/mbuf.h> 3801630a49SHans Petter Selasky #include <sys/module.h> 399d40cf60SHans Petter Selasky #include <sys/socket.h> 409d40cf60SHans Petter Selasky #include <sys/sysctl.h> 419d40cf60SHans Petter Selasky 4201630a49SHans Petter Selasky #include <net/bpf.h> 439d40cf60SHans Petter Selasky #include <net/ethernet.h> 449d40cf60SHans Petter Selasky #include <net/infiniband.h> 4501630a49SHans Petter Selasky #include <net/if.h> 4601630a49SHans Petter Selasky #include <net/if_var.h> 47*2c2b37adSJustin Hibbits #include <net/if_private.h> 489d40cf60SHans Petter Selasky #include <net/if_dl.h> 499d40cf60SHans Petter Selasky #include <net/if_media.h> 50a92c4bb6SHans Petter Selasky #include <net/if_lagg.h> 5101630a49SHans Petter Selasky #include <net/if_llatbl.h> 5201630a49SHans Petter Selasky #include <net/if_types.h> 5301630a49SHans Petter Selasky #include <net/netisr.h> 5401630a49SHans Petter Selasky #include <net/route.h> 559d40cf60SHans Petter Selasky #include <netinet/if_ether.h> 5601630a49SHans Petter Selasky #include <netinet/in.h> 579d40cf60SHans Petter Selasky #include <netinet/ip6.h> 589d40cf60SHans Petter Selasky #include <netinet6/in6_var.h> 599d40cf60SHans Petter Selasky #include <netinet6/nd6.h> 609d40cf60SHans Petter Selasky 619d40cf60SHans Petter Selasky #include <security/mac/mac_framework.h> 629d40cf60SHans Petter Selasky 63a92c4bb6SHans Petter Selasky /* if_lagg(4) support */ 64a92c4bb6SHans Petter Selasky struct mbuf *(*lagg_input_infiniband_p)(struct ifnet *, struct mbuf *); 65a92c4bb6SHans Petter Selasky 669d40cf60SHans Petter Selasky #ifdef INET 679d40cf60SHans Petter Selasky static inline void 681355e2dcSHans Petter Selasky infiniband_ipv4_multicast_map(uint32_t addr, 691355e2dcSHans Petter Selasky const uint8_t *broadcast, uint8_t *buf) 709d40cf60SHans Petter Selasky { 719d40cf60SHans Petter Selasky uint8_t scope; 729d40cf60SHans Petter Selasky 739d40cf60SHans Petter Selasky addr = ntohl(addr); 749d40cf60SHans Petter Selasky scope = broadcast[5] & 0xF; 759d40cf60SHans Petter Selasky 769d40cf60SHans Petter Selasky buf[0] = 0; 779d40cf60SHans Petter Selasky buf[1] = 0xff; 789d40cf60SHans Petter Selasky buf[2] = 0xff; 799d40cf60SHans Petter Selasky buf[3] = 0xff; 809d40cf60SHans Petter Selasky buf[4] = 0xff; 819d40cf60SHans Petter Selasky buf[5] = 0x10 | scope; 829d40cf60SHans Petter Selasky buf[6] = 0x40; 839d40cf60SHans Petter Selasky buf[7] = 0x1b; 849d40cf60SHans Petter Selasky buf[8] = broadcast[8]; 859d40cf60SHans Petter Selasky buf[9] = broadcast[9]; 869d40cf60SHans Petter Selasky buf[10] = 0; 879d40cf60SHans Petter Selasky buf[11] = 0; 889d40cf60SHans Petter Selasky buf[12] = 0; 899d40cf60SHans Petter Selasky buf[13] = 0; 909d40cf60SHans Petter Selasky buf[14] = 0; 919d40cf60SHans Petter Selasky buf[15] = 0; 929d40cf60SHans Petter Selasky buf[16] = (addr >> 24) & 0xff; 939d40cf60SHans Petter Selasky buf[17] = (addr >> 16) & 0xff; 949d40cf60SHans Petter Selasky buf[18] = (addr >> 8) & 0xff; 959d40cf60SHans Petter Selasky buf[19] = addr & 0xff; 969d40cf60SHans Petter Selasky } 979d40cf60SHans Petter Selasky #endif 989d40cf60SHans Petter Selasky 999d40cf60SHans Petter Selasky #ifdef INET6 1009d40cf60SHans Petter Selasky static inline void 1011355e2dcSHans Petter Selasky infiniband_ipv6_multicast_map(const struct in6_addr *addr, 1021355e2dcSHans Petter Selasky const uint8_t *broadcast, uint8_t *buf) 1039d40cf60SHans Petter Selasky { 1049d40cf60SHans Petter Selasky uint8_t scope; 1059d40cf60SHans Petter Selasky 1069d40cf60SHans Petter Selasky scope = broadcast[5] & 0xF; 1079d40cf60SHans Petter Selasky 1089d40cf60SHans Petter Selasky buf[0] = 0; 1099d40cf60SHans Petter Selasky buf[1] = 0xff; 1109d40cf60SHans Petter Selasky buf[2] = 0xff; 1119d40cf60SHans Petter Selasky buf[3] = 0xff; 1129d40cf60SHans Petter Selasky buf[4] = 0xff; 1139d40cf60SHans Petter Selasky buf[5] = 0x10 | scope; 1149d40cf60SHans Petter Selasky buf[6] = 0x60; 1159d40cf60SHans Petter Selasky buf[7] = 0x1b; 1169d40cf60SHans Petter Selasky buf[8] = broadcast[8]; 1179d40cf60SHans Petter Selasky buf[9] = broadcast[9]; 1189d40cf60SHans Petter Selasky memcpy(&buf[10], &addr->s6_addr[6], 10); 1199d40cf60SHans Petter Selasky } 1209d40cf60SHans Petter Selasky #endif 1219d40cf60SHans Petter Selasky 1229d40cf60SHans Petter Selasky /* 1239d40cf60SHans Petter Selasky * This is for clients that have an infiniband_header in the mbuf. 1249d40cf60SHans Petter Selasky */ 1259d40cf60SHans Petter Selasky void 1269d40cf60SHans Petter Selasky infiniband_bpf_mtap(struct ifnet *ifp, struct mbuf *mb) 1279d40cf60SHans Petter Selasky { 1289d40cf60SHans Petter Selasky struct infiniband_header *ibh; 1299d40cf60SHans Petter Selasky struct ether_header eh; 1309d40cf60SHans Petter Selasky 1319d40cf60SHans Petter Selasky if (mb->m_len < sizeof(*ibh)) 1329d40cf60SHans Petter Selasky return; 1339d40cf60SHans Petter Selasky 1349d40cf60SHans Petter Selasky ibh = mtod(mb, struct infiniband_header *); 1359d40cf60SHans Petter Selasky eh.ether_type = ibh->ib_protocol; 1369d40cf60SHans Petter Selasky memset(eh.ether_shost, 0, ETHER_ADDR_LEN); 1379d40cf60SHans Petter Selasky memcpy(eh.ether_dhost, ibh->ib_hwaddr + 4, ETHER_ADDR_LEN); 1389d40cf60SHans Petter Selasky mb->m_data += sizeof(*ibh); 1399d40cf60SHans Petter Selasky mb->m_len -= sizeof(*ibh); 1409d40cf60SHans Petter Selasky mb->m_pkthdr.len -= sizeof(*ibh); 1419d40cf60SHans Petter Selasky bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb); 1429d40cf60SHans Petter Selasky mb->m_data -= sizeof(*ibh); 1439d40cf60SHans Petter Selasky mb->m_len += sizeof(*ibh); 1449d40cf60SHans Petter Selasky mb->m_pkthdr.len += sizeof(*ibh); 1459d40cf60SHans Petter Selasky } 1469d40cf60SHans Petter Selasky 147ec52ff6dSHans Petter Selasky static void 148ec52ff6dSHans Petter Selasky update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst) 149ec52ff6dSHans Petter Selasky { 150ec52ff6dSHans Petter Selasky int csum_flags = 0; 151ec52ff6dSHans Petter Selasky 152ec52ff6dSHans Petter Selasky if (src->m_pkthdr.csum_flags & CSUM_IP) 153ec52ff6dSHans Petter Selasky csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID); 154ec52ff6dSHans Petter Selasky if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 155ec52ff6dSHans Petter Selasky csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR); 156ec52ff6dSHans Petter Selasky if (src->m_pkthdr.csum_flags & CSUM_SCTP) 157ec52ff6dSHans Petter Selasky csum_flags |= CSUM_SCTP_VALID; 158ec52ff6dSHans Petter Selasky dst->m_pkthdr.csum_flags |= csum_flags; 159ec52ff6dSHans Petter Selasky if (csum_flags & CSUM_DATA_VALID) 160ec52ff6dSHans Petter Selasky dst->m_pkthdr.csum_data = 0xffff; 161ec52ff6dSHans Petter Selasky } 162ec52ff6dSHans Petter Selasky 163ec52ff6dSHans Petter Selasky /* 164ec52ff6dSHans Petter Selasky * Handle link-layer encapsulation requests. 165ec52ff6dSHans Petter Selasky */ 166ec52ff6dSHans Petter Selasky static int 167ec52ff6dSHans Petter Selasky infiniband_requestencap(struct ifnet *ifp, struct if_encap_req *req) 168ec52ff6dSHans Petter Selasky { 169ec52ff6dSHans Petter Selasky struct infiniband_header *ih; 170ec52ff6dSHans Petter Selasky struct arphdr *ah; 171ec52ff6dSHans Petter Selasky uint16_t etype; 172ec52ff6dSHans Petter Selasky const uint8_t *lladdr; 173ec52ff6dSHans Petter Selasky 174ec52ff6dSHans Petter Selasky if (req->rtype != IFENCAP_LL) 175ec52ff6dSHans Petter Selasky return (EOPNOTSUPP); 176ec52ff6dSHans Petter Selasky 177ec52ff6dSHans Petter Selasky if (req->bufsize < INFINIBAND_HDR_LEN) 178ec52ff6dSHans Petter Selasky return (ENOMEM); 179ec52ff6dSHans Petter Selasky 180ec52ff6dSHans Petter Selasky ih = (struct infiniband_header *)req->buf; 181ec52ff6dSHans Petter Selasky lladdr = req->lladdr; 182ec52ff6dSHans Petter Selasky req->lladdr_off = 0; 183ec52ff6dSHans Petter Selasky 184ec52ff6dSHans Petter Selasky switch (req->family) { 185ec52ff6dSHans Petter Selasky case AF_INET: 186ec52ff6dSHans Petter Selasky etype = htons(ETHERTYPE_IP); 187ec52ff6dSHans Petter Selasky break; 188ec52ff6dSHans Petter Selasky case AF_INET6: 189ec52ff6dSHans Petter Selasky etype = htons(ETHERTYPE_IPV6); 190ec52ff6dSHans Petter Selasky break; 191ec52ff6dSHans Petter Selasky case AF_ARP: 192ec52ff6dSHans Petter Selasky ah = (struct arphdr *)req->hdata; 193ec52ff6dSHans Petter Selasky ah->ar_hrd = htons(ARPHRD_INFINIBAND); 194ec52ff6dSHans Petter Selasky 195ec52ff6dSHans Petter Selasky switch (ntohs(ah->ar_op)) { 196ec52ff6dSHans Petter Selasky case ARPOP_REVREQUEST: 197ec52ff6dSHans Petter Selasky case ARPOP_REVREPLY: 198ec52ff6dSHans Petter Selasky etype = htons(ETHERTYPE_REVARP); 199ec52ff6dSHans Petter Selasky break; 200ec52ff6dSHans Petter Selasky case ARPOP_REQUEST: 201ec52ff6dSHans Petter Selasky case ARPOP_REPLY: 202ec52ff6dSHans Petter Selasky default: 203ec52ff6dSHans Petter Selasky etype = htons(ETHERTYPE_ARP); 204ec52ff6dSHans Petter Selasky break; 205ec52ff6dSHans Petter Selasky } 206ec52ff6dSHans Petter Selasky 207ec52ff6dSHans Petter Selasky if (req->flags & IFENCAP_FLAG_BROADCAST) 208ec52ff6dSHans Petter Selasky lladdr = ifp->if_broadcastaddr; 209ec52ff6dSHans Petter Selasky break; 210ec52ff6dSHans Petter Selasky default: 211ec52ff6dSHans Petter Selasky return (EAFNOSUPPORT); 212ec52ff6dSHans Petter Selasky } 213ec52ff6dSHans Petter Selasky 214ec52ff6dSHans Petter Selasky ih->ib_protocol = etype; 215ec52ff6dSHans Petter Selasky ih->ib_reserved = 0; 216ec52ff6dSHans Petter Selasky memcpy(ih->ib_hwaddr, lladdr, INFINIBAND_ADDR_LEN); 217ec52ff6dSHans Petter Selasky req->bufsize = sizeof(struct infiniband_header); 218ec52ff6dSHans Petter Selasky 219ec52ff6dSHans Petter Selasky return (0); 220ec52ff6dSHans Petter Selasky } 221ec52ff6dSHans Petter Selasky 222ec52ff6dSHans Petter Selasky static int 223ec52ff6dSHans Petter Selasky infiniband_resolve_addr(struct ifnet *ifp, struct mbuf *m, 224ec52ff6dSHans Petter Selasky const struct sockaddr *dst, struct route *ro, uint8_t *phdr, 225ec52ff6dSHans Petter Selasky uint32_t *pflags, struct llentry **plle) 226ec52ff6dSHans Petter Selasky { 227ac3e46faSJohn Baldwin #if defined(INET) || defined(INET6) 228ac3e46faSJohn Baldwin struct infiniband_header *ih = (struct infiniband_header *)phdr; 229ac3e46faSJohn Baldwin #endif 230ec52ff6dSHans Petter Selasky uint32_t lleflags = 0; 231ec52ff6dSHans Petter Selasky int error = 0; 232ec52ff6dSHans Petter Selasky 233ec52ff6dSHans Petter Selasky if (plle) 234ec52ff6dSHans Petter Selasky *plle = NULL; 235ec52ff6dSHans Petter Selasky 236ec52ff6dSHans Petter Selasky switch (dst->sa_family) { 237ec52ff6dSHans Petter Selasky #ifdef INET 238ec52ff6dSHans Petter Selasky case AF_INET: 239ec52ff6dSHans Petter Selasky if ((m->m_flags & (M_BCAST | M_MCAST)) == 0) { 240ec52ff6dSHans Petter Selasky error = arpresolve(ifp, 0, m, dst, phdr, &lleflags, plle); 241ec52ff6dSHans Petter Selasky } else { 242ec52ff6dSHans Petter Selasky if (m->m_flags & M_BCAST) { 243ec52ff6dSHans Petter Selasky memcpy(ih->ib_hwaddr, ifp->if_broadcastaddr, 244ec52ff6dSHans Petter Selasky INFINIBAND_ADDR_LEN); 245ec52ff6dSHans Petter Selasky } else { 246ec52ff6dSHans Petter Selasky infiniband_ipv4_multicast_map( 247ec52ff6dSHans Petter Selasky ((const struct sockaddr_in *)dst)->sin_addr.s_addr, 248ec52ff6dSHans Petter Selasky ifp->if_broadcastaddr, ih->ib_hwaddr); 249ec52ff6dSHans Petter Selasky } 250ec52ff6dSHans Petter Selasky ih->ib_protocol = htons(ETHERTYPE_IP); 251ec52ff6dSHans Petter Selasky ih->ib_reserved = 0; 252ec52ff6dSHans Petter Selasky } 253ec52ff6dSHans Petter Selasky break; 254ec52ff6dSHans Petter Selasky #endif 255ec52ff6dSHans Petter Selasky #ifdef INET6 256ec52ff6dSHans Petter Selasky case AF_INET6: 257ec52ff6dSHans Petter Selasky if ((m->m_flags & M_MCAST) == 0) { 25862e1a437SZhenlei Huang int af = RO_GET_FAMILY(ro, dst); 25962e1a437SZhenlei Huang error = nd6_resolve(ifp, LLE_SF(af, 0), m, dst, phdr, 26062e1a437SZhenlei Huang &lleflags, plle); 261ec52ff6dSHans Petter Selasky } else { 262ec52ff6dSHans Petter Selasky infiniband_ipv6_multicast_map( 263ec52ff6dSHans Petter Selasky &((const struct sockaddr_in6 *)dst)->sin6_addr, 264ec52ff6dSHans Petter Selasky ifp->if_broadcastaddr, ih->ib_hwaddr); 265ec52ff6dSHans Petter Selasky ih->ib_protocol = htons(ETHERTYPE_IPV6); 266ec52ff6dSHans Petter Selasky ih->ib_reserved = 0; 267ec52ff6dSHans Petter Selasky } 268ec52ff6dSHans Petter Selasky break; 269ec52ff6dSHans Petter Selasky #endif 270ec52ff6dSHans Petter Selasky default: 271ec52ff6dSHans Petter Selasky if_printf(ifp, "can't handle af%d\n", dst->sa_family); 272ec52ff6dSHans Petter Selasky if (m != NULL) 273ec52ff6dSHans Petter Selasky m_freem(m); 274ec52ff6dSHans Petter Selasky return (EAFNOSUPPORT); 275ec52ff6dSHans Petter Selasky } 276ec52ff6dSHans Petter Selasky 277ec52ff6dSHans Petter Selasky if (error == EHOSTDOWN) { 278ec52ff6dSHans Petter Selasky if (ro != NULL && (ro->ro_flags & RT_HAS_GW) != 0) 279ec52ff6dSHans Petter Selasky error = EHOSTUNREACH; 280ec52ff6dSHans Petter Selasky } 281ec52ff6dSHans Petter Selasky 282ec52ff6dSHans Petter Selasky if (error != 0) 283ec52ff6dSHans Petter Selasky return (error); 284ec52ff6dSHans Petter Selasky 285ec52ff6dSHans Petter Selasky *pflags = RT_MAY_LOOP; 286ec52ff6dSHans Petter Selasky if (lleflags & LLE_IFADDR) 287ec52ff6dSHans Petter Selasky *pflags |= RT_L2_ME; 288ec52ff6dSHans Petter Selasky 289ec52ff6dSHans Petter Selasky return (0); 290ec52ff6dSHans Petter Selasky } 291ec52ff6dSHans Petter Selasky 2929d40cf60SHans Petter Selasky /* 2939d40cf60SHans Petter Selasky * Infiniband output routine. 2949d40cf60SHans Petter Selasky */ 2959d40cf60SHans Petter Selasky static int 296ec52ff6dSHans Petter Selasky infiniband_output(struct ifnet *ifp, struct mbuf *m, 297ec52ff6dSHans Petter Selasky const struct sockaddr *dst, struct route *ro) 2989d40cf60SHans Petter Selasky { 299ec52ff6dSHans Petter Selasky uint8_t linkhdr[INFINIBAND_HDR_LEN]; 300ec52ff6dSHans Petter Selasky uint8_t *phdr; 3019d40cf60SHans Petter Selasky struct llentry *lle = NULL; 302ec52ff6dSHans Petter Selasky struct infiniband_header *ih; 3039d40cf60SHans Petter Selasky int error = 0; 304ec52ff6dSHans Petter Selasky int hlen; /* link layer header length */ 305ec52ff6dSHans Petter Selasky uint32_t pflags; 306ec52ff6dSHans Petter Selasky bool addref; 3079d40cf60SHans Petter Selasky 3089d40cf60SHans Petter Selasky NET_EPOCH_ASSERT(); 3099d40cf60SHans Petter Selasky 310ec52ff6dSHans Petter Selasky addref = false; 311ec52ff6dSHans Petter Selasky phdr = NULL; 312ec52ff6dSHans Petter Selasky pflags = 0; 313ec52ff6dSHans Petter Selasky if (ro != NULL) { 314ec52ff6dSHans Petter Selasky /* XXX BPF uses ro_prepend */ 315ec52ff6dSHans Petter Selasky if (ro->ro_prepend != NULL) { 316ec52ff6dSHans Petter Selasky phdr = ro->ro_prepend; 317ec52ff6dSHans Petter Selasky hlen = ro->ro_plen; 318ec52ff6dSHans Petter Selasky } else if (!(m->m_flags & (M_BCAST | M_MCAST))) { 319ec52ff6dSHans Petter Selasky if ((ro->ro_flags & RT_LLE_CACHE) != 0) { 320ec52ff6dSHans Petter Selasky lle = ro->ro_lle; 321ec52ff6dSHans Petter Selasky if (lle != NULL && 322ec52ff6dSHans Petter Selasky (lle->la_flags & LLE_VALID) == 0) { 323ec52ff6dSHans Petter Selasky LLE_FREE(lle); 324ec52ff6dSHans Petter Selasky lle = NULL; /* redundant */ 325ec52ff6dSHans Petter Selasky ro->ro_lle = NULL; 326ec52ff6dSHans Petter Selasky } 327ec52ff6dSHans Petter Selasky if (lle == NULL) { 328ec52ff6dSHans Petter Selasky /* if we lookup, keep cache */ 329ec52ff6dSHans Petter Selasky addref = 1; 330ec52ff6dSHans Petter Selasky } else 331ec52ff6dSHans Petter Selasky /* 332ec52ff6dSHans Petter Selasky * Notify LLE code that 333ec52ff6dSHans Petter Selasky * the entry was used 334ec52ff6dSHans Petter Selasky * by datapath. 335ec52ff6dSHans Petter Selasky */ 336f3a3b061SAlexander V. Chernikov llentry_provide_feedback(lle); 337ec52ff6dSHans Petter Selasky } 338ec52ff6dSHans Petter Selasky if (lle != NULL) { 339ec52ff6dSHans Petter Selasky phdr = lle->r_linkdata; 340ec52ff6dSHans Petter Selasky hlen = lle->r_hdrlen; 341ec52ff6dSHans Petter Selasky pflags = lle->r_flags; 342ec52ff6dSHans Petter Selasky } 343ec52ff6dSHans Petter Selasky } 344ec52ff6dSHans Petter Selasky } 3459d40cf60SHans Petter Selasky 3469d40cf60SHans Petter Selasky #ifdef MAC 3479d40cf60SHans Petter Selasky error = mac_ifnet_check_transmit(ifp, m); 3489d40cf60SHans Petter Selasky if (error) 3499d40cf60SHans Petter Selasky goto bad; 3509d40cf60SHans Petter Selasky #endif 3519d40cf60SHans Petter Selasky 3529d40cf60SHans Petter Selasky M_PROFILE(m); 3539d40cf60SHans Petter Selasky if (ifp->if_flags & IFF_MONITOR) { 3549d40cf60SHans Petter Selasky error = ENETDOWN; 3559d40cf60SHans Petter Selasky goto bad; 3569d40cf60SHans Petter Selasky } 3579d40cf60SHans Petter Selasky if (!((ifp->if_flags & IFF_UP) && 3589d40cf60SHans Petter Selasky (ifp->if_drv_flags & IFF_DRV_RUNNING))) { 3599d40cf60SHans Petter Selasky error = ENETDOWN; 3609d40cf60SHans Petter Selasky goto bad; 3619d40cf60SHans Petter Selasky } 3629d40cf60SHans Petter Selasky 363ec52ff6dSHans Petter Selasky if (phdr == NULL) { 364ec52ff6dSHans Petter Selasky /* No prepend data supplied. Try to calculate ourselves. */ 365ec52ff6dSHans Petter Selasky phdr = linkhdr; 366ec52ff6dSHans Petter Selasky hlen = INFINIBAND_HDR_LEN; 367ec52ff6dSHans Petter Selasky error = infiniband_resolve_addr(ifp, m, dst, ro, phdr, &pflags, 368ec52ff6dSHans Petter Selasky addref ? &lle : NULL); 369ec52ff6dSHans Petter Selasky if (addref && lle != NULL) 370ec52ff6dSHans Petter Selasky ro->ro_lle = lle; 371ec52ff6dSHans Petter Selasky if (error != 0) 372ec52ff6dSHans Petter Selasky return (error == EWOULDBLOCK ? 0 : error); 3739d40cf60SHans Petter Selasky } 3749d40cf60SHans Petter Selasky 375ec52ff6dSHans Petter Selasky if ((pflags & RT_L2_ME) != 0) { 376ec52ff6dSHans Petter Selasky update_mbuf_csumflags(m, m); 37762e1a437SZhenlei Huang return (if_simloop(ifp, m, RO_GET_FAMILY(ro, dst), 0)); 3789d40cf60SHans Petter Selasky } 3799d40cf60SHans Petter Selasky 3809d40cf60SHans Petter Selasky /* 381ec52ff6dSHans Petter Selasky * Add local infiniband header. If no space in first mbuf, 3829d40cf60SHans Petter Selasky * allocate another. 3839d40cf60SHans Petter Selasky */ 3849d40cf60SHans Petter Selasky M_PREPEND(m, INFINIBAND_HDR_LEN, M_NOWAIT); 3859d40cf60SHans Petter Selasky if (m == NULL) { 3869d40cf60SHans Petter Selasky error = ENOBUFS; 3879d40cf60SHans Petter Selasky goto bad; 3889d40cf60SHans Petter Selasky } 389ec52ff6dSHans Petter Selasky if ((pflags & RT_HAS_HEADER) == 0) { 390ec52ff6dSHans Petter Selasky ih = mtod(m, struct infiniband_header *); 391ec52ff6dSHans Petter Selasky memcpy(ih, phdr, hlen); 392ec52ff6dSHans Petter Selasky } 3939d40cf60SHans Petter Selasky 3949d40cf60SHans Petter Selasky /* 3959d40cf60SHans Petter Selasky * Queue message on interface, update output statistics if 3969d40cf60SHans Petter Selasky * successful, and start output if interface not yet active. 3979d40cf60SHans Petter Selasky */ 3989d40cf60SHans Petter Selasky return (ifp->if_transmit(ifp, m)); 3999d40cf60SHans Petter Selasky bad: 4009d40cf60SHans Petter Selasky if (m != NULL) 4019d40cf60SHans Petter Selasky m_freem(m); 4029d40cf60SHans Petter Selasky return (error); 4039d40cf60SHans Petter Selasky } 4049d40cf60SHans Petter Selasky 4059d40cf60SHans Petter Selasky /* 4069d40cf60SHans Petter Selasky * Process a received Infiniband packet. 4079d40cf60SHans Petter Selasky */ 4089d40cf60SHans Petter Selasky static void 4099d40cf60SHans Petter Selasky infiniband_input(struct ifnet *ifp, struct mbuf *m) 4109d40cf60SHans Petter Selasky { 4119d40cf60SHans Petter Selasky struct infiniband_header *ibh; 4129d40cf60SHans Petter Selasky struct epoch_tracker et; 4139d40cf60SHans Petter Selasky int isr; 4149d40cf60SHans Petter Selasky 4159d40cf60SHans Petter Selasky CURVNET_SET_QUIET(ifp->if_vnet); 4169d40cf60SHans Petter Selasky 4179d40cf60SHans Petter Selasky if ((ifp->if_flags & IFF_UP) == 0) { 4189d40cf60SHans Petter Selasky if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 4199d40cf60SHans Petter Selasky m_freem(m); 4209d40cf60SHans Petter Selasky goto done; 4219d40cf60SHans Petter Selasky } 4229d40cf60SHans Petter Selasky 4239d40cf60SHans Petter Selasky ibh = mtod(m, struct infiniband_header *); 4249d40cf60SHans Petter Selasky 4259d40cf60SHans Petter Selasky /* 4269d40cf60SHans Petter Selasky * Reset layer specific mbuf flags to avoid confusing upper 4279d40cf60SHans Petter Selasky * layers: 4289d40cf60SHans Petter Selasky */ 4299d40cf60SHans Petter Selasky m->m_flags &= ~M_VLANTAG; 4309d40cf60SHans Petter Selasky m_clrprotoflags(m); 4319d40cf60SHans Petter Selasky 4329d40cf60SHans Petter Selasky if (INFINIBAND_IS_MULTICAST(ibh->ib_hwaddr)) { 4339d40cf60SHans Petter Selasky if (memcmp(ibh->ib_hwaddr, ifp->if_broadcastaddr, 4349d40cf60SHans Petter Selasky ifp->if_addrlen) == 0) 4359d40cf60SHans Petter Selasky m->m_flags |= M_BCAST; 4369d40cf60SHans Petter Selasky else 4379d40cf60SHans Petter Selasky m->m_flags |= M_MCAST; 4389d40cf60SHans Petter Selasky if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 4399d40cf60SHans Petter Selasky } 4409d40cf60SHans Petter Selasky 4419d40cf60SHans Petter Selasky /* Let BPF have it before we strip the header. */ 4429d40cf60SHans Petter Selasky INFINIBAND_BPF_MTAP(ifp, m); 4439d40cf60SHans Petter Selasky 4449d40cf60SHans Petter Selasky /* Allow monitor mode to claim this frame, after stats are updated. */ 4459d40cf60SHans Petter Selasky if (ifp->if_flags & IFF_MONITOR) { 4469d40cf60SHans Petter Selasky m_freem(m); 4479d40cf60SHans Petter Selasky goto done; 4489d40cf60SHans Petter Selasky } 4499d40cf60SHans Petter Selasky 4509d40cf60SHans Petter Selasky /* Direct packet to correct FIB based on interface config. */ 4519d40cf60SHans Petter Selasky M_SETFIB(m, ifp->if_fib); 4529d40cf60SHans Petter Selasky 453a92c4bb6SHans Petter Selasky /* Handle input from a lagg<N> port */ 454a92c4bb6SHans Petter Selasky if (ifp->if_type == IFT_INFINIBANDLAG) { 455a92c4bb6SHans Petter Selasky KASSERT(lagg_input_infiniband_p != NULL, 456a92c4bb6SHans Petter Selasky ("%s: if_lagg not loaded!", __func__)); 457a92c4bb6SHans Petter Selasky m = (*lagg_input_infiniband_p)(ifp, m); 458a92c4bb6SHans Petter Selasky if (__predict_false(m == NULL)) 459a92c4bb6SHans Petter Selasky goto done; 460a92c4bb6SHans Petter Selasky ifp = m->m_pkthdr.rcvif; 461a92c4bb6SHans Petter Selasky } 462a92c4bb6SHans Petter Selasky 4639d40cf60SHans Petter Selasky /* 4649d40cf60SHans Petter Selasky * Dispatch frame to upper layer. 4659d40cf60SHans Petter Selasky */ 4669d40cf60SHans Petter Selasky switch (ibh->ib_protocol) { 4679d40cf60SHans Petter Selasky #ifdef INET 4689d40cf60SHans Petter Selasky case htons(ETHERTYPE_IP): 4699d40cf60SHans Petter Selasky isr = NETISR_IP; 4709d40cf60SHans Petter Selasky break; 4719d40cf60SHans Petter Selasky 4729d40cf60SHans Petter Selasky case htons(ETHERTYPE_ARP): 4739d40cf60SHans Petter Selasky if (ifp->if_flags & IFF_NOARP) { 4749d40cf60SHans Petter Selasky /* Discard packet if ARP is disabled on interface */ 4759d40cf60SHans Petter Selasky m_freem(m); 4769d40cf60SHans Petter Selasky goto done; 4779d40cf60SHans Petter Selasky } 4789d40cf60SHans Petter Selasky isr = NETISR_ARP; 4799d40cf60SHans Petter Selasky break; 4809d40cf60SHans Petter Selasky #endif 4819d40cf60SHans Petter Selasky #ifdef INET6 4829d40cf60SHans Petter Selasky case htons(ETHERTYPE_IPV6): 4839d40cf60SHans Petter Selasky isr = NETISR_IPV6; 4849d40cf60SHans Petter Selasky break; 4859d40cf60SHans Petter Selasky #endif 4869d40cf60SHans Petter Selasky default: 4879d40cf60SHans Petter Selasky if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 4889d40cf60SHans Petter Selasky m_freem(m); 4899d40cf60SHans Petter Selasky goto done; 4909d40cf60SHans Petter Selasky } 4919d40cf60SHans Petter Selasky 4929d40cf60SHans Petter Selasky /* Strip off the Infiniband header. */ 4939d40cf60SHans Petter Selasky m_adj(m, INFINIBAND_HDR_LEN); 4949d40cf60SHans Petter Selasky 4959d40cf60SHans Petter Selasky #ifdef MAC 4969d40cf60SHans Petter Selasky /* 4979d40cf60SHans Petter Selasky * Tag the mbuf with an appropriate MAC label before any other 4989d40cf60SHans Petter Selasky * consumers can get to it. 4999d40cf60SHans Petter Selasky */ 5009d40cf60SHans Petter Selasky mac_ifnet_create_mbuf(ifp, m); 5019d40cf60SHans Petter Selasky #endif 5029d40cf60SHans Petter Selasky /* Allow monitor mode to claim this frame, after stats are updated. */ 5039d40cf60SHans Petter Selasky NET_EPOCH_ENTER(et); 5049d40cf60SHans Petter Selasky netisr_dispatch(isr, m); 5059d40cf60SHans Petter Selasky NET_EPOCH_EXIT(et); 5069d40cf60SHans Petter Selasky done: 5079d40cf60SHans Petter Selasky CURVNET_RESTORE(); 5089d40cf60SHans Petter Selasky } 5099d40cf60SHans Petter Selasky 5109d40cf60SHans Petter Selasky static int 5111355e2dcSHans Petter Selasky infiniband_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 5121355e2dcSHans Petter Selasky struct sockaddr *sa) 5139d40cf60SHans Petter Selasky { 5149d40cf60SHans Petter Selasky struct sockaddr_dl *sdl; 5159d40cf60SHans Petter Selasky #ifdef INET 5169d40cf60SHans Petter Selasky struct sockaddr_in *sin; 5179d40cf60SHans Petter Selasky #endif 5189d40cf60SHans Petter Selasky #ifdef INET6 5199d40cf60SHans Petter Selasky struct sockaddr_in6 *sin6; 5209d40cf60SHans Petter Selasky #endif 5219d40cf60SHans Petter Selasky uint8_t *e_addr; 5229d40cf60SHans Petter Selasky 5239d40cf60SHans Petter Selasky switch (sa->sa_family) { 5249d40cf60SHans Petter Selasky case AF_LINK: 5259d40cf60SHans Petter Selasky /* 5269d40cf60SHans Petter Selasky * No mapping needed. Just check that it's a valid MC address. 5279d40cf60SHans Petter Selasky */ 5289d40cf60SHans Petter Selasky sdl = (struct sockaddr_dl *)sa; 5299d40cf60SHans Petter Selasky e_addr = LLADDR(sdl); 5309d40cf60SHans Petter Selasky if (!INFINIBAND_IS_MULTICAST(e_addr)) 5319d40cf60SHans Petter Selasky return (EADDRNOTAVAIL); 5329d40cf60SHans Petter Selasky *llsa = NULL; 5339d40cf60SHans Petter Selasky return 0; 5349d40cf60SHans Petter Selasky 5359d40cf60SHans Petter Selasky #ifdef INET 5369d40cf60SHans Petter Selasky case AF_INET: 5379d40cf60SHans Petter Selasky sin = (struct sockaddr_in *)sa; 5389d40cf60SHans Petter Selasky if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 5399d40cf60SHans Petter Selasky return (EADDRNOTAVAIL); 5409d40cf60SHans Petter Selasky sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 5419d40cf60SHans Petter Selasky sdl->sdl_alen = INFINIBAND_ADDR_LEN; 5429d40cf60SHans Petter Selasky e_addr = LLADDR(sdl); 54301630a49SHans Petter Selasky infiniband_ipv4_multicast_map( 54401630a49SHans Petter Selasky sin->sin_addr.s_addr, ifp->if_broadcastaddr, e_addr); 5459d40cf60SHans Petter Selasky *llsa = (struct sockaddr *)sdl; 5469d40cf60SHans Petter Selasky return (0); 5479d40cf60SHans Petter Selasky #endif 5489d40cf60SHans Petter Selasky #ifdef INET6 5499d40cf60SHans Petter Selasky case AF_INET6: 5509d40cf60SHans Petter Selasky sin6 = (struct sockaddr_in6 *)sa; 5519d40cf60SHans Petter Selasky /* 5529d40cf60SHans Petter Selasky * An IP6 address of 0 means listen to all of the 5539d40cf60SHans Petter Selasky * multicast address used for IP6. This has no meaning 5549d40cf60SHans Petter Selasky * in infiniband. 5559d40cf60SHans Petter Selasky */ 5569d40cf60SHans Petter Selasky if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) 5579d40cf60SHans Petter Selasky return (EADDRNOTAVAIL); 5589d40cf60SHans Petter Selasky if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 5599d40cf60SHans Petter Selasky return (EADDRNOTAVAIL); 5609d40cf60SHans Petter Selasky sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND); 5619d40cf60SHans Petter Selasky sdl->sdl_alen = INFINIBAND_ADDR_LEN; 5629d40cf60SHans Petter Selasky e_addr = LLADDR(sdl); 56301630a49SHans Petter Selasky infiniband_ipv6_multicast_map( 56401630a49SHans Petter Selasky &sin6->sin6_addr, ifp->if_broadcastaddr, e_addr); 5659d40cf60SHans Petter Selasky *llsa = (struct sockaddr *)sdl; 5669d40cf60SHans Petter Selasky return (0); 5679d40cf60SHans Petter Selasky #endif 5689d40cf60SHans Petter Selasky default: 5699d40cf60SHans Petter Selasky return (EAFNOSUPPORT); 5709d40cf60SHans Petter Selasky } 5719d40cf60SHans Petter Selasky } 5729d40cf60SHans Petter Selasky 5739d40cf60SHans Petter Selasky void 5749d40cf60SHans Petter Selasky infiniband_ifattach(struct ifnet *ifp, const uint8_t *lla, const uint8_t *llb) 5759d40cf60SHans Petter Selasky { 5769d40cf60SHans Petter Selasky struct sockaddr_dl *sdl; 5779d40cf60SHans Petter Selasky struct ifaddr *ifa; 5789d40cf60SHans Petter Selasky int i; 5799d40cf60SHans Petter Selasky 5809d40cf60SHans Petter Selasky ifp->if_addrlen = INFINIBAND_ADDR_LEN; 5819d40cf60SHans Petter Selasky ifp->if_hdrlen = INFINIBAND_HDR_LEN; 5829d40cf60SHans Petter Selasky ifp->if_mtu = INFINIBAND_MTU; 5839d40cf60SHans Petter Selasky if_attach(ifp); 5849d40cf60SHans Petter Selasky ifp->if_output = infiniband_output; 5859d40cf60SHans Petter Selasky ifp->if_input = infiniband_input; 5869d40cf60SHans Petter Selasky ifp->if_resolvemulti = infiniband_resolvemulti; 587ec52ff6dSHans Petter Selasky ifp->if_requestencap = infiniband_requestencap; 5889d40cf60SHans Petter Selasky 5899d40cf60SHans Petter Selasky if (ifp->if_baudrate == 0) 5909d40cf60SHans Petter Selasky ifp->if_baudrate = IF_Gbps(10); /* default value */ 5919d40cf60SHans Petter Selasky if (llb != NULL) 5929d40cf60SHans Petter Selasky ifp->if_broadcastaddr = llb; 5939d40cf60SHans Petter Selasky 5949d40cf60SHans Petter Selasky ifa = ifp->if_addr; 5959d40cf60SHans Petter Selasky KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 5969d40cf60SHans Petter Selasky sdl = (struct sockaddr_dl *)ifa->ifa_addr; 5979d40cf60SHans Petter Selasky sdl->sdl_type = IFT_INFINIBAND; 5989d40cf60SHans Petter Selasky sdl->sdl_alen = ifp->if_addrlen; 5999d40cf60SHans Petter Selasky 6009d40cf60SHans Petter Selasky if (lla != NULL) { 6019d40cf60SHans Petter Selasky memcpy(LLADDR(sdl), lla, ifp->if_addrlen); 6029d40cf60SHans Petter Selasky 6039d40cf60SHans Petter Selasky if (ifp->if_hw_addr != NULL) 6049d40cf60SHans Petter Selasky memcpy(ifp->if_hw_addr, lla, ifp->if_addrlen); 6059d40cf60SHans Petter Selasky } else { 6069d40cf60SHans Petter Selasky lla = LLADDR(sdl); 6079d40cf60SHans Petter Selasky } 6089d40cf60SHans Petter Selasky 6099d40cf60SHans Petter Selasky /* Attach ethernet compatible network device */ 6109d40cf60SHans Petter Selasky bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN); 6119d40cf60SHans Petter Selasky 6129d40cf60SHans Petter Selasky /* Announce Infiniband MAC address if non-zero. */ 6139d40cf60SHans Petter Selasky for (i = 0; i < ifp->if_addrlen; i++) 6149d40cf60SHans Petter Selasky if (lla[i] != 0) 6159d40cf60SHans Petter Selasky break; 6169d40cf60SHans Petter Selasky if (i != ifp->if_addrlen) 6179d40cf60SHans Petter Selasky if_printf(ifp, "Infiniband address: %20D\n", lla, ":"); 6189d40cf60SHans Petter Selasky 6199d40cf60SHans Petter Selasky /* Add necessary bits are setup; announce it now. */ 6209d40cf60SHans Petter Selasky EVENTHANDLER_INVOKE(infiniband_ifattach_event, ifp); 6219d40cf60SHans Petter Selasky 6229d40cf60SHans Petter Selasky if (IS_DEFAULT_VNET(curvnet)) 6239d40cf60SHans Petter Selasky devctl_notify("INFINIBAND", ifp->if_xname, "IFATTACH", NULL); 6249d40cf60SHans Petter Selasky } 6259d40cf60SHans Petter Selasky 6269d40cf60SHans Petter Selasky /* 6279d40cf60SHans Petter Selasky * Perform common duties while detaching an Infiniband interface 6289d40cf60SHans Petter Selasky */ 6299d40cf60SHans Petter Selasky void 6309d40cf60SHans Petter Selasky infiniband_ifdetach(struct ifnet *ifp) 6319d40cf60SHans Petter Selasky { 6329d40cf60SHans Petter Selasky bpfdetach(ifp); 6339d40cf60SHans Petter Selasky if_detach(ifp); 6349d40cf60SHans Petter Selasky } 6359d40cf60SHans Petter Selasky 6369d40cf60SHans Petter Selasky static int 6379d40cf60SHans Petter Selasky infiniband_modevent(module_t mod, int type, void *data) 6389d40cf60SHans Petter Selasky { 6399d40cf60SHans Petter Selasky switch (type) { 6409d40cf60SHans Petter Selasky case MOD_LOAD: 6419d40cf60SHans Petter Selasky case MOD_UNLOAD: 6429d40cf60SHans Petter Selasky return (0); 6439d40cf60SHans Petter Selasky default: 6449d40cf60SHans Petter Selasky return (EOPNOTSUPP); 6459d40cf60SHans Petter Selasky } 6469d40cf60SHans Petter Selasky } 6479d40cf60SHans Petter Selasky 6489d40cf60SHans Petter Selasky static moduledata_t infiniband_mod = { 6499d40cf60SHans Petter Selasky .name = "if_infiniband", 6509d40cf60SHans Petter Selasky .evhand = &infiniband_modevent, 6519d40cf60SHans Petter Selasky }; 6529d40cf60SHans Petter Selasky 6539d40cf60SHans Petter Selasky DECLARE_MODULE(if_infiniband, infiniband_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 6549d40cf60SHans Petter Selasky MODULE_VERSION(if_infiniband, 1); 655