Home | History | Annotate | Line # | Download | only in net
if_arcsubr.c revision 1.1
      1  1.1  glass /*	$NetBSD: if_arcsubr.c,v 1.1 1995/02/23 07:19:51 glass Exp $	*/
      2  1.1  glass 
      3  1.1  glass /*
      4  1.1  glass  * Copyright (c) 1994, 1995 Ignatios Souvatzis
      5  1.1  glass  * Copyright (c) 1982, 1989, 1993
      6  1.1  glass  *	The Regents of the University of California.  All rights reserved.
      7  1.1  glass  *
      8  1.1  glass  * Redistribution and use in source and binary forms, with or without
      9  1.1  glass  * modification, are permitted provided that the following conditions
     10  1.1  glass  * are met:
     11  1.1  glass  * 1. Redistributions of source code must retain the above copyright
     12  1.1  glass  *    notice, this list of conditions and the following disclaimer.
     13  1.1  glass  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  glass  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  glass  *    documentation and/or other materials provided with the distribution.
     16  1.1  glass  * 3. All advertising materials mentioning features or use of this software
     17  1.1  glass  *    must display the following acknowledgement:
     18  1.1  glass  *	This product includes software developed by the University of
     19  1.1  glass  *	California, Berkeley and its contributors.
     20  1.1  glass  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  glass  *    may be used to endorse or promote products derived from this software
     22  1.1  glass  *    without specific prior written permission.
     23  1.1  glass  *
     24  1.1  glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  glass  * SUCH DAMAGE.
     35  1.1  glass  *
     36  1.1  glass  * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
     37  1.1  glass  *       @(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
     38  1.1  glass  *
     39  1.1  glass  */
     40  1.1  glass 
     41  1.1  glass #include <sys/param.h>
     42  1.1  glass #include <sys/systm.h>
     43  1.1  glass #include <sys/kernel.h>
     44  1.1  glass #include <sys/malloc.h>
     45  1.1  glass #include <sys/mbuf.h>
     46  1.1  glass #include <sys/socket.h>
     47  1.1  glass #include <sys/errno.h>
     48  1.1  glass 
     49  1.1  glass #include <machine/cpu.h>
     50  1.1  glass 
     51  1.1  glass #include <net/if.h>
     52  1.1  glass #include <net/netisr.h>
     53  1.1  glass #include <net/route.h>
     54  1.1  glass #include <net/if_dl.h>
     55  1.1  glass #include <net/if_types.h>
     56  1.1  glass 
     57  1.1  glass #ifdef INET
     58  1.1  glass #include <netinet/in.h>
     59  1.1  glass #include <netinet/in_var.h>
     60  1.1  glass #endif
     61  1.1  glass #include <netinet/if_arc.h>
     62  1.1  glass 
     63  1.1  glass u_char	arcbroadcastaddr = 0;
     64  1.1  glass extern	struct ifnet loif;
     65  1.1  glass 
     66  1.1  glass #define senderr(e) { error = (e); goto bad;}
     67  1.1  glass 
     68  1.1  glass #define SIN(s) ((struct sockaddr_in *)s)
     69  1.1  glass 
     70  1.1  glass /*
     71  1.1  glass  * ARCnet output routine.
     72  1.1  glass  * Encapsulate a packet of type family for the local net.
     73  1.1  glass  * Assumes that ifp is actually pointer to arccom structure.
     74  1.1  glass  */
     75  1.1  glass int
     76  1.1  glass arc_output(ifp, m0, dst, rt0)
     77  1.1  glass 	register struct ifnet *ifp;
     78  1.1  glass 	struct mbuf *m0;
     79  1.1  glass 	struct sockaddr *dst;
     80  1.1  glass 	struct rtentry *rt0;
     81  1.1  glass {
     82  1.1  glass 	int s, error = 0;
     83  1.1  glass 	u_char atype, adst;
     84  1.1  glass 	register struct mbuf *m = m0;
     85  1.1  glass 	register struct rtentry *rt;
     86  1.1  glass 	struct mbuf *mcopy = (struct mbuf *)0;
     87  1.1  glass 	register struct arc_header *ah;
     88  1.1  glass 	int off, len = m->m_pkthdr.len;
     89  1.1  glass 	struct arccom *ac = (struct arccom *)ifp;
     90  1.1  glass 
     91  1.1  glass 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
     92  1.1  glass 		senderr(ENETDOWN);
     93  1.1  glass 	ifp->if_lastchange = time;
     94  1.1  glass 	if (rt = rt0) {
     95  1.1  glass 		if ((rt->rt_flags & RTF_UP) == 0) {
     96  1.1  glass 			if (rt0 = rt = rtalloc1(dst, 1))
     97  1.1  glass 				rt->rt_refcnt--;
     98  1.1  glass 			else
     99  1.1  glass 				senderr(EHOSTUNREACH);
    100  1.1  glass 		}
    101  1.1  glass 		if (rt->rt_flags & RTF_GATEWAY) {
    102  1.1  glass 			if (rt->rt_gwroute == 0)
    103  1.1  glass 				goto lookup;
    104  1.1  glass 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    105  1.1  glass 				rtfree(rt); rt = rt0;
    106  1.1  glass 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    107  1.1  glass 				if ((rt = rt->rt_gwroute) == 0)
    108  1.1  glass 					senderr(EHOSTUNREACH);
    109  1.1  glass 			}
    110  1.1  glass 		}
    111  1.1  glass 		if (rt->rt_flags & RTF_REJECT)
    112  1.1  glass 			if (rt->rt_rmx.rmx_expire == 0 ||
    113  1.1  glass 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    114  1.1  glass 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    115  1.1  glass 	}
    116  1.1  glass 	switch (dst->sa_family) {
    117  1.1  glass 
    118  1.1  glass #ifdef INET
    119  1.1  glass 	case AF_INET:
    120  1.1  glass 
    121  1.1  glass 		/*
    122  1.1  glass 		 * For now, use the simple IP addr -> ARCnet addr mapping
    123  1.1  glass 		 */
    124  1.1  glass 		if (m->m_flags & M_BCAST)
    125  1.1  glass 			adst = arcbroadcastaddr; /* ARCnet broadcast address */
    126  1.1  glass 		else
    127  1.1  glass 			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
    128  1.1  glass 
    129  1.1  glass 		/* If broadcasting on a simplex interface, loopback a copy */
    130  1.1  glass 		if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
    131  1.1  glass 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    132  1.1  glass 		off = m->m_pkthdr.len - m->m_len;
    133  1.1  glass 		atype = ARCTYPE_IP_OLD;
    134  1.1  glass 		break;
    135  1.1  glass #endif
    136  1.1  glass 	case AF_UNSPEC:
    137  1.1  glass 		ah = (struct arc_header *)dst->sa_data;
    138  1.1  glass  		adst = ah->arc_dhost;
    139  1.1  glass 		atype = ah->arc_type;
    140  1.1  glass 		break;
    141  1.1  glass 
    142  1.1  glass 	default:
    143  1.1  glass 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
    144  1.1  glass 			dst->sa_family);
    145  1.1  glass 		senderr(EAFNOSUPPORT);
    146  1.1  glass 	}
    147  1.1  glass 
    148  1.1  glass 
    149  1.1  glass 	if (mcopy)
    150  1.1  glass 		(void) looutput(ifp, mcopy, dst, rt);
    151  1.1  glass 	/*
    152  1.1  glass 	 * Add local net header.  If no space in first mbuf,
    153  1.1  glass 	 * allocate another.
    154  1.1  glass 	 *
    155  1.1  glass 	 * For ARCnet, this is just symbolic. The header changes
    156  1.1  glass 	 * form and position on its way into the hardware and out of
    157  1.1  glass 	 * the wire.  At this point, it contains source, destination and
    158  1.1  glass 	 * packet type.
    159  1.1  glass 	 */
    160  1.1  glass 	M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
    161  1.1  glass 	if (m == 0)
    162  1.1  glass 		senderr(ENOBUFS);
    163  1.1  glass 	ah = mtod(m, struct arc_header *);
    164  1.1  glass 	ah->arc_type = atype;
    165  1.1  glass 	ah->arc_dhost= adst;
    166  1.1  glass 	ah->arc_shost= ac->ac_anaddr;
    167  1.1  glass 	s = splimp();
    168  1.1  glass 	/*
    169  1.1  glass 	 * Queue message on interface, and start output if interface
    170  1.1  glass 	 * not yet active.
    171  1.1  glass 	 */
    172  1.1  glass 	if (IF_QFULL(&ifp->if_snd)) {
    173  1.1  glass 		IF_DROP(&ifp->if_snd);
    174  1.1  glass 		splx(s);
    175  1.1  glass 		senderr(ENOBUFS);
    176  1.1  glass 	}
    177  1.1  glass 	IF_ENQUEUE(&ifp->if_snd, m);
    178  1.1  glass 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    179  1.1  glass 		(*ifp->if_start)(ifp);
    180  1.1  glass 	splx(s);
    181  1.1  glass 
    182  1.1  glass 	ifp->if_obytes += len + ARC_HDRLEN;
    183  1.1  glass 	return (error);
    184  1.1  glass 
    185  1.1  glass bad:
    186  1.1  glass 	if (m)
    187  1.1  glass 		m_freem(m);
    188  1.1  glass 	return (error);
    189  1.1  glass }
    190  1.1  glass 
    191  1.1  glass /*
    192  1.1  glass  * Process a received Arcnet packet;
    193  1.1  glass  * the packet is in the mbuf chain m without
    194  1.1  glass  * the ARCnet header, which is provided separately.
    195  1.1  glass  */
    196  1.1  glass void
    197  1.1  glass arc_input(ifp, ah, m)
    198  1.1  glass 	struct ifnet *ifp;
    199  1.1  glass 	register struct arc_header *ah;
    200  1.1  glass 	struct mbuf *m;
    201  1.1  glass {
    202  1.1  glass 	register struct ifqueue *inq;
    203  1.1  glass 	u_char atype;
    204  1.1  glass 	int s;
    205  1.1  glass 
    206  1.1  glass 	if ((ifp->if_flags & IFF_UP) == 0) {
    207  1.1  glass 		m_freem(m);
    208  1.1  glass 		return;
    209  1.1  glass 	}
    210  1.1  glass 	ifp->if_lastchange = time;
    211  1.1  glass 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*ah);
    212  1.1  glass 
    213  1.1  glass 	if (arcbroadcastaddr == ah->arc_dhost) {
    214  1.1  glass 		m->m_flags |= M_BCAST;
    215  1.1  glass 		ifp->if_imcasts++;
    216  1.1  glass 	}
    217  1.1  glass 
    218  1.1  glass 	atype = ah->arc_type;
    219  1.1  glass 	switch (atype) {
    220  1.1  glass #ifdef INET
    221  1.1  glass 	case ARCTYPE_IP_OLD:
    222  1.1  glass 		schednetisr(NETISR_IP);
    223  1.1  glass 		inq = &ipintrq;
    224  1.1  glass 		break;
    225  1.1  glass #endif
    226  1.1  glass 	default:
    227  1.1  glass 		m_freem(m);
    228  1.1  glass 		return;
    229  1.1  glass 	}
    230  1.1  glass 
    231  1.1  glass 	s = splimp();
    232  1.1  glass 	if (IF_QFULL(inq)) {
    233  1.1  glass 		IF_DROP(inq);
    234  1.1  glass 		m_freem(m);
    235  1.1  glass 	} else
    236  1.1  glass 		IF_ENQUEUE(inq, m);
    237  1.1  glass 	splx(s);
    238  1.1  glass }
    239  1.1  glass 
    240  1.1  glass /*
    241  1.1  glass  * Convert Arcnet address to printable (loggable) representation.
    242  1.1  glass  */
    243  1.1  glass static char digits[] = "0123456789abcdef";
    244  1.1  glass char *
    245  1.1  glass arc_sprintf(ap)
    246  1.1  glass 	register u_char *ap;
    247  1.1  glass {
    248  1.1  glass 	static char arcbuf[3];
    249  1.1  glass 	register char *cp = arcbuf;
    250  1.1  glass 
    251  1.1  glass 	*cp++ = digits[*ap >> 4];
    252  1.1  glass 	*cp++ = digits[*ap++ & 0xf];
    253  1.1  glass 	*cp   = 0;
    254  1.1  glass 	return (arcbuf);
    255  1.1  glass }
    256  1.1  glass 
    257  1.1  glass /*
    258  1.1  glass  * Perform common duties while attaching to interface list
    259  1.1  glass  */
    260  1.1  glass void
    261  1.1  glass arc_ifattach(ifp)
    262  1.1  glass 	register struct ifnet *ifp;
    263  1.1  glass {
    264  1.1  glass 	register struct ifaddr *ifa;
    265  1.1  glass 	register struct sockaddr_dl *sdl;
    266  1.1  glass 
    267  1.1  glass 	ifp->if_type = IFT_ARCNET;
    268  1.1  glass 	ifp->if_addrlen = 1;
    269  1.1  glass 	ifp->if_hdrlen = ARC_HDRLEN;
    270  1.1  glass 	ifp->if_mtu = ARCMTU;
    271  1.1  glass 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
    272  1.1  glass 		if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
    273  1.1  glass 		    sdl->sdl_family == AF_LINK) {
    274  1.1  glass 			sdl->sdl_type = IFT_ARCNET;
    275  1.1  glass 			sdl->sdl_alen = ifp->if_addrlen;
    276  1.1  glass 			bcopy((caddr_t)&((struct arccom *)ifp)->ac_anaddr,
    277  1.1  glass 			      LLADDR(sdl), ifp->if_addrlen);
    278  1.1  glass 			break;
    279  1.1  glass 		}
    280  1.1  glass }
    281