135379Skfall /* 239187Ssklower * Copyright (c) 1982, 1989 Regents of the University of California. 335379Skfall * All rights reserved. 435379Skfall * 5*44464Sbostic * %sccs.include.redist.c% 635379Skfall * 7*44464Sbostic * @(#)if_ethersubr.c 7.10 (Berkeley) 06/28/90 835379Skfall */ 935379Skfall 1035379Skfall #include "param.h" 1135379Skfall #include "systm.h" 1235379Skfall #include "malloc.h" 1335379Skfall #include "mbuf.h" 1435379Skfall #include "protosw.h" 1535379Skfall #include "socket.h" 1635379Skfall #include "ioctl.h" 1735379Skfall #include "errno.h" 1835379Skfall #include "syslog.h" 1935379Skfall 2035379Skfall #include "if.h" 2135379Skfall #include "netisr.h" 2235379Skfall #include "route.h" 2335795Skarels #include "if_llc.h" 2440790Ssklower #include "if_dl.h" 2535379Skfall 2637517Smckusick #include "machine/mtpr.h" 2735379Skfall 2835379Skfall #ifdef INET 2935379Skfall #include "../netinet/in.h" 3035379Skfall #include "../netinet/in_var.h" 3135379Skfall #include "../netinet/if_ether.h" 3235379Skfall #endif 3335379Skfall 3435379Skfall #ifdef NS 3535379Skfall #include "../netns/ns.h" 3635379Skfall #include "../netns/ns_if.h" 3735379Skfall #endif 3835379Skfall 3937472Ssklower #ifdef ISO 4037472Ssklower #include "../netiso/argo_debug.h" 4137472Ssklower #include "../netiso/iso.h" 4237472Ssklower #include "../netiso/iso_var.h" 4343073Ssklower #include "../netiso/iso_snpac.h" 4437472Ssklower #endif 4537472Ssklower 4635379Skfall u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 4735795Skarels extern struct ifnet loif; 4835379Skfall 4935379Skfall /* 5035379Skfall * Ethernet output routine. 5135379Skfall * Encapsulate a packet of type family for the local net. 5235379Skfall * Use trailer local net encapsulation if enough data in first 5335379Skfall * packet leaves a multiple of 512 bytes of data in remainder. 5435379Skfall * Assumes that ifp is actually pointer to arpcom structure. 5535379Skfall */ 5640790Ssklower ether_output(ifp, m0, dst, rt) 5735379Skfall register struct ifnet *ifp; 5835379Skfall struct mbuf *m0; 5935379Skfall struct sockaddr *dst; 6040790Ssklower struct rtentry *rt; 6135379Skfall { 6235795Skarels short type; 6335795Skarels int s, error = 0; 6435379Skfall u_char edst[6]; 6535379Skfall struct in_addr idst; 6635379Skfall register struct mbuf *m = m0; 6735795Skarels struct mbuf *mcopy = (struct mbuf *)0; 6835379Skfall register struct ether_header *eh; 6939187Ssklower int usetrailers, off, len = m->m_pkthdr.len; 7039187Ssklower extern struct timeval time; 7135379Skfall #define ac ((struct arpcom *)ifp) 7235379Skfall 7335379Skfall if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 7435379Skfall error = ENETDOWN; 7535379Skfall goto bad; 7635379Skfall } 7739187Ssklower ifp->if_lastchange = time; 7835379Skfall switch (dst->sa_family) { 7935379Skfall 8035379Skfall #ifdef INET 8135379Skfall case AF_INET: 8235379Skfall idst = ((struct sockaddr_in *)dst)->sin_addr; 8335379Skfall if (!arpresolve(ac, m, &idst, edst, &usetrailers)) 8435379Skfall return (0); /* if not yet resolved */ 8537472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 8641546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 8735379Skfall off = m->m_pkthdr.len - m->m_len; 8835379Skfall if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 8935379Skfall (m->m_flags & M_EXT) == 0 && 9035379Skfall m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 9135379Skfall type = ETHERTYPE_TRAIL + (off>>9); 9235379Skfall m->m_data -= 2 * sizeof (u_short); 9335379Skfall m->m_len += 2 * sizeof (u_short); 9439187Ssklower len += 2 * sizeof (u_short); 9535379Skfall *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 9635379Skfall *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 9735379Skfall goto gottrailertype; 9835379Skfall } 9935379Skfall type = ETHERTYPE_IP; 10035379Skfall goto gottype; 10135379Skfall #endif 10235379Skfall #ifdef NS 10335379Skfall case AF_NS: 10435379Skfall type = ETHERTYPE_NS; 10539187Ssklower bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 10639187Ssklower (caddr_t)edst, sizeof (edst)); 10735795Skarels if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 10843073Ssklower return (looutput(&loif, m, dst)); 10937472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 11041546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 11135379Skfall goto gottype; 11235379Skfall #endif 11335795Skarels #ifdef ISO 11435795Skarels case AF_ISO: { 11543073Ssklower int snpalen; 11637472Ssklower struct llc *l; 11737472Ssklower 11843073Ssklower iso_again: 11943336Ssklower iso_etherout(); 12043073Ssklower if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) { 12143073Ssklower if (rt->rt_flags & RTF_GATEWAY) { 12243336Ssklower if (rt->rt_llinfo) { 12343336Ssklower rt = (struct rtentry *)rt->rt_llinfo; 12443073Ssklower goto iso_again; 12543073Ssklower } 12643073Ssklower } else { 12743073Ssklower register struct sockaddr_dl *sdl = 12843073Ssklower (struct sockaddr_dl *)rt->rt_gateway; 12943073Ssklower if (sdl && sdl->sdl_family == AF_LINK 13043073Ssklower && sdl->sdl_alen > 0) { 13143073Ssklower bcopy(LLADDR(sdl), (char *)edst, 13243073Ssklower sizeof(edst)); 13343073Ssklower goto iso_resolved; 13443073Ssklower } 13540790Ssklower } 13640790Ssklower } 13743073Ssklower if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 13843073Ssklower (char *)edst, &snpalen)) > 0) 13943073Ssklower goto bad; /* Not Resolved */ 14040790Ssklower iso_resolved: 14135795Skarels M_PREPEND(m, 3, M_DONTWAIT); 14237472Ssklower if (m == NULL) 14343073Ssklower return (0); 14435795Skarels type = m->m_pkthdr.len; 14535795Skarels l = mtod(m, struct llc *); 14635795Skarels l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 14735795Skarels l->llc_control = LLC_UI; 14839187Ssklower len += 3; 14935795Skarels IFDEBUG(D_ETHER) 15035795Skarels int i; 15135795Skarels printf("unoutput: sending pkt to: "); 15235795Skarels for (i=0; i<6; i++) 15335795Skarels printf("%x ", edst[i] & 0xff); 15435795Skarels printf("\n"); 15535795Skarels ENDDEBUG 15635795Skarels } goto gottype; 15735795Skarels #endif ISO 15841546Smckusick #ifdef RMP 15941546Smckusick case AF_RMP: 16041546Smckusick /* 16141546Smckusick * This is IEEE 802.3 -- the Ethernet `type' field is 16241546Smckusick * really a `length' field. 16341546Smckusick */ 16441546Smckusick type = m->m_len; 16541546Smckusick bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 16641546Smckusick break; 16741546Smckusick #endif 16841546Smckusick 16935379Skfall case AF_UNSPEC: 17035379Skfall eh = (struct ether_header *)dst->sa_data; 17135379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 17235379Skfall type = eh->ether_type; 17335379Skfall goto gottype; 17435379Skfall 17535379Skfall default: 17635379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 17735379Skfall dst->sa_family); 17835379Skfall error = EAFNOSUPPORT; 17935379Skfall goto bad; 18035379Skfall } 18135379Skfall 18235379Skfall gottrailertype: 18335379Skfall /* 18435379Skfall * Packet to be sent as trailer: move first packet 18535379Skfall * (control information) to end of chain. 18635379Skfall */ 18735379Skfall while (m->m_next) 18835379Skfall m = m->m_next; 18935379Skfall m->m_next = m0; 19035379Skfall m = m0->m_next; 19135379Skfall m0->m_next = 0; 19235379Skfall 19335379Skfall gottype: 19435379Skfall /* 19535379Skfall * Add local net header. If no space in first mbuf, 19635379Skfall * allocate another. 19735379Skfall */ 19835379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 19935379Skfall if (m == 0) { 20035379Skfall error = ENOBUFS; 20135379Skfall goto bad; 20235379Skfall } 20335379Skfall eh = mtod(m, struct ether_header *); 20435795Skarels type = htons((u_short)type); 20535795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 20635795Skarels sizeof(eh->ether_type)); 20735379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 20835379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 20935379Skfall sizeof(eh->ether_shost)); 21035379Skfall /* 21135379Skfall * Queue message on interface, and start output if interface 21235379Skfall * not yet active. 21335379Skfall */ 21435379Skfall s = splimp(); 21535379Skfall if (IF_QFULL(&ifp->if_snd)) { 21635379Skfall IF_DROP(&ifp->if_snd); 21735379Skfall splx(s); 21835795Skarels error = ENOBUFS; 21935795Skarels goto bad; 22035379Skfall } 22135379Skfall IF_ENQUEUE(&ifp->if_snd, m); 22235379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 22338845Sroot (*ifp->if_start)(ifp); 22435379Skfall splx(s); 22535795Skarels if (mcopy) 22635795Skarels (void) looutput(&loif, mcopy, dst); 22739187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 22839187Ssklower if (edst[0] & 1) 22939187Ssklower ifp->if_omcasts++; 23035795Skarels return (error); 23135379Skfall 23235379Skfall bad: 23335795Skarels if (mcopy) 23435795Skarels m_freem(mcopy); 23535795Skarels if (m) 23635795Skarels m_freem(m); 23735379Skfall return (error); 23835379Skfall } 23935379Skfall 24035379Skfall /* 24138845Sroot * Process a received Ethernet packet; 24238845Sroot * the packet is in the mbuf chain m without 24338845Sroot * the ether header, which is provided separately. 24435379Skfall */ 24537472Ssklower ether_input(ifp, eh, m) 24635379Skfall struct ifnet *ifp; 24735379Skfall register struct ether_header *eh; 24835379Skfall struct mbuf *m; 24935379Skfall { 25035379Skfall register struct ifqueue *inq; 25135795Skarels register struct llc *l; 25235379Skfall int s; 25335379Skfall 25439187Ssklower ifp->if_lastchange = time; 25539187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 25635795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 25735379Skfall sizeof(etherbroadcastaddr)) == 0) 25835379Skfall m->m_flags |= M_BCAST; 25935795Skarels else if (eh->ether_dhost[0] & 1) 26035795Skarels m->m_flags |= M_MCAST; 26139187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 26239187Ssklower ifp->if_imcasts++; 26335379Skfall 26435379Skfall switch (eh->ether_type) { 26535379Skfall #ifdef INET 26635379Skfall case ETHERTYPE_IP: 26735379Skfall schednetisr(NETISR_IP); 26835379Skfall inq = &ipintrq; 26935379Skfall break; 27035379Skfall 27135379Skfall case ETHERTYPE_ARP: 27235379Skfall arpinput((struct arpcom *)ifp, m); 27335379Skfall return; 27435379Skfall #endif 27535379Skfall #ifdef NS 27635379Skfall case ETHERTYPE_NS: 27735379Skfall schednetisr(NETISR_NS); 27835379Skfall inq = &nsintrq; 27935379Skfall break; 28035379Skfall 28135379Skfall #endif 28235379Skfall default: 28338845Sroot #ifdef ISO 28437472Ssklower if (eh->ether_type > ETHERMTU) 28535795Skarels goto dropanyway; 28635795Skarels l = mtod(m, struct llc *); 28735795Skarels switch (l->llc_control) { 28835795Skarels case LLC_UI: 28935795Skarels /* LLC_UI_P forbidden in class 1 service */ 29035795Skarels if ((l->llc_dsap == LLC_ISO_LSAP) && 29135795Skarels (l->llc_ssap == LLC_ISO_LSAP)) { 29235795Skarels /* LSAP for ISO */ 29338845Sroot m->m_data += 3; /* XXX */ 29438845Sroot m->m_len -= 3; /* XXX */ 29538845Sroot m->m_pkthdr.len -= 3; /* XXX */ 29637472Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 29737472Ssklower if (m == 0) 29837472Ssklower return; 29937472Ssklower *mtod(m, struct ether_header *) = *eh; 30037472Ssklower IFDEBUG(D_ETHER) 30137472Ssklower printf("clnp packet"); 30237472Ssklower ENDDEBUG 30337472Ssklower schednetisr(NETISR_ISO); 30435795Skarels inq = &clnlintrq; 30538845Sroot break; 30635795Skarels } 30738845Sroot goto dropanyway; 30838845Sroot 30935795Skarels case LLC_XID: 31035795Skarels case LLC_XID_P: 31135795Skarels if(m->m_len < 6) 31235795Skarels goto dropanyway; 31335795Skarels l->llc_window = 0; 31435795Skarels l->llc_fid = 9; 31535795Skarels l->llc_class = 1; 31635795Skarels l->llc_dsap = l->llc_ssap = 0; 31735795Skarels /* Fall through to */ 31835795Skarels case LLC_TEST: 31935795Skarels case LLC_TEST_P: 32035795Skarels { 32135795Skarels struct sockaddr sa; 32235795Skarels register struct ether_header *eh2; 32335795Skarels int i; 32435795Skarels u_char c = l->llc_dsap; 32535795Skarels l->llc_dsap = l->llc_ssap; 32635795Skarels l->llc_ssap = c; 32737472Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 32837472Ssklower bcopy((caddr_t)ac->ac_enaddr, 32937472Ssklower (caddr_t)eh->ether_dhost, 6); 33035795Skarels sa.sa_family = AF_UNSPEC; 33137472Ssklower sa.sa_len = sizeof(sa); 33235795Skarels eh2 = (struct ether_header *)sa.sa_data; 33335795Skarels for (i = 0; i < 6; i++) { 33435795Skarels eh2->ether_shost[i] = c = eh->ether_dhost[i]; 33535795Skarels eh2->ether_dhost[i] = 33635795Skarels eh->ether_dhost[i] = eh->ether_shost[i]; 33735795Skarels eh->ether_shost[i] = c; 33835795Skarels } 33935795Skarels ifp->if_output(ifp, m, &sa); 34035795Skarels return; 34135795Skarels } 34235795Skarels dropanyway: 34335795Skarels default: 34435795Skarels m_freem(m); 34535795Skarels return; 34635795Skarels } 34738845Sroot #else 34838845Sroot m_freem(m); 34938845Sroot return; 35038845Sroot #endif ISO 35135379Skfall } 35235379Skfall 35335379Skfall s = splimp(); 35435379Skfall if (IF_QFULL(inq)) { 35535379Skfall IF_DROP(inq); 35635379Skfall m_freem(m); 35735379Skfall } else 35835379Skfall IF_ENQUEUE(inq, m); 35935379Skfall splx(s); 36035379Skfall } 36135379Skfall 36235379Skfall /* 36335379Skfall * Convert Ethernet address to printable (loggable) representation. 36435379Skfall */ 36537472Ssklower static char digits[] = "0123456789abcdef"; 36635379Skfall char * 36735379Skfall ether_sprintf(ap) 36835379Skfall register u_char *ap; 36935379Skfall { 37035379Skfall register i; 37135379Skfall static char etherbuf[18]; 37235379Skfall register char *cp = etherbuf; 37335379Skfall 37435379Skfall for (i = 0; i < 6; i++) { 37535379Skfall *cp++ = digits[*ap >> 4]; 37635379Skfall *cp++ = digits[*ap++ & 0xf]; 37735379Skfall *cp++ = ':'; 37835379Skfall } 37935379Skfall *--cp = 0; 38035379Skfall return (etherbuf); 38135379Skfall } 38243336Ssklower iso_etherout() {} 383