135379Skfall /* 239187Ssklower * Copyright (c) 1982, 1989 Regents of the University of California. 335379Skfall * All rights reserved. 435379Skfall * 544464Sbostic * %sccs.include.redist.c% 635379Skfall * 7*60346Storek * @(#)if_ethersubr.c 7.25 (Berkeley) 05/24/93 835379Skfall */ 935379Skfall 1056529Sbostic #include <sys/param.h> 1156529Sbostic #include <sys/systm.h> 1256529Sbostic #include <sys/kernel.h> 1356529Sbostic #include <sys/malloc.h> 1456529Sbostic #include <sys/mbuf.h> 1556529Sbostic #include <sys/protosw.h> 1656529Sbostic #include <sys/socket.h> 1756529Sbostic #include <sys/ioctl.h> 1856529Sbostic #include <sys/errno.h> 1956529Sbostic #include <sys/syslog.h> 2035379Skfall 2156529Sbostic #include <machine/cpu.h> 2235379Skfall 2356529Sbostic #include <net/if.h> 2456529Sbostic #include <net/netisr.h> 2556529Sbostic #include <net/route.h> 2656529Sbostic #include <net/if_llc.h> 2756529Sbostic #include <net/if_dl.h> 2856529Sbostic #include <net/if_types.h> 2956529Sbostic 3035379Skfall #ifdef INET 3156529Sbostic #include <netinet/in.h> 3256529Sbostic #include <netinet/in_var.h> 3345930Sbostic #endif 3456529Sbostic #include <netinet/if_ether.h> 3535379Skfall 3635379Skfall #ifdef NS 3756529Sbostic #include <netns/ns.h> 3856529Sbostic #include <netns/ns_if.h> 3935379Skfall #endif 4035379Skfall 4137472Ssklower #ifdef ISO 4256529Sbostic #include <netiso/argo_debug.h> 4356529Sbostic #include <netiso/iso.h> 4456529Sbostic #include <netiso/iso_var.h> 4556529Sbostic #include <netiso/iso_snpac.h> 4637472Ssklower #endif 4737472Ssklower 4858231Ssklower #ifdef LLC 4958231Ssklower #include <netccitt/dll.h> 5058231Ssklower #include <netccitt/llc_var.h> 5158231Ssklower #endif 5258231Ssklower 5358231Ssklower #if defined(LLC) && defined(CCITT) 5458231Ssklower extern struct ifqueue pkintrq; 5558231Ssklower #endif 5658231Ssklower 5735379Skfall u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 5835795Skarels extern struct ifnet loif; 5950133Ssklower #define senderr(e) { error = (e); goto bad;} 6035379Skfall 6135379Skfall /* 6235379Skfall * Ethernet output routine. 6335379Skfall * Encapsulate a packet of type family for the local net. 6435379Skfall * Use trailer local net encapsulation if enough data in first 6535379Skfall * packet leaves a multiple of 512 bytes of data in remainder. 6635379Skfall * Assumes that ifp is actually pointer to arpcom structure. 6735379Skfall */ 6850133Ssklower ether_output(ifp, m0, dst, rt0) 6935379Skfall register struct ifnet *ifp; 7035379Skfall struct mbuf *m0; 7135379Skfall struct sockaddr *dst; 7250133Ssklower struct rtentry *rt0; 7335379Skfall { 7435795Skarels short type; 7535795Skarels int s, error = 0; 7635379Skfall u_char edst[6]; 7735379Skfall register struct mbuf *m = m0; 7850133Ssklower register struct rtentry *rt; 7935795Skarels struct mbuf *mcopy = (struct mbuf *)0; 8035379Skfall register struct ether_header *eh; 8154718Ssklower int off, len = m->m_pkthdr.len; 8254718Ssklower struct arpcom *ac = (struct arpcom *)ifp; 8335379Skfall 8450133Ssklower if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 8550133Ssklower senderr(ENETDOWN); 8650133Ssklower ifp->if_lastchange = time; 8750133Ssklower if (rt = rt0) { 8850133Ssklower if ((rt->rt_flags & RTF_UP) == 0) { 8950133Ssklower if (rt0 = rt = rtalloc1(dst, 1)) 9050133Ssklower rt->rt_refcnt--; 9150133Ssklower else 9253060Ssklower senderr(EHOSTUNREACH); 9350133Ssklower } 9450133Ssklower if (rt->rt_flags & RTF_GATEWAY) { 9550133Ssklower if (rt->rt_gwroute == 0) 9650133Ssklower goto lookup; 9750133Ssklower if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 9850133Ssklower rtfree(rt); rt = rt0; 9950133Ssklower lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 10050133Ssklower if ((rt = rt->rt_gwroute) == 0) 10153060Ssklower senderr(EHOSTUNREACH); 10250133Ssklower } 10350133Ssklower } 10450133Ssklower if (rt->rt_flags & RTF_REJECT) 10550133Ssklower if (rt->rt_rmx.rmx_expire == 0 || 10650133Ssklower time.tv_sec < rt->rt_rmx.rmx_expire) 10753060Ssklower senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 10835379Skfall } 10935379Skfall switch (dst->sa_family) { 11035379Skfall 11135379Skfall #ifdef INET 11235379Skfall case AF_INET: 11350133Ssklower if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst, 11454718Ssklower edst)) 11535379Skfall return (0); /* if not yet resolved */ 11654718Ssklower /* If broadcasting on a simplex interface, loopback a copy */ 11754718Ssklower if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 11841546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 11935379Skfall off = m->m_pkthdr.len - m->m_len; 12035379Skfall type = ETHERTYPE_IP; 12154718Ssklower break; 12235379Skfall #endif 12335379Skfall #ifdef NS 12435379Skfall case AF_NS: 12535379Skfall type = ETHERTYPE_NS; 12639187Ssklower bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 12739187Ssklower (caddr_t)edst, sizeof (edst)); 12835795Skarels if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 12945632Ssklower return (looutput(ifp, m, dst, rt)); 13054718Ssklower /* If broadcasting on a simplex interface, loopback a copy */ 13154718Ssklower if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX)) 13241546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 13354718Ssklower break; 13435379Skfall #endif 13535795Skarels #ifdef ISO 13635795Skarels case AF_ISO: { 13743073Ssklower int snpalen; 13837472Ssklower struct llc *l; 13950133Ssklower register struct sockaddr_dl *sdl; 14037472Ssklower 14150133Ssklower if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 14250133Ssklower sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 14350133Ssklower bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 14450133Ssklower } else if (error = 14550133Ssklower iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 14650133Ssklower (char *)edst, &snpalen)) 14743073Ssklower goto bad; /* Not Resolved */ 14854718Ssklower /* If broadcasting on a simplex interface, loopback a copy */ 14954718Ssklower if (*edst & 1) 15054718Ssklower m->m_flags |= (M_BCAST|M_MCAST); 15154718Ssklower if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX) && 15245632Ssklower (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 15345632Ssklower M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 15445632Ssklower if (mcopy) { 15545632Ssklower eh = mtod(mcopy, struct ether_header *); 15645632Ssklower bcopy((caddr_t)edst, 15745632Ssklower (caddr_t)eh->ether_dhost, sizeof (edst)); 15845632Ssklower bcopy((caddr_t)ac->ac_enaddr, 15945632Ssklower (caddr_t)eh->ether_shost, sizeof (edst)); 16045632Ssklower } 16145632Ssklower } 16235795Skarels M_PREPEND(m, 3, M_DONTWAIT); 16337472Ssklower if (m == NULL) 16443073Ssklower return (0); 16535795Skarels type = m->m_pkthdr.len; 16635795Skarels l = mtod(m, struct llc *); 16735795Skarels l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 16835795Skarels l->llc_control = LLC_UI; 16939187Ssklower len += 3; 17035795Skarels IFDEBUG(D_ETHER) 17135795Skarels int i; 17235795Skarels printf("unoutput: sending pkt to: "); 17335795Skarels for (i=0; i<6; i++) 17435795Skarels printf("%x ", edst[i] & 0xff); 17535795Skarels printf("\n"); 17635795Skarels ENDDEBUG 17754718Ssklower } break; 178*60346Storek #endif /* ISO */ 17958231Ssklower #ifdef LLC 18058231Ssklower /* case AF_NSAP: */ 18158231Ssklower case AF_CCITT: { 18258231Ssklower register struct sockaddr_dl *sdl = 18358231Ssklower (struct sockaddr_dl *) rt -> rt_gateway; 18441546Smckusick 18558231Ssklower if (sdl && sdl->sdl_family == AF_LINK 18658231Ssklower && sdl->sdl_alen > 0) { 18758231Ssklower bcopy(LLADDR(sdl), (char *)edst, 18858231Ssklower sizeof(edst)); 18958231Ssklower } else goto bad; /* Not a link interface ? Funny ... */ 19058231Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 19158231Ssklower (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 19258231Ssklower M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 19358231Ssklower if (mcopy) { 19458231Ssklower eh = mtod(mcopy, struct ether_header *); 19558231Ssklower bcopy((caddr_t)edst, 19658231Ssklower (caddr_t)eh->ether_dhost, sizeof (edst)); 19758231Ssklower bcopy((caddr_t)ac->ac_enaddr, 19858231Ssklower (caddr_t)eh->ether_shost, sizeof (edst)); 19958231Ssklower } 20058231Ssklower } 20158231Ssklower type = m->m_pkthdr.len; 20258231Ssklower #ifdef LLC_DEBUG 20358231Ssklower { 20458231Ssklower int i; 20558231Ssklower register struct llc *l = mtod(m, struct llc *); 20658231Ssklower 20758231Ssklower printf("ether_output: sending LLC2 pkt to: "); 20858231Ssklower for (i=0; i<6; i++) 20958231Ssklower printf("%x ", edst[i] & 0xff); 21058231Ssklower printf(" len 0x%x dsap 0x%x ssap 0x%x control 0x%x\n", 21158231Ssklower type & 0xff, l->llc_dsap & 0xff, l->llc_ssap &0xff, 21258231Ssklower l->llc_control & 0xff); 21358231Ssklower 21458231Ssklower } 215*60346Storek #endif /* LLC_DEBUG */ 21658231Ssklower } break; 217*60346Storek #endif /* LLC */ 21858231Ssklower 21935379Skfall case AF_UNSPEC: 22035379Skfall eh = (struct ether_header *)dst->sa_data; 22135379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 22235379Skfall type = eh->ether_type; 22354718Ssklower break; 22435379Skfall 22535379Skfall default: 22635379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 22735379Skfall dst->sa_family); 22850133Ssklower senderr(EAFNOSUPPORT); 22935379Skfall } 23035379Skfall 23135379Skfall 23245632Ssklower if (mcopy) 23345632Ssklower (void) looutput(ifp, mcopy, dst, rt); 23435379Skfall /* 23535379Skfall * Add local net header. If no space in first mbuf, 23635379Skfall * allocate another. 23735379Skfall */ 23835379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 23950133Ssklower if (m == 0) 24050133Ssklower senderr(ENOBUFS); 24135379Skfall eh = mtod(m, struct ether_header *); 24235795Skarels type = htons((u_short)type); 24335795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 24435795Skarels sizeof(eh->ether_type)); 24535379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 24635379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 24735379Skfall sizeof(eh->ether_shost)); 24845632Ssklower s = splimp(); 24935379Skfall /* 25035379Skfall * Queue message on interface, and start output if interface 25135379Skfall * not yet active. 25235379Skfall */ 25335379Skfall if (IF_QFULL(&ifp->if_snd)) { 25435379Skfall IF_DROP(&ifp->if_snd); 25535379Skfall splx(s); 25650133Ssklower senderr(ENOBUFS); 25735379Skfall } 25835379Skfall IF_ENQUEUE(&ifp->if_snd, m); 25935379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 26038845Sroot (*ifp->if_start)(ifp); 26135379Skfall splx(s); 26239187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 26354718Ssklower if (m->m_flags & M_MCAST) 26439187Ssklower ifp->if_omcasts++; 26535795Skarels return (error); 26635379Skfall 26735379Skfall bad: 26835795Skarels if (m) 26935795Skarels m_freem(m); 27035379Skfall return (error); 27135379Skfall } 27235379Skfall 27335379Skfall /* 27438845Sroot * Process a received Ethernet packet; 27538845Sroot * the packet is in the mbuf chain m without 27638845Sroot * the ether header, which is provided separately. 27735379Skfall */ 27837472Ssklower ether_input(ifp, eh, m) 27935379Skfall struct ifnet *ifp; 28035379Skfall register struct ether_header *eh; 28135379Skfall struct mbuf *m; 28235379Skfall { 28335379Skfall register struct ifqueue *inq; 28435795Skarels register struct llc *l; 28558231Ssklower struct arpcom *ac = (struct arpcom *)ifp; 28635379Skfall int s; 28735379Skfall 28853696Ssklower if ((ifp->if_flags & IFF_UP) == 0) { 28953696Ssklower m_freem(m); 29053696Ssklower return; 29153696Ssklower } 29239187Ssklower ifp->if_lastchange = time; 29339187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 29435795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 29535379Skfall sizeof(etherbroadcastaddr)) == 0) 29635379Skfall m->m_flags |= M_BCAST; 29735795Skarels else if (eh->ether_dhost[0] & 1) 29835795Skarels m->m_flags |= M_MCAST; 29939187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 30039187Ssklower ifp->if_imcasts++; 30135379Skfall 30235379Skfall switch (eh->ether_type) { 30335379Skfall #ifdef INET 30435379Skfall case ETHERTYPE_IP: 30535379Skfall schednetisr(NETISR_IP); 30635379Skfall inq = &ipintrq; 30735379Skfall break; 30835379Skfall 30935379Skfall case ETHERTYPE_ARP: 31050133Ssklower schednetisr(NETISR_ARP); 31150133Ssklower inq = &arpintrq; 31250133Ssklower break; 31335379Skfall #endif 31435379Skfall #ifdef NS 31535379Skfall case ETHERTYPE_NS: 31635379Skfall schednetisr(NETISR_NS); 31735379Skfall inq = &nsintrq; 31835379Skfall break; 31935379Skfall 32035379Skfall #endif 32135379Skfall default: 32258231Ssklower #if defined (ISO) || defined (LLC) 32337472Ssklower if (eh->ether_type > ETHERMTU) 32435795Skarels goto dropanyway; 32535795Skarels l = mtod(m, struct llc *); 32658231Ssklower switch (l->llc_dsap) { 32758231Ssklower #ifdef ISO 32858231Ssklower case LLC_ISO_LSAP: 32958231Ssklower switch (l->llc_control) { 33058231Ssklower case LLC_UI: 33158231Ssklower /* LLC_UI_P forbidden in class 1 service */ 33258231Ssklower if ((l->llc_dsap == LLC_ISO_LSAP) && 33358231Ssklower (l->llc_ssap == LLC_ISO_LSAP)) { 33458231Ssklower /* LSAP for ISO */ 33558231Ssklower if (m->m_pkthdr.len > eh->ether_type) 33658231Ssklower m_adj(m, eh->ether_type - m->m_pkthdr.len); 33758231Ssklower m->m_data += 3; /* XXX */ 33858231Ssklower m->m_len -= 3; /* XXX */ 33958231Ssklower m->m_pkthdr.len -= 3; /* XXX */ 34058231Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 34158231Ssklower if (m == 0) 34258231Ssklower return; 34358231Ssklower *mtod(m, struct ether_header *) = *eh; 34458231Ssklower IFDEBUG(D_ETHER) 34558231Ssklower printf("clnp packet"); 34658231Ssklower ENDDEBUG 34758231Ssklower schednetisr(NETISR_ISO); 34858231Ssklower inq = &clnlintrq; 34958231Ssklower break; 35058231Ssklower } 35158231Ssklower goto dropanyway; 35258231Ssklower 35358231Ssklower case LLC_XID: 35458231Ssklower case LLC_XID_P: 35558231Ssklower if(m->m_len < 6) 35658231Ssklower goto dropanyway; 35758231Ssklower l->llc_window = 0; 35858231Ssklower l->llc_fid = 9; 35958231Ssklower l->llc_class = 1; 36058231Ssklower l->llc_dsap = l->llc_ssap = 0; 36158231Ssklower /* Fall through to */ 36258231Ssklower case LLC_TEST: 36358231Ssklower case LLC_TEST_P: 36458231Ssklower { 36558231Ssklower struct sockaddr sa; 36658231Ssklower register struct ether_header *eh2; 36758231Ssklower int i; 36858231Ssklower u_char c = l->llc_dsap; 36958231Ssklower 37058231Ssklower l->llc_dsap = l->llc_ssap; 37158231Ssklower l->llc_ssap = c; 37258231Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 37358231Ssklower bcopy((caddr_t)ac->ac_enaddr, 37458231Ssklower (caddr_t)eh->ether_dhost, 6); 37558231Ssklower sa.sa_family = AF_UNSPEC; 37658231Ssklower sa.sa_len = sizeof(sa); 37758231Ssklower eh2 = (struct ether_header *)sa.sa_data; 37858231Ssklower for (i = 0; i < 6; i++) { 37958231Ssklower eh2->ether_shost[i] = c = eh->ether_dhost[i]; 38058231Ssklower eh2->ether_dhost[i] = 38158231Ssklower eh->ether_dhost[i] = eh->ether_shost[i]; 38258231Ssklower eh->ether_shost[i] = c; 38358231Ssklower } 38458231Ssklower ifp->if_output(ifp, m, &sa, NULL); 38558231Ssklower return; 38658231Ssklower } 38758231Ssklower default: 38858231Ssklower m_freem(m); 38958231Ssklower return; 39058231Ssklower } 39158231Ssklower break; 39258231Ssklower #endif /* ISO */ 39358231Ssklower #ifdef LLC 39458231Ssklower case LLC_X25_LSAP: 39558231Ssklower { 39645632Ssklower if (m->m_pkthdr.len > eh->ether_type) 39745632Ssklower m_adj(m, eh->ether_type - m->m_pkthdr.len); 39858231Ssklower M_PREPEND(m, sizeof(struct sdl_hdr) , M_DONTWAIT); 39937472Ssklower if (m == 0) 40037472Ssklower return; 40158231Ssklower if ( !sdl_sethdrif(ifp, eh->ether_shost, LLC_X25_LSAP, 40258231Ssklower eh->ether_dhost, LLC_X25_LSAP, 6, 40358231Ssklower mtod(m, struct sdl_hdr *))) 40458231Ssklower panic("ETHER cons addr failure"); 40558231Ssklower mtod(m, struct sdl_hdr *)->sdlhdr_len = eh->ether_type; 40658231Ssklower #ifdef LLC_DEBUG 40758231Ssklower printf("llc packet\n"); 408*60346Storek #endif /* LLC_DEBUG */ 40958231Ssklower schednetisr(NETISR_CCITT); 41058231Ssklower inq = &llcintrq; 41138845Sroot break; 41235795Skarels } 41358231Ssklower #endif /* LLC */ 41435795Skarels dropanyway: 41535795Skarels default: 41658231Ssklower m_freem(m); 41758231Ssklower return; 41858231Ssklower } 41958231Ssklower #else /* ISO || LLC */ 42038845Sroot m_freem(m); 42138845Sroot return; 42258231Ssklower #endif /* ISO || LLC */ 42335379Skfall } 42435379Skfall 42535379Skfall s = splimp(); 42635379Skfall if (IF_QFULL(inq)) { 42735379Skfall IF_DROP(inq); 42835379Skfall m_freem(m); 42935379Skfall } else 43035379Skfall IF_ENQUEUE(inq, m); 43135379Skfall splx(s); 43235379Skfall } 43335379Skfall 43435379Skfall /* 43535379Skfall * Convert Ethernet address to printable (loggable) representation. 43635379Skfall */ 43737472Ssklower static char digits[] = "0123456789abcdef"; 43835379Skfall char * 43935379Skfall ether_sprintf(ap) 44035379Skfall register u_char *ap; 44135379Skfall { 44235379Skfall register i; 44335379Skfall static char etherbuf[18]; 44435379Skfall register char *cp = etherbuf; 44535379Skfall 44635379Skfall for (i = 0; i < 6; i++) { 44735379Skfall *cp++ = digits[*ap >> 4]; 44835379Skfall *cp++ = digits[*ap++ & 0xf]; 44935379Skfall *cp++ = ':'; 45035379Skfall } 45135379Skfall *--cp = 0; 45235379Skfall return (etherbuf); 45335379Skfall } 45452567Ssklower 45552567Ssklower /* 45652567Ssklower * Perform common duties while attaching to interface list 45752567Ssklower */ 45852567Ssklower ether_ifattach(ifp) 45952567Ssklower register struct ifnet *ifp; 46052567Ssklower { 46152567Ssklower register struct ifaddr *ifa; 46252567Ssklower register struct sockaddr_dl *sdl; 46352567Ssklower 46452567Ssklower ifp->if_type = IFT_ETHER; 46552567Ssklower ifp->if_addrlen = 6; 46652567Ssklower ifp->if_hdrlen = 14; 46752567Ssklower ifp->if_mtu = ETHERMTU; 46852567Ssklower for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 46952567Ssklower if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 47052567Ssklower sdl->sdl_family == AF_LINK) { 47152567Ssklower sdl->sdl_type = IFT_ETHER; 47252567Ssklower sdl->sdl_alen = ifp->if_addrlen; 47352567Ssklower bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 47452567Ssklower LLADDR(sdl), ifp->if_addrlen); 47552567Ssklower break; 47652567Ssklower } 47752567Ssklower } 47854718Ssklower 47954718Ssklower u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 48054718Ssklower u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff }; 48154718Ssklower /* 48254718Ssklower * Add an Ethernet multicast address or range of addresses to the list for a 48354718Ssklower * given interface. 48454718Ssklower */ 48554718Ssklower int 48654718Ssklower ether_addmulti(ifr, ac) 48754718Ssklower struct ifreq *ifr; 48854718Ssklower register struct arpcom *ac; 48954718Ssklower { 49054718Ssklower register struct ether_multi *enm; 49154718Ssklower struct sockaddr_in *sin; 49254718Ssklower u_char addrlo[6]; 49354718Ssklower u_char addrhi[6]; 49454718Ssklower int s = splimp(); 49554718Ssklower 49654718Ssklower switch (ifr->ifr_addr.sa_family) { 49754718Ssklower 49854718Ssklower case AF_UNSPEC: 49954718Ssklower bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 50054718Ssklower bcopy(addrlo, addrhi, 6); 50154718Ssklower break; 50254718Ssklower 50354718Ssklower #ifdef INET 50454718Ssklower case AF_INET: 50554718Ssklower sin = (struct sockaddr_in *)&(ifr->ifr_addr); 50654718Ssklower if (sin->sin_addr.s_addr == INADDR_ANY) { 50754718Ssklower /* 50854718Ssklower * An IP address of INADDR_ANY means listen to all 50954718Ssklower * of the Ethernet multicast addresses used for IP. 51054718Ssklower * (This is for the sake of IP multicast routers.) 51154718Ssklower */ 51254718Ssklower bcopy(ether_ipmulticast_min, addrlo, 6); 51354718Ssklower bcopy(ether_ipmulticast_max, addrhi, 6); 51454718Ssklower } 51554718Ssklower else { 51654718Ssklower ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 51754718Ssklower bcopy(addrlo, addrhi, 6); 51854718Ssklower } 51954718Ssklower break; 52054718Ssklower #endif 52154718Ssklower 52254718Ssklower default: 52354718Ssklower splx(s); 52454718Ssklower return (EAFNOSUPPORT); 52554718Ssklower } 52654718Ssklower 52754718Ssklower /* 52854718Ssklower * Verify that we have valid Ethernet multicast addresses. 52954718Ssklower */ 53054718Ssklower if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) { 53154718Ssklower splx(s); 53254718Ssklower return (EINVAL); 53354718Ssklower } 53454718Ssklower /* 53554718Ssklower * See if the address range is already in the list. 53654718Ssklower */ 53754718Ssklower ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 53854718Ssklower if (enm != NULL) { 53954718Ssklower /* 54054718Ssklower * Found it; just increment the reference count. 54154718Ssklower */ 54254718Ssklower ++enm->enm_refcount; 54354718Ssklower splx(s); 54454718Ssklower return (0); 54554718Ssklower } 54654718Ssklower /* 54754718Ssklower * New address or range; malloc a new multicast record 54854718Ssklower * and link it into the interface's multicast list. 54954718Ssklower */ 55054718Ssklower enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT); 55154718Ssklower if (enm == NULL) { 55254718Ssklower splx(s); 55354718Ssklower return (ENOBUFS); 55454718Ssklower } 55554718Ssklower bcopy(addrlo, enm->enm_addrlo, 6); 55654718Ssklower bcopy(addrhi, enm->enm_addrhi, 6); 55754718Ssklower enm->enm_ac = ac; 55854718Ssklower enm->enm_refcount = 1; 55954718Ssklower enm->enm_next = ac->ac_multiaddrs; 56054718Ssklower ac->ac_multiaddrs = enm; 56154718Ssklower ac->ac_multicnt++; 56254718Ssklower splx(s); 56354718Ssklower /* 56454718Ssklower * Return ENETRESET to inform the driver that the list has changed 56554718Ssklower * and its reception filter should be adjusted accordingly. 56654718Ssklower */ 56754718Ssklower return (ENETRESET); 56854718Ssklower } 56954718Ssklower 57054718Ssklower /* 57154718Ssklower * Delete a multicast address record. 57254718Ssklower */ 57354718Ssklower int 57454718Ssklower ether_delmulti(ifr, ac) 57554718Ssklower struct ifreq *ifr; 57654718Ssklower register struct arpcom *ac; 57754718Ssklower { 57854718Ssklower register struct ether_multi *enm; 57954718Ssklower register struct ether_multi **p; 58054718Ssklower struct sockaddr_in *sin; 58154718Ssklower u_char addrlo[6]; 58254718Ssklower u_char addrhi[6]; 58354718Ssklower int s = splimp(); 58454718Ssklower 58554718Ssklower switch (ifr->ifr_addr.sa_family) { 58654718Ssklower 58754718Ssklower case AF_UNSPEC: 58854718Ssklower bcopy(ifr->ifr_addr.sa_data, addrlo, 6); 58954718Ssklower bcopy(addrlo, addrhi, 6); 59054718Ssklower break; 59154718Ssklower 59254718Ssklower #ifdef INET 59354718Ssklower case AF_INET: 59454718Ssklower sin = (struct sockaddr_in *)&(ifr->ifr_addr); 59554718Ssklower if (sin->sin_addr.s_addr == INADDR_ANY) { 59654718Ssklower /* 59754718Ssklower * An IP address of INADDR_ANY means stop listening 59854718Ssklower * to the range of Ethernet multicast addresses used 59954718Ssklower * for IP. 60054718Ssklower */ 60154718Ssklower bcopy(ether_ipmulticast_min, addrlo, 6); 60254718Ssklower bcopy(ether_ipmulticast_max, addrhi, 6); 60354718Ssklower } 60454718Ssklower else { 60554718Ssklower ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo); 60654718Ssklower bcopy(addrlo, addrhi, 6); 60754718Ssklower } 60854718Ssklower break; 60954718Ssklower #endif 61054718Ssklower 61154718Ssklower default: 61254718Ssklower splx(s); 61354718Ssklower return (EAFNOSUPPORT); 61454718Ssklower } 61554718Ssklower 61654718Ssklower /* 61754718Ssklower * Look up the address in our list. 61854718Ssklower */ 61954718Ssklower ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm); 62054718Ssklower if (enm == NULL) { 62154718Ssklower splx(s); 62254718Ssklower return (ENXIO); 62354718Ssklower } 62454718Ssklower if (--enm->enm_refcount != 0) { 62554718Ssklower /* 62654718Ssklower * Still some claims to this record. 62754718Ssklower */ 62854718Ssklower splx(s); 62954718Ssklower return (0); 63054718Ssklower } 63154718Ssklower /* 63254718Ssklower * No remaining claims to this record; unlink and free it. 63354718Ssklower */ 63454718Ssklower for (p = &enm->enm_ac->ac_multiaddrs; 63554718Ssklower *p != enm; 63654718Ssklower p = &(*p)->enm_next) 63754718Ssklower continue; 63854718Ssklower *p = (*p)->enm_next; 63954718Ssklower free(enm, M_IFMADDR); 64054718Ssklower ac->ac_multicnt--; 64154718Ssklower splx(s); 64254718Ssklower /* 64354718Ssklower * Return ENETRESET to inform the driver that the list has changed 64454718Ssklower * and its reception filter should be adjusted accordingly. 64554718Ssklower */ 64654718Ssklower return (ENETRESET); 64754718Ssklower } 648