135379Skfall /* 239187Ssklower * Copyright (c) 1982, 1989 Regents of the University of California. 335379Skfall * All rights reserved. 435379Skfall * 544464Sbostic * %sccs.include.redist.c% 635379Skfall * 7*53060Ssklower * @(#)if_ethersubr.c 7.18 (Berkeley) 03/26/92 835379Skfall */ 935379Skfall 1035379Skfall #include "param.h" 1135379Skfall #include "systm.h" 1248455Skarels #include "kernel.h" 1335379Skfall #include "malloc.h" 1435379Skfall #include "mbuf.h" 1535379Skfall #include "protosw.h" 1635379Skfall #include "socket.h" 1735379Skfall #include "ioctl.h" 1835379Skfall #include "errno.h" 1935379Skfall #include "syslog.h" 2052270Storek #include "machine/cpu.h" 2135379Skfall 2235379Skfall #include "if.h" 2335379Skfall #include "netisr.h" 2435379Skfall #include "route.h" 2535795Skarels #include "if_llc.h" 2640790Ssklower #include "if_dl.h" 2752567Ssklower #include "if_types.h" 2835379Skfall 2935379Skfall #ifdef INET 3035379Skfall #include "../netinet/in.h" 3135379Skfall #include "../netinet/in_var.h" 3245930Sbostic #endif 3335379Skfall #include "../netinet/if_ether.h" 3435379Skfall 3535379Skfall #ifdef NS 3635379Skfall #include "../netns/ns.h" 3735379Skfall #include "../netns/ns_if.h" 3835379Skfall #endif 3935379Skfall 4037472Ssklower #ifdef ISO 4137472Ssklower #include "../netiso/argo_debug.h" 4237472Ssklower #include "../netiso/iso.h" 4337472Ssklower #include "../netiso/iso_var.h" 4443073Ssklower #include "../netiso/iso_snpac.h" 4537472Ssklower #endif 4637472Ssklower 4735379Skfall u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 4835795Skarels extern struct ifnet loif; 4950133Ssklower #define senderr(e) { error = (e); goto bad;} 5035379Skfall 5135379Skfall /* 5235379Skfall * Ethernet output routine. 5335379Skfall * Encapsulate a packet of type family for the local net. 5435379Skfall * Use trailer local net encapsulation if enough data in first 5535379Skfall * packet leaves a multiple of 512 bytes of data in remainder. 5635379Skfall * Assumes that ifp is actually pointer to arpcom structure. 5735379Skfall */ 5850133Ssklower ether_output(ifp, m0, dst, rt0) 5935379Skfall register struct ifnet *ifp; 6035379Skfall struct mbuf *m0; 6135379Skfall struct sockaddr *dst; 6250133Ssklower struct rtentry *rt0; 6335379Skfall { 6435795Skarels short type; 6535795Skarels int s, error = 0; 6635379Skfall u_char edst[6]; 6735379Skfall register struct mbuf *m = m0; 6850133Ssklower register struct rtentry *rt; 6935795Skarels struct mbuf *mcopy = (struct mbuf *)0; 7035379Skfall register struct ether_header *eh; 7139187Ssklower int usetrailers, off, len = m->m_pkthdr.len; 7235379Skfall #define ac ((struct arpcom *)ifp) 7335379Skfall 7450133Ssklower if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 7550133Ssklower senderr(ENETDOWN); 7650133Ssklower ifp->if_lastchange = time; 7750133Ssklower if (rt = rt0) { 7850133Ssklower if ((rt->rt_flags & RTF_UP) == 0) { 7950133Ssklower if (rt0 = rt = rtalloc1(dst, 1)) 8050133Ssklower rt->rt_refcnt--; 8150133Ssklower else 82*53060Ssklower senderr(EHOSTUNREACH); 8350133Ssklower } 8450133Ssklower if (rt->rt_flags & RTF_GATEWAY) { 8550133Ssklower if (rt->rt_gwroute == 0) 8650133Ssklower goto lookup; 8750133Ssklower if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 8850133Ssklower rtfree(rt); rt = rt0; 8950133Ssklower lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 9050133Ssklower if ((rt = rt->rt_gwroute) == 0) 91*53060Ssklower senderr(EHOSTUNREACH); 9250133Ssklower } 9350133Ssklower } 9450133Ssklower if (rt->rt_flags & RTF_REJECT) 9550133Ssklower if (rt->rt_rmx.rmx_expire == 0 || 9650133Ssklower time.tv_sec < rt->rt_rmx.rmx_expire) 97*53060Ssklower senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); 9835379Skfall } 9935379Skfall switch (dst->sa_family) { 10035379Skfall 10135379Skfall #ifdef INET 10235379Skfall case AF_INET: 10350133Ssklower if (!arpresolve(ac, rt, m, (struct sockaddr_in *)dst, 10450133Ssklower edst, &usetrailers)) 10535379Skfall return (0); /* if not yet resolved */ 10637472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 10741546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 10835379Skfall off = m->m_pkthdr.len - m->m_len; 10935379Skfall if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 11035379Skfall (m->m_flags & M_EXT) == 0 && 11135379Skfall m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 11235379Skfall type = ETHERTYPE_TRAIL + (off>>9); 11335379Skfall m->m_data -= 2 * sizeof (u_short); 11435379Skfall m->m_len += 2 * sizeof (u_short); 11539187Ssklower len += 2 * sizeof (u_short); 11635379Skfall *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 11735379Skfall *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 11835379Skfall goto gottrailertype; 11935379Skfall } 12035379Skfall type = ETHERTYPE_IP; 12135379Skfall goto gottype; 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)); 13037472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 13141546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 13235379Skfall goto gottype; 13335379Skfall #endif 13435795Skarels #ifdef ISO 13535795Skarels case AF_ISO: { 13643073Ssklower int snpalen; 13737472Ssklower struct llc *l; 13850133Ssklower register struct sockaddr_dl *sdl; 13937472Ssklower 14050133Ssklower if (rt && (sdl = (struct sockaddr_dl *)rt->rt_gateway) && 14150133Ssklower sdl->sdl_family == AF_LINK && sdl->sdl_alen > 0) { 14250133Ssklower bcopy(LLADDR(sdl), (caddr_t)edst, sizeof(edst)); 14350133Ssklower } else if (error = 14450133Ssklower iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 14550133Ssklower (char *)edst, &snpalen)) 14643073Ssklower goto bad; /* Not Resolved */ 14745632Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) && 14845632Ssklower (mcopy = m_copy(m, 0, (int)M_COPYALL))) { 14945632Ssklower M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT); 15045632Ssklower if (mcopy) { 15145632Ssklower eh = mtod(mcopy, struct ether_header *); 15245632Ssklower bcopy((caddr_t)edst, 15345632Ssklower (caddr_t)eh->ether_dhost, sizeof (edst)); 15445632Ssklower bcopy((caddr_t)ac->ac_enaddr, 15545632Ssklower (caddr_t)eh->ether_shost, sizeof (edst)); 15645632Ssklower } 15745632Ssklower } 15835795Skarels M_PREPEND(m, 3, M_DONTWAIT); 15937472Ssklower if (m == NULL) 16043073Ssklower return (0); 16135795Skarels type = m->m_pkthdr.len; 16235795Skarels l = mtod(m, struct llc *); 16335795Skarels l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 16435795Skarels l->llc_control = LLC_UI; 16539187Ssklower len += 3; 16635795Skarels IFDEBUG(D_ETHER) 16735795Skarels int i; 16835795Skarels printf("unoutput: sending pkt to: "); 16935795Skarels for (i=0; i<6; i++) 17035795Skarels printf("%x ", edst[i] & 0xff); 17135795Skarels printf("\n"); 17235795Skarels ENDDEBUG 17335795Skarels } goto gottype; 17435795Skarels #endif ISO 17541546Smckusick #ifdef RMP 17641546Smckusick case AF_RMP: 17741546Smckusick /* 17841546Smckusick * This is IEEE 802.3 -- the Ethernet `type' field is 17941546Smckusick * really a `length' field. 18041546Smckusick */ 18141546Smckusick type = m->m_len; 18241546Smckusick bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 18341546Smckusick break; 18441546Smckusick #endif 18541546Smckusick 18635379Skfall case AF_UNSPEC: 18735379Skfall eh = (struct ether_header *)dst->sa_data; 18835379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 18935379Skfall type = eh->ether_type; 19035379Skfall goto gottype; 19135379Skfall 19235379Skfall default: 19335379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 19435379Skfall dst->sa_family); 19550133Ssklower senderr(EAFNOSUPPORT); 19635379Skfall } 19735379Skfall 19835379Skfall gottrailertype: 19935379Skfall /* 20035379Skfall * Packet to be sent as trailer: move first packet 20135379Skfall * (control information) to end of chain. 20235379Skfall */ 20335379Skfall while (m->m_next) 20435379Skfall m = m->m_next; 20535379Skfall m->m_next = m0; 20635379Skfall m = m0->m_next; 20735379Skfall m0->m_next = 0; 20835379Skfall 20935379Skfall gottype: 21045632Ssklower if (mcopy) 21145632Ssklower (void) looutput(ifp, mcopy, dst, rt); 21235379Skfall /* 21335379Skfall * Add local net header. If no space in first mbuf, 21435379Skfall * allocate another. 21535379Skfall */ 21635379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 21750133Ssklower if (m == 0) 21850133Ssklower senderr(ENOBUFS); 21935379Skfall eh = mtod(m, struct ether_header *); 22035795Skarels type = htons((u_short)type); 22135795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 22235795Skarels sizeof(eh->ether_type)); 22335379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 22435379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 22535379Skfall sizeof(eh->ether_shost)); 22645632Ssklower s = splimp(); 22735379Skfall /* 22835379Skfall * Queue message on interface, and start output if interface 22935379Skfall * not yet active. 23035379Skfall */ 23135379Skfall if (IF_QFULL(&ifp->if_snd)) { 23235379Skfall IF_DROP(&ifp->if_snd); 23335379Skfall splx(s); 23450133Ssklower senderr(ENOBUFS); 23535379Skfall } 23635379Skfall IF_ENQUEUE(&ifp->if_snd, m); 23735379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 23838845Sroot (*ifp->if_start)(ifp); 23935379Skfall splx(s); 24039187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 24139187Ssklower if (edst[0] & 1) 24239187Ssklower ifp->if_omcasts++; 24335795Skarels return (error); 24435379Skfall 24535379Skfall bad: 24635795Skarels if (m) 24735795Skarels m_freem(m); 24835379Skfall return (error); 24935379Skfall } 25035379Skfall 25135379Skfall /* 25238845Sroot * Process a received Ethernet packet; 25338845Sroot * the packet is in the mbuf chain m without 25438845Sroot * the ether header, which is provided separately. 25535379Skfall */ 25637472Ssklower ether_input(ifp, eh, m) 25735379Skfall struct ifnet *ifp; 25835379Skfall register struct ether_header *eh; 25935379Skfall struct mbuf *m; 26035379Skfall { 26135379Skfall register struct ifqueue *inq; 26235795Skarels register struct llc *l; 26335379Skfall int s; 26435379Skfall 26539187Ssklower ifp->if_lastchange = time; 26639187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 26735795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 26835379Skfall sizeof(etherbroadcastaddr)) == 0) 26935379Skfall m->m_flags |= M_BCAST; 27035795Skarels else if (eh->ether_dhost[0] & 1) 27135795Skarels m->m_flags |= M_MCAST; 27239187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 27339187Ssklower ifp->if_imcasts++; 27435379Skfall 27535379Skfall switch (eh->ether_type) { 27635379Skfall #ifdef INET 27735379Skfall case ETHERTYPE_IP: 27835379Skfall schednetisr(NETISR_IP); 27935379Skfall inq = &ipintrq; 28035379Skfall break; 28135379Skfall 28235379Skfall case ETHERTYPE_ARP: 28350133Ssklower schednetisr(NETISR_ARP); 28450133Ssklower inq = &arpintrq; 28550133Ssklower break; 28635379Skfall #endif 28735379Skfall #ifdef NS 28835379Skfall case ETHERTYPE_NS: 28935379Skfall schednetisr(NETISR_NS); 29035379Skfall inq = &nsintrq; 29135379Skfall break; 29235379Skfall 29335379Skfall #endif 29435379Skfall default: 29538845Sroot #ifdef ISO 29637472Ssklower if (eh->ether_type > ETHERMTU) 29735795Skarels goto dropanyway; 29835795Skarels l = mtod(m, struct llc *); 29935795Skarels switch (l->llc_control) { 30035795Skarels case LLC_UI: 30135795Skarels /* LLC_UI_P forbidden in class 1 service */ 30235795Skarels if ((l->llc_dsap == LLC_ISO_LSAP) && 30335795Skarels (l->llc_ssap == LLC_ISO_LSAP)) { 30435795Skarels /* LSAP for ISO */ 30545632Ssklower if (m->m_pkthdr.len > eh->ether_type) 30645632Ssklower m_adj(m, eh->ether_type - m->m_pkthdr.len); 30738845Sroot m->m_data += 3; /* XXX */ 30838845Sroot m->m_len -= 3; /* XXX */ 30938845Sroot m->m_pkthdr.len -= 3; /* XXX */ 31037472Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 31137472Ssklower if (m == 0) 31237472Ssklower return; 31337472Ssklower *mtod(m, struct ether_header *) = *eh; 31437472Ssklower IFDEBUG(D_ETHER) 31537472Ssklower printf("clnp packet"); 31637472Ssklower ENDDEBUG 31737472Ssklower schednetisr(NETISR_ISO); 31835795Skarels inq = &clnlintrq; 31938845Sroot break; 32035795Skarels } 32138845Sroot goto dropanyway; 32238845Sroot 32335795Skarels case LLC_XID: 32435795Skarels case LLC_XID_P: 32535795Skarels if(m->m_len < 6) 32635795Skarels goto dropanyway; 32735795Skarels l->llc_window = 0; 32835795Skarels l->llc_fid = 9; 32935795Skarels l->llc_class = 1; 33035795Skarels l->llc_dsap = l->llc_ssap = 0; 33135795Skarels /* Fall through to */ 33235795Skarels case LLC_TEST: 33335795Skarels case LLC_TEST_P: 33435795Skarels { 33535795Skarels struct sockaddr sa; 33635795Skarels register struct ether_header *eh2; 33735795Skarels int i; 33835795Skarels u_char c = l->llc_dsap; 33935795Skarels l->llc_dsap = l->llc_ssap; 34035795Skarels l->llc_ssap = c; 34137472Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 34237472Ssklower bcopy((caddr_t)ac->ac_enaddr, 34337472Ssklower (caddr_t)eh->ether_dhost, 6); 34435795Skarels sa.sa_family = AF_UNSPEC; 34537472Ssklower sa.sa_len = sizeof(sa); 34635795Skarels eh2 = (struct ether_header *)sa.sa_data; 34735795Skarels for (i = 0; i < 6; i++) { 34835795Skarels eh2->ether_shost[i] = c = eh->ether_dhost[i]; 34935795Skarels eh2->ether_dhost[i] = 35035795Skarels eh->ether_dhost[i] = eh->ether_shost[i]; 35135795Skarels eh->ether_shost[i] = c; 35235795Skarels } 35352448Ssklower ifp->if_output(ifp, m, &sa, (struct rtentry *)0); 35435795Skarels return; 35535795Skarels } 35635795Skarels dropanyway: 35735795Skarels default: 35835795Skarels m_freem(m); 35935795Skarels return; 36035795Skarels } 36138845Sroot #else 36238845Sroot m_freem(m); 36338845Sroot return; 36438845Sroot #endif ISO 36535379Skfall } 36635379Skfall 36735379Skfall s = splimp(); 36835379Skfall if (IF_QFULL(inq)) { 36935379Skfall IF_DROP(inq); 37035379Skfall m_freem(m); 37135379Skfall } else 37235379Skfall IF_ENQUEUE(inq, m); 37335379Skfall splx(s); 37435379Skfall } 37535379Skfall 37635379Skfall /* 37735379Skfall * Convert Ethernet address to printable (loggable) representation. 37835379Skfall */ 37937472Ssklower static char digits[] = "0123456789abcdef"; 38035379Skfall char * 38135379Skfall ether_sprintf(ap) 38235379Skfall register u_char *ap; 38335379Skfall { 38435379Skfall register i; 38535379Skfall static char etherbuf[18]; 38635379Skfall register char *cp = etherbuf; 38735379Skfall 38835379Skfall for (i = 0; i < 6; i++) { 38935379Skfall *cp++ = digits[*ap >> 4]; 39035379Skfall *cp++ = digits[*ap++ & 0xf]; 39135379Skfall *cp++ = ':'; 39235379Skfall } 39335379Skfall *--cp = 0; 39435379Skfall return (etherbuf); 39535379Skfall } 39652567Ssklower 39752567Ssklower /* 39852567Ssklower * Perform common duties while attaching to interface list 39952567Ssklower */ 40052567Ssklower ether_ifattach(ifp) 40152567Ssklower register struct ifnet *ifp; 40252567Ssklower { 40352567Ssklower register struct ifaddr *ifa; 40452567Ssklower register struct sockaddr_dl *sdl; 40552567Ssklower 40652567Ssklower ifp->if_type = IFT_ETHER; 40752567Ssklower ifp->if_addrlen = 6; 40852567Ssklower ifp->if_hdrlen = 14; 40952567Ssklower ifp->if_mtu = ETHERMTU; 41052567Ssklower for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 41152567Ssklower if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) && 41252567Ssklower sdl->sdl_family == AF_LINK) { 41352567Ssklower sdl->sdl_type = IFT_ETHER; 41452567Ssklower sdl->sdl_alen = ifp->if_addrlen; 41552567Ssklower bcopy((caddr_t)((struct arpcom *)ifp)->ac_enaddr, 41652567Ssklower LLADDR(sdl), ifp->if_addrlen); 41752567Ssklower break; 41852567Ssklower } 41952567Ssklower } 420