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*43336Ssklower * @(#)if_ethersubr.c 7.9 (Berkeley) 06/20/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" 5343073Ssklower #include "../netiso/iso_snpac.h" 5437472Ssklower #endif 5537472Ssklower 5635379Skfall u_char etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 5735795Skarels extern struct ifnet loif; 5835379Skfall 5935379Skfall /* 6035379Skfall * Ethernet output routine. 6135379Skfall * Encapsulate a packet of type family for the local net. 6235379Skfall * Use trailer local net encapsulation if enough data in first 6335379Skfall * packet leaves a multiple of 512 bytes of data in remainder. 6435379Skfall * Assumes that ifp is actually pointer to arpcom structure. 6535379Skfall */ 6640790Ssklower ether_output(ifp, m0, dst, rt) 6735379Skfall register struct ifnet *ifp; 6835379Skfall struct mbuf *m0; 6935379Skfall struct sockaddr *dst; 7040790Ssklower struct rtentry *rt; 7135379Skfall { 7235795Skarels short type; 7335795Skarels int s, error = 0; 7435379Skfall u_char edst[6]; 7535379Skfall struct in_addr idst; 7635379Skfall register struct mbuf *m = m0; 7735795Skarels struct mbuf *mcopy = (struct mbuf *)0; 7835379Skfall register struct ether_header *eh; 7939187Ssklower int usetrailers, off, len = m->m_pkthdr.len; 8039187Ssklower extern struct timeval time; 8135379Skfall #define ac ((struct arpcom *)ifp) 8235379Skfall 8335379Skfall if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) { 8435379Skfall error = ENETDOWN; 8535379Skfall goto bad; 8635379Skfall } 8739187Ssklower ifp->if_lastchange = time; 8835379Skfall switch (dst->sa_family) { 8935379Skfall 9035379Skfall #ifdef INET 9135379Skfall case AF_INET: 9235379Skfall idst = ((struct sockaddr_in *)dst)->sin_addr; 9335379Skfall if (!arpresolve(ac, m, &idst, edst, &usetrailers)) 9435379Skfall return (0); /* if not yet resolved */ 9537472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 9641546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 9735379Skfall off = m->m_pkthdr.len - m->m_len; 9835379Skfall if (usetrailers && off > 0 && (off & 0x1ff) == 0 && 9935379Skfall (m->m_flags & M_EXT) == 0 && 10035379Skfall m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) { 10135379Skfall type = ETHERTYPE_TRAIL + (off>>9); 10235379Skfall m->m_data -= 2 * sizeof (u_short); 10335379Skfall m->m_len += 2 * sizeof (u_short); 10439187Ssklower len += 2 * sizeof (u_short); 10535379Skfall *mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP); 10635379Skfall *(mtod(m, u_short *) + 1) = htons((u_short)m->m_len); 10735379Skfall goto gottrailertype; 10835379Skfall } 10935379Skfall type = ETHERTYPE_IP; 11035379Skfall goto gottype; 11135379Skfall #endif 11235379Skfall #ifdef NS 11335379Skfall case AF_NS: 11435379Skfall type = ETHERTYPE_NS; 11539187Ssklower bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host), 11639187Ssklower (caddr_t)edst, sizeof (edst)); 11735795Skarels if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))) 11843073Ssklower return (looutput(&loif, m, dst)); 11937472Ssklower if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1)) 12041546Smckusick mcopy = m_copy(m, 0, (int)M_COPYALL); 12135379Skfall goto gottype; 12235379Skfall #endif 12335795Skarels #ifdef ISO 12435795Skarels case AF_ISO: { 12543073Ssklower int snpalen; 12637472Ssklower struct llc *l; 12737472Ssklower 12843073Ssklower iso_again: 129*43336Ssklower iso_etherout(); 13043073Ssklower if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) { 13143073Ssklower if (rt->rt_flags & RTF_GATEWAY) { 132*43336Ssklower if (rt->rt_llinfo) { 133*43336Ssklower rt = (struct rtentry *)rt->rt_llinfo; 13443073Ssklower goto iso_again; 13543073Ssklower } 13643073Ssklower } else { 13743073Ssklower register struct sockaddr_dl *sdl = 13843073Ssklower (struct sockaddr_dl *)rt->rt_gateway; 13943073Ssklower if (sdl && sdl->sdl_family == AF_LINK 14043073Ssklower && sdl->sdl_alen > 0) { 14143073Ssklower bcopy(LLADDR(sdl), (char *)edst, 14243073Ssklower sizeof(edst)); 14343073Ssklower goto iso_resolved; 14443073Ssklower } 14540790Ssklower } 14640790Ssklower } 14743073Ssklower if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst, 14843073Ssklower (char *)edst, &snpalen)) > 0) 14943073Ssklower goto bad; /* Not Resolved */ 15040790Ssklower iso_resolved: 15135795Skarels M_PREPEND(m, 3, M_DONTWAIT); 15237472Ssklower if (m == NULL) 15343073Ssklower return (0); 15435795Skarels type = m->m_pkthdr.len; 15535795Skarels l = mtod(m, struct llc *); 15635795Skarels l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP; 15735795Skarels l->llc_control = LLC_UI; 15839187Ssklower len += 3; 15935795Skarels IFDEBUG(D_ETHER) 16035795Skarels int i; 16135795Skarels printf("unoutput: sending pkt to: "); 16235795Skarels for (i=0; i<6; i++) 16335795Skarels printf("%x ", edst[i] & 0xff); 16435795Skarels printf("\n"); 16535795Skarels ENDDEBUG 16635795Skarels } goto gottype; 16735795Skarels #endif ISO 16841546Smckusick #ifdef RMP 16941546Smckusick case AF_RMP: 17041546Smckusick /* 17141546Smckusick * This is IEEE 802.3 -- the Ethernet `type' field is 17241546Smckusick * really a `length' field. 17341546Smckusick */ 17441546Smckusick type = m->m_len; 17541546Smckusick bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst)); 17641546Smckusick break; 17741546Smckusick #endif 17841546Smckusick 17935379Skfall case AF_UNSPEC: 18035379Skfall eh = (struct ether_header *)dst->sa_data; 18135379Skfall bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst)); 18235379Skfall type = eh->ether_type; 18335379Skfall goto gottype; 18435379Skfall 18535379Skfall default: 18635379Skfall printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit, 18735379Skfall dst->sa_family); 18835379Skfall error = EAFNOSUPPORT; 18935379Skfall goto bad; 19035379Skfall } 19135379Skfall 19235379Skfall gottrailertype: 19335379Skfall /* 19435379Skfall * Packet to be sent as trailer: move first packet 19535379Skfall * (control information) to end of chain. 19635379Skfall */ 19735379Skfall while (m->m_next) 19835379Skfall m = m->m_next; 19935379Skfall m->m_next = m0; 20035379Skfall m = m0->m_next; 20135379Skfall m0->m_next = 0; 20235379Skfall 20335379Skfall gottype: 20435379Skfall /* 20535379Skfall * Add local net header. If no space in first mbuf, 20635379Skfall * allocate another. 20735379Skfall */ 20835379Skfall M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT); 20935379Skfall if (m == 0) { 21035379Skfall error = ENOBUFS; 21135379Skfall goto bad; 21235379Skfall } 21335379Skfall eh = mtod(m, struct ether_header *); 21435795Skarels type = htons((u_short)type); 21535795Skarels bcopy((caddr_t)&type,(caddr_t)&eh->ether_type, 21635795Skarels sizeof(eh->ether_type)); 21735379Skfall bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst)); 21835379Skfall bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost, 21935379Skfall sizeof(eh->ether_shost)); 22035379Skfall /* 22135379Skfall * Queue message on interface, and start output if interface 22235379Skfall * not yet active. 22335379Skfall */ 22435379Skfall s = splimp(); 22535379Skfall if (IF_QFULL(&ifp->if_snd)) { 22635379Skfall IF_DROP(&ifp->if_snd); 22735379Skfall splx(s); 22835795Skarels error = ENOBUFS; 22935795Skarels goto bad; 23035379Skfall } 23135379Skfall IF_ENQUEUE(&ifp->if_snd, m); 23235379Skfall if ((ifp->if_flags & IFF_OACTIVE) == 0) 23338845Sroot (*ifp->if_start)(ifp); 23435379Skfall splx(s); 23535795Skarels if (mcopy) 23635795Skarels (void) looutput(&loif, mcopy, dst); 23739187Ssklower ifp->if_obytes += len + sizeof (struct ether_header); 23839187Ssklower if (edst[0] & 1) 23939187Ssklower ifp->if_omcasts++; 24035795Skarels return (error); 24135379Skfall 24235379Skfall bad: 24335795Skarels if (mcopy) 24435795Skarels m_freem(mcopy); 24535795Skarels if (m) 24635795Skarels m_freem(m); 24735379Skfall return (error); 24835379Skfall } 24935379Skfall 25035379Skfall /* 25138845Sroot * Process a received Ethernet packet; 25238845Sroot * the packet is in the mbuf chain m without 25338845Sroot * the ether header, which is provided separately. 25435379Skfall */ 25537472Ssklower ether_input(ifp, eh, m) 25635379Skfall struct ifnet *ifp; 25735379Skfall register struct ether_header *eh; 25835379Skfall struct mbuf *m; 25935379Skfall { 26035379Skfall register struct ifqueue *inq; 26135795Skarels register struct llc *l; 26235379Skfall int s; 26335379Skfall 26439187Ssklower ifp->if_lastchange = time; 26539187Ssklower ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh); 26635795Skarels if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost, 26735379Skfall sizeof(etherbroadcastaddr)) == 0) 26835379Skfall m->m_flags |= M_BCAST; 26935795Skarels else if (eh->ether_dhost[0] & 1) 27035795Skarels m->m_flags |= M_MCAST; 27139187Ssklower if (m->m_flags & (M_BCAST|M_MCAST)) 27239187Ssklower ifp->if_imcasts++; 27335379Skfall 27435379Skfall switch (eh->ether_type) { 27535379Skfall #ifdef INET 27635379Skfall case ETHERTYPE_IP: 27735379Skfall schednetisr(NETISR_IP); 27835379Skfall inq = &ipintrq; 27935379Skfall break; 28035379Skfall 28135379Skfall case ETHERTYPE_ARP: 28235379Skfall arpinput((struct arpcom *)ifp, m); 28335379Skfall return; 28435379Skfall #endif 28535379Skfall #ifdef NS 28635379Skfall case ETHERTYPE_NS: 28735379Skfall schednetisr(NETISR_NS); 28835379Skfall inq = &nsintrq; 28935379Skfall break; 29035379Skfall 29135379Skfall #endif 29235379Skfall default: 29338845Sroot #ifdef ISO 29437472Ssklower if (eh->ether_type > ETHERMTU) 29535795Skarels goto dropanyway; 29635795Skarels l = mtod(m, struct llc *); 29735795Skarels switch (l->llc_control) { 29835795Skarels case LLC_UI: 29935795Skarels /* LLC_UI_P forbidden in class 1 service */ 30035795Skarels if ((l->llc_dsap == LLC_ISO_LSAP) && 30135795Skarels (l->llc_ssap == LLC_ISO_LSAP)) { 30235795Skarels /* LSAP for ISO */ 30338845Sroot m->m_data += 3; /* XXX */ 30438845Sroot m->m_len -= 3; /* XXX */ 30538845Sroot m->m_pkthdr.len -= 3; /* XXX */ 30637472Ssklower M_PREPEND(m, sizeof *eh, M_DONTWAIT); 30737472Ssklower if (m == 0) 30837472Ssklower return; 30937472Ssklower *mtod(m, struct ether_header *) = *eh; 31037472Ssklower IFDEBUG(D_ETHER) 31137472Ssklower printf("clnp packet"); 31237472Ssklower ENDDEBUG 31337472Ssklower schednetisr(NETISR_ISO); 31435795Skarels inq = &clnlintrq; 31538845Sroot break; 31635795Skarels } 31738845Sroot goto dropanyway; 31838845Sroot 31935795Skarels case LLC_XID: 32035795Skarels case LLC_XID_P: 32135795Skarels if(m->m_len < 6) 32235795Skarels goto dropanyway; 32335795Skarels l->llc_window = 0; 32435795Skarels l->llc_fid = 9; 32535795Skarels l->llc_class = 1; 32635795Skarels l->llc_dsap = l->llc_ssap = 0; 32735795Skarels /* Fall through to */ 32835795Skarels case LLC_TEST: 32935795Skarels case LLC_TEST_P: 33035795Skarels { 33135795Skarels struct sockaddr sa; 33235795Skarels register struct ether_header *eh2; 33335795Skarels int i; 33435795Skarels u_char c = l->llc_dsap; 33535795Skarels l->llc_dsap = l->llc_ssap; 33635795Skarels l->llc_ssap = c; 33737472Ssklower if (m->m_flags & (M_BCAST | M_MCAST)) 33837472Ssklower bcopy((caddr_t)ac->ac_enaddr, 33937472Ssklower (caddr_t)eh->ether_dhost, 6); 34035795Skarels sa.sa_family = AF_UNSPEC; 34137472Ssklower sa.sa_len = sizeof(sa); 34235795Skarels eh2 = (struct ether_header *)sa.sa_data; 34335795Skarels for (i = 0; i < 6; i++) { 34435795Skarels eh2->ether_shost[i] = c = eh->ether_dhost[i]; 34535795Skarels eh2->ether_dhost[i] = 34635795Skarels eh->ether_dhost[i] = eh->ether_shost[i]; 34735795Skarels eh->ether_shost[i] = c; 34835795Skarels } 34935795Skarels ifp->if_output(ifp, m, &sa); 35035795Skarels return; 35135795Skarels } 35235795Skarels dropanyway: 35335795Skarels default: 35435795Skarels m_freem(m); 35535795Skarels return; 35635795Skarels } 35738845Sroot #else 35838845Sroot m_freem(m); 35938845Sroot return; 36038845Sroot #endif ISO 36135379Skfall } 36235379Skfall 36335379Skfall s = splimp(); 36435379Skfall if (IF_QFULL(inq)) { 36535379Skfall IF_DROP(inq); 36635379Skfall m_freem(m); 36735379Skfall } else 36835379Skfall IF_ENQUEUE(inq, m); 36935379Skfall splx(s); 37035379Skfall } 37135379Skfall 37235379Skfall /* 37335379Skfall * Convert Ethernet address to printable (loggable) representation. 37435379Skfall */ 37537472Ssklower static char digits[] = "0123456789abcdef"; 37635379Skfall char * 37735379Skfall ether_sprintf(ap) 37835379Skfall register u_char *ap; 37935379Skfall { 38035379Skfall register i; 38135379Skfall static char etherbuf[18]; 38235379Skfall register char *cp = etherbuf; 38335379Skfall 38435379Skfall for (i = 0; i < 6; i++) { 38535379Skfall *cp++ = digits[*ap >> 4]; 38635379Skfall *cp++ = digits[*ap++ & 0xf]; 38735379Skfall *cp++ = ':'; 38835379Skfall } 38935379Skfall *--cp = 0; 39035379Skfall return (etherbuf); 39135379Skfall } 392*43336Ssklower iso_etherout() {} 393