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*40790Ssklower * @(#)if_ethersubr.c 7.6 (Berkeley) 04/05/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" 34*40790Ssklower #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 */ 65*40790Ssklower ether_output(ifp, m0, dst, rt) 6635379Skfall register struct ifnet *ifp; 6735379Skfall struct mbuf *m0; 6835379Skfall struct sockaddr *dst; 69*40790Ssklower 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)) 9537472Ssklower 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)) 11937472Ssklower 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 128*40790Ssklower if (rt && rt->rt_gateway && 129*40790Ssklower rt->rt_gateway->sa_family == AF_LINK) { 130*40790Ssklower register struct sockaddr_dl *sdl; 131*40790Ssklower sdl = (struct sockaddr_dl *)rt->rt_gateway; 132*40790Ssklower if (len = sdl->sdl_nlen) { 133*40790Ssklower bcopy(LLADDR(sdl), (char *)edst, len); 134*40790Ssklower goto iso_resolved; 135*40790Ssklower } 136*40790Ssklower } 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 } 145*40790Ssklower 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 16335379Skfall case AF_UNSPEC: 16435379Skfall eh = (struct ether_header *)dst->sa_data; 16535379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 16635379Skfall type = eh->ether_type; 16735379Skfall goto gottype; 16835379Skfall 16935379Skfall default: 17035379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 17135379Skfall dst->sa_family); 17235379Skfall error = EAFNOSUPPORT; 17335379Skfall goto bad; 17435379Skfall } 17535379Skfall 17635379Skfall gottrailertype: 17735379Skfall /* 17835379Skfall * Packet to be sent as trailer: move first packet 17935379Skfall * (control information) to end of chain. 18035379Skfall */ 18135379Skfall while (m->m_next) 18235379Skfall m = m->m_next; 18335379Skfall m->m_next = m0; 18435379Skfall m = m0->m_next; 18535379Skfall m0->m_next = 0; 18635379Skfall 18735379Skfall gottype: 18835379Skfall /* 18935379Skfall * Add local net header. If no space in first mbuf, 19035379Skfall * allocate another. 19135379Skfall */ 19235379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 19335379Skfall if (m == 0) { 19435379Skfall error = ENOBUFS; 19535379Skfall goto bad; 19635379Skfall } 19735379Skfall eh = mtod(m, struct ether_header *); 19835795Skarels type = htons((u_short)type); 19935795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 20035795Skarels sizeof(eh->ether_type)); 20135379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 20235379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 20335379Skfall sizeof(eh->ether_shost)); 20435379Skfall /* 20535379Skfall * Queue message on interface, and start output if interface 20635379Skfall * not yet active. 20735379Skfall */ 20835379Skfall s = splimp(); 20935379Skfall if (IF_QFULL(&ifp->if_snd)) { 21035379Skfall IF_DROP(&ifp->if_snd); 21135379Skfall splx(s); 21235795Skarels error = ENOBUFS; 21335795Skarels goto bad; 21435379Skfall } 21535379Skfall IF_ENQUEUE(&ifp->if_snd, m); 21635379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 21738845Sroot (*ifp->if_start)(ifp); 21835379Skfall splx(s); 21935795Skarels if (mcopy) 22035795Skarels (void) looutput(&loif, mcopy, dst); 22139187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 22239187Ssklower if (edst[0] & 1) 22339187Ssklower ifp->if_omcasts++; 22435795Skarels return (error); 22535379Skfall 22635379Skfall bad: 22735795Skarels if (mcopy) 22835795Skarels m_freem(mcopy); 22935795Skarels if (m) 23035795Skarels m_freem(m); 23135379Skfall return (error); 23235379Skfall } 23335379Skfall 23435379Skfall /* 23538845Sroot * Process a received Ethernet packet; 23638845Sroot * the packet is in the mbuf chain m without 23738845Sroot * the ether header, which is provided separately. 23835379Skfall */ 23937472Ssklower ether_input(ifp, eh, m) 24035379Skfall struct ifnet *ifp; 24135379Skfall register struct ether_header *eh; 24235379Skfall struct mbuf *m; 24335379Skfall { 24435379Skfall register struct ifqueue *inq; 24535795Skarels register struct llc *l; 24635379Skfall int s; 24735379Skfall 24839187Ssklower ifp->if_lastchange = time; 24939187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 25035795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 25135379Skfall sizeof(etherbroadcastaddr)) == 0) 25235379Skfall m->m_flags |= M_BCAST; 25335795Skarels else if (eh->ether_dhost[0] & 1) 25435795Skarels m->m_flags |= M_MCAST; 25539187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 25639187Ssklower ifp->if_imcasts++; 25735379Skfall 25835379Skfall switch (eh->ether_type) { 25935379Skfall #ifdef INET 26035379Skfall case ETHERTYPE_IP: 26135379Skfall schednetisr(NETISR_IP); 26235379Skfall inq = &ipintrq; 26335379Skfall break; 26435379Skfall 26535379Skfall case ETHERTYPE_ARP: 26635379Skfall arpinput((struct arpcom *)ifp, m); 26735379Skfall return; 26835379Skfall #endif 26935379Skfall #ifdef NS 27035379Skfall case ETHERTYPE_NS: 27135379Skfall schednetisr(NETISR_NS); 27235379Skfall inq = &nsintrq; 27335379Skfall break; 27435379Skfall 27535379Skfall #endif 27635379Skfall default: 27738845Sroot #ifdef ISO 27837472Ssklower if (eh->ether_type > ETHERMTU) 27935795Skarels goto dropanyway; 28035795Skarels l = mtod(m, struct llc *); 28135795Skarels switch (l->llc_control) { 28235795Skarels case LLC_UI: 28335795Skarels /* LLC_UI_P forbidden in class 1 service */ 28435795Skarels if ((l->llc_dsap == LLC_ISO_LSAP) && 28535795Skarels (l->llc_ssap == LLC_ISO_LSAP)) { 28635795Skarels /* LSAP for ISO */ 28738845Sroot m->m_data += 3; /* XXX */ 28838845Sroot m->m_len -= 3; /* XXX */ 28938845Sroot m->m_pkthdr.len -= 3; /* XXX */ 29037472Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 29137472Ssklower if (m == 0) 29237472Ssklower return; 29337472Ssklower *mtod(m, struct ether_header *) = *eh; 29437472Ssklower IFDEBUG(D_ETHER) 29537472Ssklower printf("clnp packet"); 29637472Ssklower ENDDEBUG 29737472Ssklower schednetisr(NETISR_ISO); 29835795Skarels inq = &clnlintrq; 29938845Sroot break; 30035795Skarels } 30138845Sroot goto dropanyway; 30238845Sroot 30335795Skarels case LLC_XID: 30435795Skarels case LLC_XID_P: 30535795Skarels if(m->m_len < 6) 30635795Skarels goto dropanyway; 30735795Skarels l->llc_window = 0; 30835795Skarels l->llc_fid = 9; 30935795Skarels l->llc_class = 1; 31035795Skarels l->llc_dsap = l->llc_ssap = 0; 31135795Skarels /* Fall through to */ 31235795Skarels case LLC_TEST: 31335795Skarels case LLC_TEST_P: 31435795Skarels { 31535795Skarels struct sockaddr sa; 31635795Skarels register struct ether_header *eh2; 31735795Skarels int i; 31835795Skarels u_char c = l->llc_dsap; 31935795Skarels l->llc_dsap = l->llc_ssap; 32035795Skarels l->llc_ssap = c; 32137472Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 32237472Ssklower bcopy((caddr_t)ac->ac_enaddr, 32337472Ssklower (caddr_t)eh->ether_dhost, 6); 32435795Skarels sa.sa_family = AF_UNSPEC; 32537472Ssklower sa.sa_len = sizeof(sa); 32635795Skarels eh2 = (struct ether_header *)sa.sa_data; 32735795Skarels for (i = 0; i < 6; i++) { 32835795Skarels eh2->ether_shost[i] = c = eh->ether_dhost[i]; 32935795Skarels eh2->ether_dhost[i] = 33035795Skarels eh->ether_dhost[i] = eh->ether_shost[i]; 33135795Skarels eh->ether_shost[i] = c; 33235795Skarels } 33335795Skarels ifp->if_output(ifp, m, &sa); 33435795Skarels return; 33535795Skarels } 33635795Skarels dropanyway: 33735795Skarels default: 33835795Skarels m_freem(m); 33935795Skarels return; 34035795Skarels } 34138845Sroot #else 34238845Sroot m_freem(m); 34338845Sroot return; 34438845Sroot #endif ISO 34535379Skfall } 34635379Skfall 34735379Skfall s = splimp(); 34835379Skfall if (IF_QFULL(inq)) { 34935379Skfall IF_DROP(inq); 35035379Skfall m_freem(m); 35135379Skfall } else 35235379Skfall IF_ENQUEUE(inq, m); 35335379Skfall splx(s); 35435379Skfall } 35535379Skfall 35635379Skfall /* 35735379Skfall * Convert Ethernet address to printable (loggable) representation. 35835379Skfall */ 35937472Ssklower static char digits[] = "0123456789abcdef"; 36035379Skfall char * 36135379Skfall ether_sprintf(ap) 36235379Skfall register u_char *ap; 36335379Skfall { 36435379Skfall register i; 36535379Skfall static char etherbuf[18]; 36635379Skfall register char *cp = etherbuf; 36735379Skfall 36835379Skfall for (i = 0; i < 6; i++) { 36935379Skfall *cp++ = digits[*ap >> 4]; 37035379Skfall *cp++ = digits[*ap++ & 0xf]; 37135379Skfall *cp++ = ':'; 37235379Skfall } 37335379Skfall *--cp = 0; 37435379Skfall return (etherbuf); 37535379Skfall } 376