135379Skfall /* 239187Ssklower * Copyright (c) 1982, 1989 Regents of the University of California. 335379Skfall * All rights reserved. 435379Skfall * 535379Skfall * Redistribution and use in source and binary forms are permitted 635379Skfall * provided that the above copyright notice and this paragraph are 735379Skfall * duplicated in all such forms and that any documentation, 835379Skfall * advertising materials, and other materials related to such 935379Skfall * distribution and use acknowledge that the software was developed 1035379Skfall * by the University of California, Berkeley. The name of the 1135379Skfall * University may not be used to endorse or promote products derived 1235379Skfall * from this software without specific prior written permission. 1335379Skfall * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435379Skfall * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535379Skfall * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1635379Skfall * 17*41546Smckusick * @(#)if_ethersubr.c 7.7 (Berkeley) 05/10/90 1835379Skfall */ 1935379Skfall 2035379Skfall #include "param.h" 2135379Skfall #include "systm.h" 2235379Skfall #include "malloc.h" 2335379Skfall #include "mbuf.h" 2435379Skfall #include "protosw.h" 2535379Skfall #include "socket.h" 2635379Skfall #include "ioctl.h" 2735379Skfall #include "errno.h" 2835379Skfall #include "syslog.h" 2935379Skfall 3035379Skfall #include "if.h" 3135379Skfall #include "netisr.h" 3235379Skfall #include "route.h" 3335795Skarels #include "if_llc.h" 3440790Ssklower #include "if_dl.h" 3535379Skfall 3637517Smckusick #include "machine/mtpr.h" 3735379Skfall 3835379Skfall #ifdef INET 3935379Skfall #include "../netinet/in.h" 4035379Skfall #include "../netinet/in_var.h" 4135379Skfall #include "../netinet/if_ether.h" 4235379Skfall #endif 4335379Skfall 4435379Skfall #ifdef NS 4535379Skfall #include "../netns/ns.h" 4635379Skfall #include "../netns/ns_if.h" 4735379Skfall #endif 4835379Skfall 4937472Ssklower #ifdef ISO 5037472Ssklower #include "../netiso/argo_debug.h" 5137472Ssklower #include "../netiso/iso.h" 5237472Ssklower #include "../netiso/iso_var.h" 5337472Ssklower #endif 5437472Ssklower 5535379Skfall u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 5635795Skarels extern struct ifnet loif; 5735379Skfall 5835379Skfall /* 5935379Skfall * Ethernet output routine. 6035379Skfall * Encapsulate a packet of type family for the local net. 6135379Skfall * Use trailer local net encapsulation if enough data in first 6235379Skfall * packet leaves a multiple of 512 bytes of data in remainder. 6335379Skfall * Assumes that ifp is actually pointer to arpcom structure. 6435379Skfall */ 6540790Ssklower ether_output(ifp, m0, dst, rt) 6635379Skfall register struct ifnet *ifp; 6735379Skfall struct mbuf *m0; 6835379Skfall struct sockaddr *dst; 6940790Ssklower struct rtentry *rt; 7035379Skfall { 7135795Skarels short type; 7235795Skarels int s, error = 0; 7335379Skfall u_char edst[6]; 7435379Skfall struct in_addr idst; 7535379Skfall register struct mbuf *m = m0; 7635795Skarels struct mbuf *mcopy = (struct mbuf *)0; 7735379Skfall register struct ether_header *eh; 7839187Ssklower int usetrailers, off, len = m->m_pkthdr.len; 7939187Ssklower extern struct timeval time; 8035379Skfall #define ac ((struct arpcom *)ifp) 8135379Skfall 8235379Skfall if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 8335379Skfall error = ENETDOWN; 8435379Skfall goto bad; 8535379Skfall } 8639187Ssklower ifp->if_lastchange = time; 8735379Skfall switch (dst->sa_family) { 8835379Skfall 8935379Skfall #ifdef INET 9035379Skfall case AF_INET: 9135379Skfall idst = ((struct sockaddr_in *)dst)->sin_addr; 9235379Skfall if (!arpresolve(ac, m, &idst, edst, &usetrailers)) 9335379Skfall return (0); /* if not yet resolved */ 9437472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 95*41546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 9635379Skfall off = m->m_pkthdr.len - m->m_len; 9735379Skfall if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 9835379Skfall (m->m_flags & M_EXT) == 0 && 9935379Skfall m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 10035379Skfall type = ETHERTYPE_TRAIL + (off>>9); 10135379Skfall m->m_data -= 2 * sizeof (u_short); 10235379Skfall m->m_len += 2 * sizeof (u_short); 10339187Ssklower len += 2 * sizeof (u_short); 10435379Skfall *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 10535379Skfall *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 10635379Skfall goto gottrailertype; 10735379Skfall } 10835379Skfall type = ETHERTYPE_IP; 10935379Skfall goto gottype; 11035379Skfall #endif 11135379Skfall #ifdef NS 11235379Skfall case AF_NS: 11335379Skfall type = ETHERTYPE_NS; 11439187Ssklower bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 11539187Ssklower (caddr_t)edst, sizeof (edst)); 11635795Skarels if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 11735795Skarels return(looutput(&loif, m, dst)); 11837472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 119*41546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 12035379Skfall goto gottype; 12135379Skfall #endif 12235795Skarels #ifdef ISO 12335795Skarels case AF_ISO: { 12437472Ssklower int len; 12535795Skarels int ret; 12637472Ssklower struct llc *l; 12737472Ssklower 12840790Ssklower if (rt && rt->rt_gateway && 12940790Ssklower rt->rt_gateway->sa_family == AF_LINK) { 13040790Ssklower register struct sockaddr_dl *sdl; 13140790Ssklower sdl = (struct sockaddr_dl *)rt->rt_gateway; 13240790Ssklower if (len = sdl->sdl_nlen) { 13340790Ssklower bcopy(LLADDR(sdl), (char *)edst, len); 13440790Ssklower goto iso_resolved; 13540790Ssklower } 13640790Ssklower } 13737472Ssklower if ((ret = iso_tryloopback(m, (struct sockaddr_iso *)dst)) >= 0) 13837472Ssklower return (ret); 13937472Ssklower ret = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 14037472Ssklower (char *)edst, &len); 14137472Ssklower if (ret > 0) { 14237472Ssklower m_freem(m); /* Not Resolved */ 14337472Ssklower return(ret); 14435795Skarels } 14540790Ssklower iso_resolved: 14635795Skarels M_PREPEND(m, 3, M_DONTWAIT); 14737472Ssklower if (m == NULL) 14835795Skarels return(0); 14935795Skarels type = m->m_pkthdr.len; 15035795Skarels l = mtod(m, struct llc *); 15135795Skarels l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 15235795Skarels l->llc_control = LLC_UI; 15339187Ssklower len += 3; 15435795Skarels IFDEBUG(D_ETHER) 15535795Skarels int i; 15635795Skarels printf("unoutput: sending pkt to: "); 15735795Skarels for (i=0; i<6; i++) 15835795Skarels printf("%x ", edst[i] & 0xff); 15935795Skarels printf("\n"); 16035795Skarels ENDDEBUG 16135795Skarels } goto gottype; 16235795Skarels #endif ISO 163*41546Smckusick #ifdef RMP 164*41546Smckusick case AF_RMP: 165*41546Smckusick /* 166*41546Smckusick * This is IEEE 802.3 -- the Ethernet `type' field is 167*41546Smckusick * really a `length' field. 168*41546Smckusick */ 169*41546Smckusick type = m->m_len; 170*41546Smckusick bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 171*41546Smckusick break; 172*41546Smckusick #endif 173*41546Smckusick 17435379Skfall case AF_UNSPEC: 17535379Skfall eh = (struct ether_header *)dst->sa_data; 17635379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 17735379Skfall type = eh->ether_type; 17835379Skfall goto gottype; 17935379Skfall 18035379Skfall default: 18135379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 18235379Skfall dst->sa_family); 18335379Skfall error = EAFNOSUPPORT; 18435379Skfall goto bad; 18535379Skfall } 18635379Skfall 18735379Skfall gottrailertype: 18835379Skfall /* 18935379Skfall * Packet to be sent as trailer: move first packet 19035379Skfall * (control information) to end of chain. 19135379Skfall */ 19235379Skfall while (m->m_next) 19335379Skfall m = m->m_next; 19435379Skfall m->m_next = m0; 19535379Skfall m = m0->m_next; 19635379Skfall m0->m_next = 0; 19735379Skfall 19835379Skfall gottype: 19935379Skfall /* 20035379Skfall * Add local net header. If no space in first mbuf, 20135379Skfall * allocate another. 20235379Skfall */ 20335379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 20435379Skfall if (m == 0) { 20535379Skfall error = ENOBUFS; 20635379Skfall goto bad; 20735379Skfall } 20835379Skfall eh = mtod(m, struct ether_header *); 20935795Skarels type = htons((u_short)type); 21035795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 21135795Skarels sizeof(eh->ether_type)); 21235379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 21335379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 21435379Skfall sizeof(eh->ether_shost)); 21535379Skfall /* 21635379Skfall * Queue message on interface, and start output if interface 21735379Skfall * not yet active. 21835379Skfall */ 21935379Skfall s = splimp(); 22035379Skfall if (IF_QFULL(&ifp->if_snd)) { 22135379Skfall IF_DROP(&ifp->if_snd); 22235379Skfall splx(s); 22335795Skarels error = ENOBUFS; 22435795Skarels goto bad; 22535379Skfall } 22635379Skfall IF_ENQUEUE(&ifp->if_snd, m); 22735379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 22838845Sroot (*ifp->if_start)(ifp); 22935379Skfall splx(s); 23035795Skarels if (mcopy) 23135795Skarels (void) looutput(&loif, mcopy, dst); 23239187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 23339187Ssklower if (edst[0] & 1) 23439187Ssklower ifp->if_omcasts++; 23535795Skarels return (error); 23635379Skfall 23735379Skfall bad: 23835795Skarels if (mcopy) 23935795Skarels m_freem(mcopy); 24035795Skarels if (m) 24135795Skarels m_freem(m); 24235379Skfall return (error); 24335379Skfall } 24435379Skfall 24535379Skfall /* 24638845Sroot * Process a received Ethernet packet; 24738845Sroot * the packet is in the mbuf chain m without 24838845Sroot * the ether header, which is provided separately. 24935379Skfall */ 25037472Ssklower ether_input(ifp, eh, m) 25135379Skfall struct ifnet *ifp; 25235379Skfall register struct ether_header *eh; 25335379Skfall struct mbuf *m; 25435379Skfall { 25535379Skfall register struct ifqueue *inq; 25635795Skarels register struct llc *l; 25735379Skfall int s; 25835379Skfall 25939187Ssklower ifp->if_lastchange = time; 26039187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 26135795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 26235379Skfall sizeof(etherbroadcastaddr)) == 0) 26335379Skfall m->m_flags |= M_BCAST; 26435795Skarels else if (eh->ether_dhost[0] & 1) 26535795Skarels m->m_flags |= M_MCAST; 26639187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 26739187Ssklower ifp->if_imcasts++; 26835379Skfall 26935379Skfall switch (eh->ether_type) { 27035379Skfall #ifdef INET 27135379Skfall case ETHERTYPE_IP: 27235379Skfall schednetisr(NETISR_IP); 27335379Skfall inq = &ipintrq; 27435379Skfall break; 27535379Skfall 27635379Skfall case ETHERTYPE_ARP: 27735379Skfall arpinput((struct arpcom *)ifp, m); 27835379Skfall return; 27935379Skfall #endif 28035379Skfall #ifdef NS 28135379Skfall case ETHERTYPE_NS: 28235379Skfall schednetisr(NETISR_NS); 28335379Skfall inq = &nsintrq; 28435379Skfall break; 28535379Skfall 28635379Skfall #endif 28735379Skfall default: 28838845Sroot #ifdef ISO 28937472Ssklower if (eh->ether_type > ETHERMTU) 29035795Skarels goto dropanyway; 29135795Skarels l = mtod(m, struct llc *); 29235795Skarels switch (l->llc_control) { 29335795Skarels case LLC_UI: 29435795Skarels /* LLC_UI_P forbidden in class 1 service */ 29535795Skarels if ((l->llc_dsap == LLC_ISO_LSAP) && 29635795Skarels (l->llc_ssap == LLC_ISO_LSAP)) { 29735795Skarels /* LSAP for ISO */ 29838845Sroot m->m_data += 3; /* XXX */ 29938845Sroot m->m_len -= 3; /* XXX */ 30038845Sroot m->m_pkthdr.len -= 3; /* XXX */ 30137472Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 30237472Ssklower if (m == 0) 30337472Ssklower return; 30437472Ssklower *mtod(m, struct ether_header *) = *eh; 30537472Ssklower IFDEBUG(D_ETHER) 30637472Ssklower printf("clnp packet"); 30737472Ssklower ENDDEBUG 30837472Ssklower schednetisr(NETISR_ISO); 30935795Skarels inq = &clnlintrq; 31038845Sroot break; 31135795Skarels } 31238845Sroot goto dropanyway; 31338845Sroot 31435795Skarels case LLC_XID: 31535795Skarels case LLC_XID_P: 31635795Skarels if(m->m_len < 6) 31735795Skarels goto dropanyway; 31835795Skarels l->llc_window = 0; 31935795Skarels l->llc_fid = 9; 32035795Skarels l->llc_class = 1; 32135795Skarels l->llc_dsap = l->llc_ssap = 0; 32235795Skarels /* Fall through to */ 32335795Skarels case LLC_TEST: 32435795Skarels case LLC_TEST_P: 32535795Skarels { 32635795Skarels struct sockaddr sa; 32735795Skarels register struct ether_header *eh2; 32835795Skarels int i; 32935795Skarels u_char c = l->llc_dsap; 33035795Skarels l->llc_dsap = l->llc_ssap; 33135795Skarels l->llc_ssap = c; 33237472Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 33337472Ssklower bcopy((caddr_t)ac->ac_enaddr, 33437472Ssklower (caddr_t)eh->ether_dhost, 6); 33535795Skarels sa.sa_family = AF_UNSPEC; 33637472Ssklower sa.sa_len = sizeof(sa); 33735795Skarels eh2 = (struct ether_header *)sa.sa_data; 33835795Skarels for (i = 0; i < 6; i++) { 33935795Skarels eh2->ether_shost[i] = c = eh->ether_dhost[i]; 34035795Skarels eh2->ether_dhost[i] = 34135795Skarels eh->ether_dhost[i] = eh->ether_shost[i]; 34235795Skarels eh->ether_shost[i] = c; 34335795Skarels } 34435795Skarels ifp->if_output(ifp, m, &sa); 34535795Skarels return; 34635795Skarels } 34735795Skarels dropanyway: 34835795Skarels default: 34935795Skarels m_freem(m); 35035795Skarels return; 35135795Skarels } 35238845Sroot #else 35338845Sroot m_freem(m); 35438845Sroot return; 35538845Sroot #endif ISO 35635379Skfall } 35735379Skfall 35835379Skfall s = splimp(); 35935379Skfall if (IF_QFULL(inq)) { 36035379Skfall IF_DROP(inq); 36135379Skfall m_freem(m); 36235379Skfall } else 36335379Skfall IF_ENQUEUE(inq, m); 36435379Skfall splx(s); 36535379Skfall } 36635379Skfall 36735379Skfall /* 36835379Skfall * Convert Ethernet address to printable (loggable) representation. 36935379Skfall */ 37037472Ssklower static char digits[] = "0123456789abcdef"; 37135379Skfall char * 37235379Skfall ether_sprintf(ap) 37335379Skfall register u_char *ap; 37435379Skfall { 37535379Skfall register i; 37635379Skfall static char etherbuf[18]; 37735379Skfall register char *cp = etherbuf; 37835379Skfall 37935379Skfall for (i = 0; i < 6; i++) { 38035379Skfall *cp++ = digits[*ap >> 4]; 38135379Skfall *cp++ = digits[*ap++ & 0xf]; 38235379Skfall *cp++ = ':'; 38335379Skfall } 38435379Skfall *--cp = 0; 38535379Skfall return (etherbuf); 38635379Skfall } 387