if_arcsubr.c revision 271867
189099Sfjoe/* $NetBSD: if_arcsubr.c,v 1.36 2001/06/14 05:44:23 itojun Exp $ */ 289099Sfjoe/* $FreeBSD: head/sys/net/if_arcsubr.c 271867 2014-09-19 10:39:58Z glebius $ */ 389099Sfjoe 4139823Simp/*- 589099Sfjoe * Copyright (c) 1994, 1995 Ignatios Souvatzis 689099Sfjoe * Copyright (c) 1982, 1989, 1993 789099Sfjoe * The Regents of the University of California. All rights reserved. 889099Sfjoe * 989099Sfjoe * Redistribution and use in source and binary forms, with or without 1089099Sfjoe * modification, are permitted provided that the following conditions 1189099Sfjoe * are met: 1289099Sfjoe * 1. Redistributions of source code must retain the above copyright 1389099Sfjoe * notice, this list of conditions and the following disclaimer. 1489099Sfjoe * 2. Redistributions in binary form must reproduce the above copyright 1589099Sfjoe * notice, this list of conditions and the following disclaimer in the 1689099Sfjoe * documentation and/or other materials provided with the distribution. 1789099Sfjoe * 3. All advertising materials mentioning features or use of this software 1889099Sfjoe * must display the following acknowledgement: 1989099Sfjoe * This product includes software developed by the University of 2089099Sfjoe * California, Berkeley and its contributors. 2189099Sfjoe * 4. Neither the name of the University nor the names of its contributors 2289099Sfjoe * may be used to endorse or promote products derived from this software 2389099Sfjoe * without specific prior written permission. 2489099Sfjoe * 2589099Sfjoe * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2689099Sfjoe * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2789099Sfjoe * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2889099Sfjoe * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2989099Sfjoe * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3089099Sfjoe * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3189099Sfjoe * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3289099Sfjoe * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3389099Sfjoe * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3489099Sfjoe * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3589099Sfjoe * SUCH DAMAGE. 3689099Sfjoe * 3789099Sfjoe * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp 3889099Sfjoe * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93 3989099Sfjoe * 4089099Sfjoe */ 4189099Sfjoe#include "opt_inet.h" 4289099Sfjoe#include "opt_inet6.h" 4389099Sfjoe 4489099Sfjoe#include <sys/param.h> 4589099Sfjoe#include <sys/systm.h> 4689099Sfjoe#include <sys/kernel.h> 47129880Sphk#include <sys/module.h> 4889099Sfjoe#include <sys/malloc.h> 4989099Sfjoe#include <sys/mbuf.h> 5089099Sfjoe#include <sys/protosw.h> 5189099Sfjoe#include <sys/socket.h> 5289099Sfjoe#include <sys/sockio.h> 5389099Sfjoe#include <sys/errno.h> 5489099Sfjoe#include <sys/syslog.h> 5589099Sfjoe 5689099Sfjoe#include <machine/cpu.h> 5789099Sfjoe 5889099Sfjoe#include <net/if.h> 59257176Sglebius#include <net/if_var.h> 6089099Sfjoe#include <net/netisr.h> 6189099Sfjoe#include <net/route.h> 6289099Sfjoe#include <net/if_dl.h> 6389099Sfjoe#include <net/if_types.h> 6489099Sfjoe#include <net/if_arc.h> 6589099Sfjoe#include <net/if_arp.h> 6689099Sfjoe#include <net/bpf.h> 67186119Sqingli#include <net/if_llatbl.h> 6889099Sfjoe 6989099Sfjoe#if defined(INET) || defined(INET6) 7089099Sfjoe#include <netinet/in.h> 7189099Sfjoe#include <netinet/in_var.h> 7289099Sfjoe#include <netinet/if_ether.h> 7389099Sfjoe#endif 7489099Sfjoe 7589099Sfjoe#ifdef INET6 7689099Sfjoe#include <netinet6/nd6.h> 7789099Sfjoe#endif 7889099Sfjoe 7989099Sfjoe#define ARCNET_ALLOW_BROKEN_ARP 8089099Sfjoe 8192725Salfredstatic struct mbuf *arc_defrag(struct ifnet *, struct mbuf *); 82109771Sfjoestatic int arc_resolvemulti(struct ifnet *, struct sockaddr **, 83109771Sfjoe struct sockaddr *); 8489099Sfjoe 8589099Sfjoeu_int8_t arcbroadcastaddr = 0; 8689099Sfjoe 87110106Sfjoe#define ARC_LLADDR(ifp) (*(u_int8_t *)IF_LLADDR(ifp)) 88110106Sfjoe 8989099Sfjoe#define senderr(e) { error = (e); goto bad;} 90249925Sglebius#define SIN(s) ((const struct sockaddr_in *)(s)) 9189099Sfjoe 9289099Sfjoe/* 9389099Sfjoe * ARCnet output routine. 9489099Sfjoe * Encapsulate a packet of type family for the local net. 9589099Sfjoe * Assumes that ifp is actually pointer to arccom structure. 9689099Sfjoe */ 9789099Sfjoeint 98249925Sglebiusarc_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst, 99191148Skmacy struct route *ro) 10089099Sfjoe{ 10189099Sfjoe struct arc_header *ah; 10289099Sfjoe int error; 10389099Sfjoe u_int8_t atype, adst; 104109771Sfjoe int loop_copy = 0; 105110106Sfjoe int isphds; 106193891Sbz#if defined(INET) || defined(INET6) 107186119Sqingli struct llentry *lle; 108193891Sbz#endif 10989099Sfjoe 110148887Srwatson if (!((ifp->if_flags & IFF_UP) && 111148887Srwatson (ifp->if_drv_flags & IFF_DRV_RUNNING))) 11289099Sfjoe return(ENETDOWN); /* m, m1 aren't initialized yet */ 11389099Sfjoe 11489099Sfjoe error = 0; 11589099Sfjoe 11689099Sfjoe switch (dst->sa_family) { 11789099Sfjoe#ifdef INET 11889099Sfjoe case AF_INET: 11989099Sfjoe 12089099Sfjoe /* 12189099Sfjoe * For now, use the simple IP addr -> ARCnet addr mapping 12289099Sfjoe */ 12389099Sfjoe if (m->m_flags & (M_BCAST|M_MCAST)) 12489099Sfjoe adst = arcbroadcastaddr; /* ARCnet broadcast address */ 12589099Sfjoe else if (ifp->if_flags & IFF_NOARP) 12689099Sfjoe adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF; 127128636Sluigi else { 128191148Skmacy error = arpresolve(ifp, ro ? ro->ro_rt : NULL, 129191148Skmacy m, dst, &adst, &lle); 130128636Sluigi if (error) 131128636Sluigi return (error == EWOULDBLOCK ? 0 : error); 132128636Sluigi } 13389099Sfjoe 13489099Sfjoe atype = (ifp->if_flags & IFF_LINK0) ? 13589099Sfjoe ARCTYPE_IP_OLD : ARCTYPE_IP; 13689099Sfjoe break; 137127260Smdodd case AF_ARP: 138127260Smdodd { 139127260Smdodd struct arphdr *ah; 140127260Smdodd ah = mtod(m, struct arphdr *); 141127260Smdodd ah->ar_hrd = htons(ARPHRD_ARCNET); 142127260Smdodd 143127260Smdodd loop_copy = -1; /* if this is for us, don't do it */ 144127260Smdodd 145127260Smdodd switch(ntohs(ah->ar_op)) { 146127260Smdodd case ARPOP_REVREQUEST: 147127260Smdodd case ARPOP_REVREPLY: 148127275Smdodd atype = ARCTYPE_REVARP; 149127260Smdodd break; 150127260Smdodd case ARPOP_REQUEST: 151127260Smdodd case ARPOP_REPLY: 152127260Smdodd default: 153127275Smdodd atype = ARCTYPE_ARP; 154127260Smdodd break; 155127260Smdodd } 156127260Smdodd 157127260Smdodd if (m->m_flags & M_BCAST) 158127290Smdodd bcopy(ifp->if_broadcastaddr, &adst, ARC_ADDR_LEN); 159127260Smdodd else 160127290Smdodd bcopy(ar_tha(ah), &adst, ARC_ADDR_LEN); 161127260Smdodd 162127260Smdodd } 163127260Smdodd break; 16489099Sfjoe#endif 16589099Sfjoe#ifdef INET6 16689099Sfjoe case AF_INET6: 167186217Sqingli error = nd6_storelladdr(ifp, m, dst, (u_char *)&adst, &lle); 168128636Sluigi if (error) 169128636Sluigi return (error); 17089099Sfjoe atype = ARCTYPE_INET6; 17189099Sfjoe break; 17289099Sfjoe#endif 17389099Sfjoe case AF_UNSPEC: 174249925Sglebius { 175249925Sglebius const struct arc_header *ah; 176249925Sglebius 177109771Sfjoe loop_copy = -1; 178249925Sglebius ah = (const struct arc_header *)dst->sa_data; 17989099Sfjoe adst = ah->arc_dhost; 18089099Sfjoe atype = ah->arc_type; 18189099Sfjoe 18289099Sfjoe if (atype == ARCTYPE_ARP) { 18389099Sfjoe atype = (ifp->if_flags & IFF_LINK0) ? 18489099Sfjoe ARCTYPE_ARP_OLD: ARCTYPE_ARP; 18589099Sfjoe 18689099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP 18789099Sfjoe /* 18889099Sfjoe * XXX It's not clear per RFC826 if this is needed, but 18989099Sfjoe * "assigned numbers" say this is wrong. 19089099Sfjoe * However, e.g., AmiTCP 3.0Beta used it... we make this 19189099Sfjoe * switchable for emergency cases. Not perfect, but... 19289099Sfjoe */ 19389099Sfjoe if (ifp->if_flags & IFF_LINK2) 194109771Sfjoe mtod(m, struct arphdr *)->ar_pro = atype - 1; 19589099Sfjoe#endif 19689099Sfjoe } 19789099Sfjoe break; 198249925Sglebius } 19989099Sfjoe default: 200105598Sbrooks if_printf(ifp, "can't handle af%d\n", dst->sa_family); 20189099Sfjoe senderr(EAFNOSUPPORT); 20289099Sfjoe } 20389099Sfjoe 204110106Sfjoe isphds = arc_isphds(atype); 205243882Sglebius M_PREPEND(m, isphds ? ARC_HDRNEWLEN : ARC_HDRLEN, M_NOWAIT); 20689099Sfjoe if (m == 0) 20789099Sfjoe senderr(ENOBUFS); 20889099Sfjoe ah = mtod(m, struct arc_header *); 20989099Sfjoe ah->arc_type = atype; 21089099Sfjoe ah->arc_dhost = adst; 211110106Sfjoe ah->arc_shost = ARC_LLADDR(ifp); 212110106Sfjoe if (isphds) { 213110106Sfjoe ah->arc_flag = 0; 214110106Sfjoe ah->arc_seqid = 0; 215110106Sfjoe } 21689099Sfjoe 217109771Sfjoe if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) { 218109771Sfjoe if ((m->m_flags & M_BCAST) || (loop_copy > 0)) { 219109771Sfjoe struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 220109771Sfjoe 221109771Sfjoe (void) if_simloop(ifp, n, dst->sa_family, ARC_HDRLEN); 222109771Sfjoe } else if (ah->arc_dhost == ah->arc_shost) { 223109771Sfjoe (void) if_simloop(ifp, m, dst->sa_family, ARC_HDRLEN); 224109771Sfjoe return (0); /* XXX */ 225109771Sfjoe } 226109771Sfjoe } 227109771Sfjoe 228106939Ssam BPF_MTAP(ifp, m); 22989099Sfjoe 230191605Skmacy error = ifp->if_transmit(ifp, m); 23189099Sfjoe 23289099Sfjoe return (error); 23389099Sfjoe 23489099Sfjoebad: 23589099Sfjoe if (m) 23689099Sfjoe m_freem(m); 23789099Sfjoe return (error); 23889099Sfjoe} 23989099Sfjoe 24089099Sfjoevoid 241157681Srwatsonarc_frag_init(struct ifnet *ifp) 24289099Sfjoe{ 24389099Sfjoe struct arccom *ac; 24489099Sfjoe 245147256Sbrooks ac = (struct arccom *)ifp->if_l2com; 24689099Sfjoe ac->curr_frag = 0; 24789099Sfjoe} 24889099Sfjoe 24989099Sfjoestruct mbuf * 250157681Srwatsonarc_frag_next(struct ifnet *ifp) 25189099Sfjoe{ 25289099Sfjoe struct arccom *ac; 25389099Sfjoe struct mbuf *m; 25489099Sfjoe struct arc_header *ah; 25589099Sfjoe 256147256Sbrooks ac = (struct arccom *)ifp->if_l2com; 25789099Sfjoe if ((m = ac->curr_frag) == 0) { 25889099Sfjoe int tfrags; 25989099Sfjoe 26089099Sfjoe /* dequeue new packet */ 26189099Sfjoe IF_DEQUEUE(&ifp->if_snd, m); 26289099Sfjoe if (m == 0) 26389099Sfjoe return 0; 26489099Sfjoe 26589099Sfjoe ah = mtod(m, struct arc_header *); 26689099Sfjoe if (!arc_isphds(ah->arc_type)) 26789099Sfjoe return m; 26889099Sfjoe 26989099Sfjoe ++ac->ac_seqid; /* make the seqid unique */ 270109771Sfjoe tfrags = (m->m_pkthdr.len + ARC_MAX_DATA - 1) / ARC_MAX_DATA; 27189099Sfjoe ac->fsflag = 2 * tfrags - 3; 27289099Sfjoe ac->sflag = 0; 27389099Sfjoe ac->rsflag = ac->fsflag; 27489099Sfjoe ac->arc_dhost = ah->arc_dhost; 27589099Sfjoe ac->arc_shost = ah->arc_shost; 27689099Sfjoe ac->arc_type = ah->arc_type; 27789099Sfjoe 278110106Sfjoe m_adj(m, ARC_HDRNEWLEN); 27989099Sfjoe ac->curr_frag = m; 28089099Sfjoe } 28189099Sfjoe 28289099Sfjoe /* split out next fragment and return it */ 28389099Sfjoe if (ac->sflag < ac->fsflag) { 28489099Sfjoe /* we CAN'T have short packets here */ 285243882Sglebius ac->curr_frag = m_split(m, ARC_MAX_DATA, M_NOWAIT); 28689099Sfjoe if (ac->curr_frag == 0) { 28793750Sluigi m_freem(m); 28889099Sfjoe return 0; 28989099Sfjoe } 29089099Sfjoe 291243882Sglebius M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT); 29289099Sfjoe if (m == 0) { 29393750Sluigi m_freem(ac->curr_frag); 29489099Sfjoe ac->curr_frag = 0; 29589099Sfjoe return 0; 29689099Sfjoe } 29789099Sfjoe 29889099Sfjoe ah = mtod(m, struct arc_header *); 29989099Sfjoe ah->arc_flag = ac->rsflag; 30089099Sfjoe ah->arc_seqid = ac->ac_seqid; 30189099Sfjoe 30289099Sfjoe ac->sflag += 2; 30389099Sfjoe ac->rsflag = ac->sflag; 30489099Sfjoe } else if ((m->m_pkthdr.len >= 30589099Sfjoe ARC_MIN_FORBID_LEN - ARC_HDRNEWLEN + 2) && 30689099Sfjoe (m->m_pkthdr.len <= 30789099Sfjoe ARC_MAX_FORBID_LEN - ARC_HDRNEWLEN + 2)) { 30889099Sfjoe ac->curr_frag = 0; 30989099Sfjoe 310243882Sglebius M_PREPEND(m, ARC_HDRNEWLEN_EXC, M_NOWAIT); 31189099Sfjoe if (m == 0) 31289099Sfjoe return 0; 31389099Sfjoe 31489099Sfjoe ah = mtod(m, struct arc_header *); 31589099Sfjoe ah->arc_flag = 0xFF; 31689099Sfjoe ah->arc_seqid = 0xFFFF; 31789099Sfjoe ah->arc_type2 = ac->arc_type; 31889099Sfjoe ah->arc_flag2 = ac->sflag; 31989099Sfjoe ah->arc_seqid2 = ac->ac_seqid; 32089099Sfjoe } else { 32189099Sfjoe ac->curr_frag = 0; 32289099Sfjoe 323243882Sglebius M_PREPEND(m, ARC_HDRNEWLEN, M_NOWAIT); 32489099Sfjoe if (m == 0) 32589099Sfjoe return 0; 32689099Sfjoe 32789099Sfjoe ah = mtod(m, struct arc_header *); 32889099Sfjoe ah->arc_flag = ac->sflag; 32989099Sfjoe ah->arc_seqid = ac->ac_seqid; 33089099Sfjoe } 33189099Sfjoe 33289099Sfjoe ah->arc_dhost = ac->arc_dhost; 33389099Sfjoe ah->arc_shost = ac->arc_shost; 33489099Sfjoe ah->arc_type = ac->arc_type; 33589099Sfjoe 33689099Sfjoe return m; 33789099Sfjoe} 33889099Sfjoe 33989099Sfjoe/* 34089099Sfjoe * Defragmenter. Returns mbuf if last packet found, else 34189099Sfjoe * NULL. frees imcoming mbuf as necessary. 34289099Sfjoe */ 34389099Sfjoe 344105228Sphkstatic __inline struct mbuf * 345157681Srwatsonarc_defrag(struct ifnet *ifp, struct mbuf *m) 34689099Sfjoe{ 34789099Sfjoe struct arc_header *ah, *ah1; 34889099Sfjoe struct arccom *ac; 34989099Sfjoe struct ac_frag *af; 35089099Sfjoe struct mbuf *m1; 35189099Sfjoe char *s; 35289099Sfjoe int newflen; 35389099Sfjoe u_char src,dst,typ; 35489099Sfjoe 355147256Sbrooks ac = (struct arccom *)ifp->if_l2com; 35689099Sfjoe 35789099Sfjoe if (m->m_len < ARC_HDRNEWLEN) { 35889099Sfjoe m = m_pullup(m, ARC_HDRNEWLEN); 35989099Sfjoe if (m == NULL) { 360271867Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 36189099Sfjoe return NULL; 36289099Sfjoe } 36389099Sfjoe } 36489099Sfjoe 36589099Sfjoe ah = mtod(m, struct arc_header *); 36689099Sfjoe typ = ah->arc_type; 36789099Sfjoe 36889099Sfjoe if (!arc_isphds(typ)) 36989099Sfjoe return m; 37089099Sfjoe 37189099Sfjoe src = ah->arc_shost; 37289099Sfjoe dst = ah->arc_dhost; 37389099Sfjoe 37489099Sfjoe if (ah->arc_flag == 0xff) { 37589099Sfjoe m_adj(m, 4); 37689099Sfjoe 37789099Sfjoe if (m->m_len < ARC_HDRNEWLEN) { 37889099Sfjoe m = m_pullup(m, ARC_HDRNEWLEN); 37989099Sfjoe if (m == NULL) { 380271867Sglebius if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 38189099Sfjoe return NULL; 38289099Sfjoe } 38389099Sfjoe } 38489099Sfjoe 38589099Sfjoe ah = mtod(m, struct arc_header *); 38689099Sfjoe } 38789099Sfjoe 38889099Sfjoe af = &ac->ac_fragtab[src]; 38989099Sfjoe m1 = af->af_packet; 39089099Sfjoe s = "debug code error"; 39189099Sfjoe 39289099Sfjoe if (ah->arc_flag & 1) { 39389099Sfjoe /* 39489099Sfjoe * first fragment. We always initialize, which is 39589099Sfjoe * about the right thing to do, as we only want to 39689099Sfjoe * accept one fragmented packet per src at a time. 39789099Sfjoe */ 39889099Sfjoe if (m1 != NULL) 39989099Sfjoe m_freem(m1); 40089099Sfjoe 40189099Sfjoe af->af_packet = m; 40289099Sfjoe m1 = m; 40389099Sfjoe af->af_maxflag = ah->arc_flag; 40489099Sfjoe af->af_lastseen = 0; 40589099Sfjoe af->af_seqid = ah->arc_seqid; 40689099Sfjoe 40789099Sfjoe return NULL; 40889099Sfjoe /* notreached */ 40989099Sfjoe } else { 41089099Sfjoe /* check for unfragmented packet */ 41189099Sfjoe if (ah->arc_flag == 0) 41289099Sfjoe return m; 41389099Sfjoe 41489099Sfjoe /* do we have a first packet from that src? */ 41589099Sfjoe if (m1 == NULL) { 41689099Sfjoe s = "no first frag"; 41789099Sfjoe goto outofseq; 41889099Sfjoe } 41989099Sfjoe 42089099Sfjoe ah1 = mtod(m1, struct arc_header *); 42189099Sfjoe 42289099Sfjoe if (ah->arc_seqid != ah1->arc_seqid) { 42389099Sfjoe s = "seqid differs"; 42489099Sfjoe goto outofseq; 42589099Sfjoe } 42689099Sfjoe 42789099Sfjoe if (typ != ah1->arc_type) { 42889099Sfjoe s = "type differs"; 42989099Sfjoe goto outofseq; 43089099Sfjoe } 43189099Sfjoe 43289099Sfjoe if (dst != ah1->arc_dhost) { 43389099Sfjoe s = "dest host differs"; 43489099Sfjoe goto outofseq; 43589099Sfjoe } 43689099Sfjoe 43789099Sfjoe /* typ, seqid and dst are ok here. */ 43889099Sfjoe 43989099Sfjoe if (ah->arc_flag == af->af_lastseen) { 44089099Sfjoe m_freem(m); 44189099Sfjoe return NULL; 44289099Sfjoe } 44389099Sfjoe 44489099Sfjoe if (ah->arc_flag == af->af_lastseen + 2) { 44589099Sfjoe /* ok, this is next fragment */ 44689099Sfjoe af->af_lastseen = ah->arc_flag; 44789099Sfjoe m_adj(m,ARC_HDRNEWLEN); 44889099Sfjoe 44989099Sfjoe /* 45089099Sfjoe * m_cat might free the first mbuf (with pkthdr) 45189099Sfjoe * in 2nd chain; therefore: 45289099Sfjoe */ 45389099Sfjoe 45489099Sfjoe newflen = m->m_pkthdr.len; 45589099Sfjoe 45689099Sfjoe m_cat(m1,m); 45789099Sfjoe 45889099Sfjoe m1->m_pkthdr.len += newflen; 45989099Sfjoe 46089099Sfjoe /* is it the last one? */ 46189099Sfjoe if (af->af_lastseen > af->af_maxflag) { 46289099Sfjoe af->af_packet = NULL; 46389099Sfjoe return(m1); 46489099Sfjoe } else 46589099Sfjoe return NULL; 46689099Sfjoe } 46789099Sfjoe s = "other reason"; 46889099Sfjoe /* if all else fails, it is out of sequence, too */ 46989099Sfjoe } 47089099Sfjoeoutofseq: 47189099Sfjoe if (m1) { 47289099Sfjoe m_freem(m1); 47389099Sfjoe af->af_packet = NULL; 47489099Sfjoe } 47589099Sfjoe 47689099Sfjoe if (m) 47789099Sfjoe m_freem(m); 47889099Sfjoe 479121816Sbrooks log(LOG_INFO,"%s: got out of seq. packet: %s\n", 480121816Sbrooks ifp->if_xname, s); 48189099Sfjoe 48289099Sfjoe return NULL; 48389099Sfjoe} 48489099Sfjoe 48589099Sfjoe/* 48689099Sfjoe * return 1 if Packet Header Definition Standard, else 0. 48789099Sfjoe * For now: old IP, old ARP aren't obviously. Lacking correct information, 48889099Sfjoe * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS. 48989099Sfjoe * (Apple and Novell corporations were involved, among others, in PHDS work). 49089099Sfjoe * Easiest is to assume that everybody else uses that, too. 49189099Sfjoe */ 49289099Sfjoeint 493157681Srwatsonarc_isphds(u_int8_t type) 49489099Sfjoe{ 49589099Sfjoe return (type != ARCTYPE_IP_OLD && 49689099Sfjoe type != ARCTYPE_ARP_OLD && 49789099Sfjoe type != ARCTYPE_DIAGNOSE); 49889099Sfjoe} 49989099Sfjoe 50089099Sfjoe/* 50189099Sfjoe * Process a received Arcnet packet; 50289099Sfjoe * the packet is in the mbuf chain m with 50389099Sfjoe * the ARCnet header. 50489099Sfjoe */ 50589099Sfjoevoid 506157681Srwatsonarc_input(struct ifnet *ifp, struct mbuf *m) 50789099Sfjoe{ 50889099Sfjoe struct arc_header *ah; 509111888Sjlemon int isr; 51089099Sfjoe u_int8_t atype; 51189099Sfjoe 51289099Sfjoe if ((ifp->if_flags & IFF_UP) == 0) { 51389099Sfjoe m_freem(m); 51489099Sfjoe return; 51589099Sfjoe } 51689099Sfjoe 51789099Sfjoe /* possibly defragment: */ 51889099Sfjoe m = arc_defrag(ifp, m); 51989099Sfjoe if (m == NULL) 52089099Sfjoe return; 52189099Sfjoe 522106939Ssam BPF_MTAP(ifp, m); 52389099Sfjoe 52489099Sfjoe ah = mtod(m, struct arc_header *); 525109771Sfjoe /* does this belong to us? */ 526110106Sfjoe if ((ifp->if_flags & IFF_PROMISC) == 0 527109771Sfjoe && ah->arc_dhost != arcbroadcastaddr 528110106Sfjoe && ah->arc_dhost != ARC_LLADDR(ifp)) { 529109771Sfjoe m_freem(m); 530109771Sfjoe return; 531109771Sfjoe } 53289099Sfjoe 533271867Sglebius if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len); 53489099Sfjoe 535109771Sfjoe if (ah->arc_dhost == arcbroadcastaddr) { 53689099Sfjoe m->m_flags |= M_BCAST|M_MCAST; 537271867Sglebius if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1); 53889099Sfjoe } 53989099Sfjoe 54089099Sfjoe atype = ah->arc_type; 54189099Sfjoe switch (atype) { 54289099Sfjoe#ifdef INET 54389099Sfjoe case ARCTYPE_IP: 54489099Sfjoe m_adj(m, ARC_HDRNEWLEN); 545154518Sandre if ((m = ip_fastforward(m)) == NULL) 546109771Sfjoe return; 547111888Sjlemon isr = NETISR_IP; 54889099Sfjoe break; 54989099Sfjoe 55089099Sfjoe case ARCTYPE_IP_OLD: 55189099Sfjoe m_adj(m, ARC_HDRLEN); 552154518Sandre if ((m = ip_fastforward(m)) == NULL) 553109771Sfjoe return; 554111888Sjlemon isr = NETISR_IP; 55589099Sfjoe break; 55689099Sfjoe 55789099Sfjoe case ARCTYPE_ARP: 55889099Sfjoe if (ifp->if_flags & IFF_NOARP) { 55989099Sfjoe /* Discard packet if ARP is disabled on interface */ 56089099Sfjoe m_freem(m); 56189099Sfjoe return; 56289099Sfjoe } 56389099Sfjoe m_adj(m, ARC_HDRNEWLEN); 564111888Sjlemon isr = NETISR_ARP; 56589099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP 56689099Sfjoe mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 56789099Sfjoe#endif 56889099Sfjoe break; 56989099Sfjoe 57089099Sfjoe case ARCTYPE_ARP_OLD: 57189099Sfjoe if (ifp->if_flags & IFF_NOARP) { 57289099Sfjoe /* Discard packet if ARP is disabled on interface */ 57389099Sfjoe m_freem(m); 57489099Sfjoe return; 57589099Sfjoe } 57689099Sfjoe m_adj(m, ARC_HDRLEN); 577111888Sjlemon isr = NETISR_ARP; 57889099Sfjoe#ifdef ARCNET_ALLOW_BROKEN_ARP 57989099Sfjoe mtod(m, struct arphdr *)->ar_pro = htons(ETHERTYPE_IP); 58089099Sfjoe#endif 58189099Sfjoe break; 58289099Sfjoe#endif 58389099Sfjoe#ifdef INET6 58489099Sfjoe case ARCTYPE_INET6: 58589099Sfjoe m_adj(m, ARC_HDRNEWLEN); 586111888Sjlemon isr = NETISR_IPV6; 58789099Sfjoe break; 58889099Sfjoe#endif 58989099Sfjoe default: 59089099Sfjoe m_freem(m); 59189099Sfjoe return; 59289099Sfjoe } 593223741Sbz M_SETFIB(m, ifp->if_fib); 594111888Sjlemon netisr_dispatch(isr, m); 59589099Sfjoe} 59689099Sfjoe 59789099Sfjoe/* 59889099Sfjoe * Register (new) link level address. 59989099Sfjoe */ 60089099Sfjoevoid 601157681Srwatsonarc_storelladdr(struct ifnet *ifp, u_int8_t lla) 60289099Sfjoe{ 603110106Sfjoe ARC_LLADDR(ifp) = lla; 60489099Sfjoe} 60589099Sfjoe 60689099Sfjoe/* 60789099Sfjoe * Perform common duties while attaching to interface list 60889099Sfjoe */ 60989099Sfjoevoid 610157681Srwatsonarc_ifattach(struct ifnet *ifp, u_int8_t lla) 61189099Sfjoe{ 61289099Sfjoe struct ifaddr *ifa; 61389099Sfjoe struct sockaddr_dl *sdl; 61489099Sfjoe struct arccom *ac; 61589099Sfjoe 61689099Sfjoe if_attach(ifp); 61789099Sfjoe ifp->if_addrlen = 1; 61889099Sfjoe ifp->if_hdrlen = ARC_HDRLEN; 61989099Sfjoe ifp->if_mtu = 1500; 620109771Sfjoe ifp->if_resolvemulti = arc_resolvemulti; 62189099Sfjoe if (ifp->if_baudrate == 0) 62289099Sfjoe ifp->if_baudrate = 2500000; 623152315Sru ifa = ifp->if_addr; 624135577Sstefanf KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__)); 62589099Sfjoe sdl = (struct sockaddr_dl *)ifa->ifa_addr; 62689099Sfjoe sdl->sdl_type = IFT_ARCNET; 62789099Sfjoe sdl->sdl_alen = ifp->if_addrlen; 62889099Sfjoe 62989099Sfjoe if (ifp->if_flags & IFF_BROADCAST) 63089099Sfjoe ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI; 63189099Sfjoe 632147256Sbrooks ac = (struct arccom *)ifp->if_l2com; 63389099Sfjoe ac->ac_seqid = (time_second) & 0xFFFF; /* try to make seqid unique */ 63489099Sfjoe if (lla == 0) { 63589099Sfjoe /* XXX this message isn't entirely clear, to me -- cgd */ 636121816Sbrooks log(LOG_ERR,"%s: link address 0 reserved for broadcasts. Please change it and ifconfig %s down up\n", 637121816Sbrooks ifp->if_xname, ifp->if_xname); 63889099Sfjoe } 63989099Sfjoe arc_storelladdr(ifp, lla); 64089099Sfjoe 64189099Sfjoe ifp->if_broadcastaddr = &arcbroadcastaddr; 64289099Sfjoe 64389099Sfjoe bpfattach(ifp, DLT_ARCNET, ARC_HDRLEN); 64489099Sfjoe} 64589099Sfjoe 64689099Sfjoevoid 647157681Srwatsonarc_ifdetach(struct ifnet *ifp) 64889099Sfjoe{ 64989099Sfjoe bpfdetach(ifp); 65089099Sfjoe if_detach(ifp); 65189099Sfjoe} 65289099Sfjoe 65389099Sfjoeint 654194581Srdivackyarc_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 65589099Sfjoe{ 65689099Sfjoe struct ifaddr *ifa = (struct ifaddr *) data; 65789099Sfjoe struct ifreq *ifr = (struct ifreq *) data; 65889099Sfjoe int error = 0; 65989099Sfjoe 66089099Sfjoe switch (command) { 66189099Sfjoe case SIOCSIFADDR: 66289099Sfjoe ifp->if_flags |= IFF_UP; 66389099Sfjoe switch (ifa->ifa_addr->sa_family) { 66489099Sfjoe#ifdef INET 66589099Sfjoe case AF_INET: 66689099Sfjoe ifp->if_init(ifp->if_softc); /* before arpwhohas */ 66789099Sfjoe arp_ifinit(ifp, ifa); 66889099Sfjoe break; 66989099Sfjoe#endif 67089099Sfjoe default: 67189099Sfjoe ifp->if_init(ifp->if_softc); 67289099Sfjoe break; 67389099Sfjoe } 67489099Sfjoe break; 67589099Sfjoe 676109771Sfjoe case SIOCGIFADDR: 677109771Sfjoe { 678109771Sfjoe struct sockaddr *sa; 679109771Sfjoe 680109771Sfjoe sa = (struct sockaddr *) &ifr->ifr_data; 681110106Sfjoe *(u_int8_t *)sa->sa_data = ARC_LLADDR(ifp); 682109771Sfjoe } 683109771Sfjoe break; 684109771Sfjoe 68589099Sfjoe case SIOCADDMULTI: 68689099Sfjoe case SIOCDELMULTI: 68789099Sfjoe if (ifr == NULL) 68889099Sfjoe error = EAFNOSUPPORT; 68989099Sfjoe else { 69089099Sfjoe switch (ifr->ifr_addr.sa_family) { 69189099Sfjoe case AF_INET: 69289099Sfjoe case AF_INET6: 69389099Sfjoe error = 0; 69489099Sfjoe break; 69589099Sfjoe default: 69689099Sfjoe error = EAFNOSUPPORT; 69789099Sfjoe break; 69889099Sfjoe } 69989099Sfjoe } 70089099Sfjoe break; 70189099Sfjoe 70289099Sfjoe case SIOCSIFMTU: 70389099Sfjoe /* 70489099Sfjoe * Set the interface MTU. 70589099Sfjoe * mtu can't be larger than ARCMTU for RFC1051 70689099Sfjoe * and can't be larger than ARC_PHDS_MTU 70789099Sfjoe */ 70889099Sfjoe if (((ifp->if_flags & IFF_LINK0) && ifr->ifr_mtu > ARCMTU) || 70989099Sfjoe ifr->ifr_mtu > ARC_PHDS_MAXMTU) 71089099Sfjoe error = EINVAL; 71189099Sfjoe else 71289099Sfjoe ifp->if_mtu = ifr->ifr_mtu; 71389099Sfjoe break; 714109771Sfjoe } 71589099Sfjoe 716109771Sfjoe return (error); 717109771Sfjoe} 71889099Sfjoe 719109771Sfjoe/* based on ether_resolvemulti() */ 720109771Sfjoeint 721157681Srwatsonarc_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa, 722157681Srwatson struct sockaddr *sa) 723109771Sfjoe{ 724109771Sfjoe struct sockaddr_dl *sdl; 725184680Sbz#ifdef INET 726109771Sfjoe struct sockaddr_in *sin; 727184680Sbz#endif 728109771Sfjoe#ifdef INET6 729109771Sfjoe struct sockaddr_in6 *sin6; 730109771Sfjoe#endif 731109771Sfjoe 732109771Sfjoe switch(sa->sa_family) { 733109771Sfjoe case AF_LINK: 734109771Sfjoe /* 735109771Sfjoe * No mapping needed. Just check that it's a valid MC address. 736109771Sfjoe */ 737109771Sfjoe sdl = (struct sockaddr_dl *)sa; 738109771Sfjoe if (*LLADDR(sdl) != arcbroadcastaddr) 739109771Sfjoe return EADDRNOTAVAIL; 740109771Sfjoe *llsa = 0; 741109771Sfjoe return 0; 742109771Sfjoe#ifdef INET 743109771Sfjoe case AF_INET: 744109771Sfjoe sin = (struct sockaddr_in *)sa; 745109771Sfjoe if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) 746109771Sfjoe return EADDRNOTAVAIL; 747260870Smelifaro sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 748109771Sfjoe sdl->sdl_alen = ARC_ADDR_LEN; 749109771Sfjoe *LLADDR(sdl) = 0; 750109771Sfjoe *llsa = (struct sockaddr *)sdl; 751109771Sfjoe return 0; 752109771Sfjoe#endif 753109771Sfjoe#ifdef INET6 754109771Sfjoe case AF_INET6: 755109771Sfjoe sin6 = (struct sockaddr_in6 *)sa; 756109771Sfjoe if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 757109771Sfjoe /* 758109771Sfjoe * An IP6 address of 0 means listen to all 759109771Sfjoe * of the Ethernet multicast address used for IP6. 760109771Sfjoe * (This is used for multicast routers.) 761109771Sfjoe */ 762109771Sfjoe ifp->if_flags |= IFF_ALLMULTI; 763109771Sfjoe *llsa = 0; 764109771Sfjoe return 0; 76589099Sfjoe } 766109771Sfjoe if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) 767109771Sfjoe return EADDRNOTAVAIL; 768260870Smelifaro sdl = link_init_sdl(ifp, *llsa, IFT_ETHER); 769109771Sfjoe sdl->sdl_alen = ARC_ADDR_LEN; 770109771Sfjoe *LLADDR(sdl) = 0; 771109771Sfjoe *llsa = (struct sockaddr *)sdl; 772109771Sfjoe return 0; 77389099Sfjoe#endif 774109771Sfjoe 775109771Sfjoe default: 776109771Sfjoe /* 777109771Sfjoe * Well, the text isn't quite right, but it's the name 778109771Sfjoe * that counts... 779109771Sfjoe */ 780109771Sfjoe return EAFNOSUPPORT; 78189099Sfjoe } 78289099Sfjoe} 783147256Sbrooks 784227293Sedstatic MALLOC_DEFINE(M_ARCCOM, "arccom", "ARCNET interface internals"); 785147256Sbrooks 786147256Sbrooksstatic void* 787147256Sbrooksarc_alloc(u_char type, struct ifnet *ifp) 788147256Sbrooks{ 789147256Sbrooks struct arccom *ac; 790147256Sbrooks 791147256Sbrooks ac = malloc(sizeof(struct arccom), M_ARCCOM, M_WAITOK | M_ZERO); 792147256Sbrooks ac->ac_ifp = ifp; 793147256Sbrooks 794147256Sbrooks return (ac); 795147256Sbrooks} 796147256Sbrooks 797147256Sbrooksstatic void 798147256Sbrooksarc_free(void *com, u_char type) 799147256Sbrooks{ 800147256Sbrooks 801147256Sbrooks free(com, M_ARCCOM); 802147256Sbrooks} 803147256Sbrooks 804147256Sbrooksstatic int 805147256Sbrooksarc_modevent(module_t mod, int type, void *data) 806147256Sbrooks{ 807147256Sbrooks 808147256Sbrooks switch (type) { 809147256Sbrooks case MOD_LOAD: 810147256Sbrooks if_register_com_alloc(IFT_ARCNET, arc_alloc, arc_free); 811147256Sbrooks break; 812147256Sbrooks case MOD_UNLOAD: 813147256Sbrooks if_deregister_com_alloc(IFT_ARCNET); 814147256Sbrooks break; 815147256Sbrooks default: 816147256Sbrooks return EOPNOTSUPP; 817147256Sbrooks } 818147256Sbrooks 819147256Sbrooks return (0); 820147256Sbrooks} 821147256Sbrooks 822147256Sbrooksstatic moduledata_t arc_mod = { 823147256Sbrooks "arcnet", 824147256Sbrooks arc_modevent, 825241394Skevlo 0 826147256Sbrooks}; 827147256Sbrooks 828147256SbrooksDECLARE_MODULE(arcnet, arc_mod, SI_SUB_INIT_IF, SI_ORDER_ANY); 829147256SbrooksMODULE_VERSION(arcnet, 1); 830