Home | History | Annotate | Line # | Download | only in net
if_arcsubr.c revision 1.11.4.3
      1 /*	$NetBSD: if_arcsubr.c,v 1.11.4.3 1997/02/11 16:28:24 is Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Ignatios Souvatzis
      5  * Copyright (c) 1982, 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  * from: NetBSD: if_ethersubr.c,v 1.9 1994/06/29 06:36:11 cgd Exp
     37  *       @(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
     38  *
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/protosw.h>
     47 #include <sys/socket.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/errno.h>
     50 #include <sys/syslog.h>
     51 
     52 #include <machine/cpu.h>
     53 
     54 #include <net/if.h>
     55 #include <net/netisr.h>
     56 #include <net/route.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_types.h>
     59 
     60 #ifdef INET
     61 #include <netinet/in.h>
     62 #include <netinet/in_var.h>
     63 #endif
     64 #include <netinet/if_arc.h>
     65 
     66 #ifndef	ARC_PHDSMTU
     67 #define	ARC_PHDSMTU	1500
     68 #endif
     69 
     70 static struct mbuf *arc_defrag __P((struct ifnet *, struct mbuf *));
     71 
     72 /*
     73  * RC1201 requires us to have this configurable. We have it only per
     74  * machine at the moment... there is no generic "set mtu" ioctl, AFAICS.
     75  * Anyway, it is possible to binpatch this or set it per kernel config
     76  * option.
     77  */
     78 #if ARC_PHDSMTU > 60480
     79 ERROR: The arc_phdsmtu is ARC_PHDSMTU, but must not exceed 60480.
     80 #endif
     81 u_int16_t arc_phdsmtu = ARC_PHDSMTU;
     82 u_int8_t  arcbroadcastaddr = 0;
     83 
     84 #define senderr(e) { error = (e); goto bad;}
     85 #define SIN(s) ((struct sockaddr_in *)s)
     86 
     87 /*
     88  * ARCnet output routine.
     89  * Encapsulate a packet of type family for the local net.
     90  * Assumes that ifp is actually pointer to arccom structure.
     91  */
     92 int
     93 arc_output(ifp, m0, dst, rt0)
     94 	register struct ifnet *ifp;
     95 	struct mbuf *m0;
     96 	struct sockaddr *dst;
     97 	struct rtentry *rt0;
     98 {
     99 	struct mbuf		*m, *m1, *mcopy;
    100 	struct rtentry		*rt;
    101 	struct arccom		*ac;
    102 	register struct arc_header *ah;
    103 	int			s, error, newencoding;
    104 	u_int8_t		atype, adst, myself;
    105 	int			tfrags, sflag, fsflag, rsflag;
    106 
    107 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
    108 		return(ENETDOWN); /* m, m1 aren't initialized yet */
    109 
    110 	error = newencoding = 0;
    111 	ac = (struct arccom *)ifp;
    112 	m = m0;
    113 	mcopy = m1 = NULL;
    114 
    115 	myself = *LLADDR(ifp->if_sadl);
    116 
    117 	ifp->if_lastchange = time;
    118 	if ((rt = rt0)) {
    119 		if ((rt->rt_flags & RTF_UP) == 0) {
    120 			if ((rt0 = rt = rtalloc1(dst, 1)))
    121 				rt->rt_refcnt--;
    122 			else
    123 				senderr(EHOSTUNREACH);
    124 		}
    125 		if (rt->rt_flags & RTF_GATEWAY) {
    126 			if (rt->rt_gwroute == 0)
    127 				goto lookup;
    128 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
    129 				rtfree(rt); rt = rt0;
    130 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
    131 				if ((rt = rt->rt_gwroute) == 0)
    132 					senderr(EHOSTUNREACH);
    133 			}
    134 		}
    135 		if (rt->rt_flags & RTF_REJECT)
    136 			if (rt->rt_rmx.rmx_expire == 0 ||
    137 			    time.tv_sec < rt->rt_rmx.rmx_expire)
    138 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
    139 	}
    140 
    141 	switch (dst->sa_family) {
    142 #ifdef INET
    143 	case AF_INET:
    144 
    145 		/*
    146 		 * For now, use the simple IP addr -> ARCnet addr mapping
    147 		 */
    148 		if (m->m_flags & (M_BCAST|M_MCAST))
    149 			adst = arcbroadcastaddr; /* ARCnet broadcast address */
    150 		else
    151 			adst = ntohl(SIN(dst)->sin_addr.s_addr) & 0xFF;
    152 
    153 		/* If broadcasting on a simplex interface, loopback a copy */
    154 		if ((m->m_flags & (M_BCAST|M_MCAST)) &&
    155 		    (ifp->if_flags & IFF_SIMPLEX))
    156 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    157 		if (ifp->if_flags & IFF_LINK0) {
    158 			atype = ARCTYPE_IP;
    159 			newencoding = 1;
    160 		} else {
    161 			atype = ARCTYPE_IP_OLD;
    162 			newencoding = 0;
    163 		}
    164 		break;
    165 #endif
    166 
    167 	case AF_UNSPEC:
    168 		ah = (struct arc_header *)dst->sa_data;
    169  		adst = ah->arc_dhost;
    170 		atype = ah->arc_type;
    171 		break;
    172 
    173 	default:
    174 		printf("%s: can't handle af%d\n", ifp->if_xname,
    175 		    dst->sa_family);
    176 		senderr(EAFNOSUPPORT);
    177 	}
    178 
    179 	if (mcopy)
    180 		(void) looutput(ifp, mcopy, dst, rt);
    181 
    182 	/*
    183 	 * Add local net header.  If no space in first mbuf,
    184 	 * allocate another.
    185 	 *
    186 	 * For ARCnet, this is just symbolic. The header changes
    187 	 * form and position on its way into the hardware and out of
    188 	 * the wire.  At this point, it contains source, destination and
    189 	 * packet type.
    190 	 */
    191 	if (newencoding) {
    192 		++ac->ac_seqid; /* make the seqid unique */
    193 
    194 		tfrags = (m->m_pkthdr.len + 503) / 504;
    195 		fsflag = 2 * tfrags - 3;
    196 		sflag = 0;
    197 		rsflag = fsflag;
    198 
    199 		while (sflag < fsflag) {
    200 			/* we CAN'T have short packets here */
    201 			m1 = m_split(m, 504, M_DONTWAIT);
    202 			if (m1 == 0)
    203 				senderr(ENOBUFS);
    204 
    205 			M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    206 			if (m == 0)
    207 				senderr(ENOBUFS);
    208 			ah = mtod(m, struct arc_header *);
    209 			ah->arc_type = atype;
    210 			ah->arc_dhost = adst;
    211 			ah->arc_shost = myself;
    212 			ah->arc_flag = rsflag;
    213 			ah->arc_seqid = ac->ac_seqid;
    214 
    215 			s = splimp();
    216 			/*
    217 			 * Queue message on interface, and start output if
    218 			 * interface not yet active.
    219 			 */
    220 			if (IF_QFULL(&ifp->if_snd)) {
    221 				IF_DROP(&ifp->if_snd);
    222 				splx(s);
    223 				senderr(ENOBUFS);
    224 			}
    225 			ifp->if_obytes += m->m_pkthdr.len;
    226 			IF_ENQUEUE(&ifp->if_snd, m);
    227 			if ((ifp->if_flags & IFF_OACTIVE) == 0)
    228 				(*ifp->if_start)(ifp);
    229 			splx(s);
    230 
    231 			m = m1;
    232 			sflag += 2;
    233 			rsflag = sflag;
    234 		}
    235 		m1 = NULL;
    236 
    237 		M_PREPEND(m, ARC_HDRNEWLEN, M_DONTWAIT);
    238 		if (m == 0)
    239 			senderr(ENOBUFS);
    240 		ah = mtod(m, struct arc_header *);
    241 		ah->arc_type = atype;
    242 		ah->arc_flag = sflag;
    243 		ah->arc_seqid = ac->ac_seqid;
    244 
    245 		/* here we can have small, especially forbidden packets */
    246 
    247 		if ((m->m_pkthdr.len >= ARC_MIN_FORBID_LEN + 2) &&
    248 		    (m->m_pkthdr.len <= ARC_MAX_FORBID_LEN + 2)) {
    249 			M_PREPEND(m, 4, M_DONTWAIT);
    250 			if (m == 0)
    251 				senderr(ENOBUFS);
    252 			m = m_pullup(m, ARC_HDRNEWLEN);
    253 			if (m == 0)
    254 				senderr(ENOBUFS);
    255 			ah = mtod(m, struct arc_header *);
    256 			ah->arc_type = atype;
    257 			ah->arc_flag = 0xFF;
    258 			ah->arc_seqid = 0xFFFF;
    259 		}
    260 
    261 		ah->arc_dhost = adst;
    262 		ah->arc_shost = myself;
    263 	} else {
    264 		M_PREPEND(m, ARC_HDRLEN, M_DONTWAIT);
    265 		if (m == 0)
    266 			senderr(ENOBUFS);
    267 		ah = mtod(m, struct arc_header *);
    268 		ah->arc_type = atype;
    269 		ah->arc_dhost = adst;
    270 		ah->arc_shost = myself;
    271 	}
    272 
    273 	s = splimp();
    274 	/*
    275 	 * Queue message on interface, and start output if interface
    276 	 * not yet active.
    277 	 */
    278 	if (IF_QFULL(&ifp->if_snd)) {
    279 		IF_DROP(&ifp->if_snd);
    280 		splx(s);
    281 		senderr(ENOBUFS);
    282 	}
    283 	ifp->if_obytes += m->m_pkthdr.len;
    284 	IF_ENQUEUE(&ifp->if_snd, m);
    285 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    286 		(*ifp->if_start)(ifp);
    287 	splx(s);
    288 
    289 	return (error);
    290 
    291 bad:
    292 	if (m1)
    293 		m_freem(m1);
    294 	if (m)
    295 		m_freem(m);
    296 	return (error);
    297 }
    298 
    299 /*
    300  * Defragmenter. Returns mbuf if last packet found, else
    301  * NULL. frees imcoming mbuf as necessary.
    302  */
    303 
    304 __inline struct mbuf *
    305 arc_defrag(ifp, m)
    306 	struct ifnet *ifp;
    307 	struct mbuf *m;
    308 {
    309 	struct arc_header *ah, *ah1;
    310 	struct arccom *ac;
    311 	struct ac_frag *af;
    312 	struct mbuf *m1;
    313 	char *s;
    314 	int newflen;
    315 	u_char src,dst,typ;
    316 
    317 	ac = (struct arccom *)ifp;
    318 
    319 	m = m_pullup(m, ARC_HDRNEWLEN);
    320 	if (m == NULL) {
    321 		++ifp->if_ierrors;
    322 		return NULL;
    323 	}
    324 
    325 	ah = mtod(m, struct arc_header *);
    326 	typ = ah->arc_type;
    327 
    328 	if (!arc_isphds(typ))
    329 		return m;
    330 
    331 	src = ah->arc_shost;
    332 	dst = ah->arc_dhost;
    333 
    334 	if (ah->arc_flag == 0xff) {
    335 		m_adj(m, 4);
    336 
    337 		m = m_pullup(m, ARC_HDRNEWLEN);
    338 		if (m == NULL) {
    339 			++ifp->if_ierrors;
    340 			return NULL;
    341 		}
    342 
    343 		ah = mtod(m, struct arc_header *);
    344 		ah->arc_shost = src;
    345 		ah->arc_dhost = dst;
    346 		ah->arc_type = typ;
    347 	}
    348 
    349 	af = &ac->ac_fragtab[src];
    350 	m1 = af->af_packet;
    351 	s = "debug code error";
    352 
    353 	if (ah->arc_flag & 1) {
    354 		/*
    355 		 * first fragment. We always initialize, which is
    356 		 * about the right thing to do, as we only want to
    357 		 * accept one fragmented packet per src at a time.
    358 		 */
    359 		if (m1 != NULL)
    360 			m_freem(m1);
    361 
    362 		af->af_packet = m;
    363 		m1 = m;
    364 		af->af_maxflag = ah->arc_flag;
    365 		af->af_lastseen = 0;
    366 		af->af_seqid = ah->arc_seqid;
    367 
    368 		return NULL;
    369 		/* notreached */
    370 	} else {
    371 		/* check for unfragmented packet */
    372 		if (ah->arc_flag == 0)
    373 			return m;
    374 
    375 		/* do we have a first packet from that src? */
    376 		if (m1 == NULL) {
    377 			s = "no first frag";
    378 			goto outofseq;
    379 		}
    380 
    381 		ah1 = mtod(m1, struct arc_header *);
    382 
    383 		if (ah->arc_seqid != ah1->arc_seqid) {
    384 			s = "seqid differs";
    385 			goto outofseq;
    386 		}
    387 
    388 		if (ah->arc_type != ah1->arc_type) {
    389 			s = "type differs";
    390 			goto outofseq;
    391 		}
    392 
    393 		if (ah->arc_dhost != ah1->arc_dhost) {
    394 			s = "dest host differs";
    395 			goto outofseq;
    396 		}
    397 
    398 		/* typ, seqid and dst are ok here. */
    399 
    400 		if (ah->arc_flag == af->af_lastseen) {
    401 			m_freem(m);
    402 			return NULL;
    403 		}
    404 
    405 		if (ah->arc_flag == af->af_lastseen + 2) {
    406 			/* ok, this is next fragment */
    407 			af->af_lastseen = ah->arc_flag;
    408 			m_adj(m,ARC_HDRNEWLEN);
    409 
    410 			/*
    411 			 * m_cat might free the first mbuf (with pkthdr)
    412 			 * in 2nd chain; therefore:
    413 			 */
    414 
    415 			newflen = m->m_pkthdr.len;
    416 
    417 			m_cat(m1,m);
    418 
    419 			m1->m_pkthdr.len += newflen;
    420 
    421 			/* is it the last one? */
    422 			if (af->af_lastseen > af->af_maxflag) {
    423 				af->af_packet = NULL;
    424 				return(m1);
    425 			} else
    426 				return NULL;
    427 		}
    428 		s = "other reason";
    429 		/* if all else fails, it is out of sequence, too */
    430 	}
    431 outofseq:
    432 	if (m1) {
    433 		m_freem(m1);
    434 		af->af_packet = NULL;
    435 	}
    436 
    437 	if (m)
    438 		m_freem(m);
    439 
    440 	log(LOG_INFO,"%s: got out of seq. packet: %s\n",
    441 	    ifp->if_xname, s);
    442 
    443 	return NULL;
    444 }
    445 
    446 /*
    447  * return 1 if Packet Header Definition Standard, else 0.
    448  * For now: old IP, old ARP aren't obviously. Lacking correct information,
    449  * we guess that besides new IP and new ARP also IPX and APPLETALK are PHDS.
    450  * (Apple and Novell corporations were involved, among others, in PHDS work).
    451  * Easiest is to assume that everybody else uses that, too.
    452  */
    453 int
    454 arc_isphds(type)
    455 	u_int8_t type;
    456 {
    457 	return ((type != ARCTYPE_IP_OLD &&
    458 		 type != ARCTYPE_ARP_OLD));
    459 }
    460 
    461 /*
    462  * Process a received Arcnet packet;
    463  * the packet is in the mbuf chain m with
    464  * the ARCnet header.
    465  */
    466 void
    467 arc_input(ifp, m)
    468 	struct ifnet *ifp;
    469 	struct mbuf *m;
    470 {
    471 	register struct arc_header *ah;
    472 	register struct ifqueue *inq;
    473 	u_int8_t atype;
    474 	int s;
    475 
    476 	if ((ifp->if_flags & IFF_UP) == 0) {
    477 		m_freem(m);
    478 		return;
    479 	}
    480 
    481 	/* possibly defragment: */
    482 	m = arc_defrag(ifp, m);
    483 	if (m == NULL)
    484 		return;
    485 
    486 	ah = mtod(m, struct arc_header *);
    487 
    488 	ifp->if_lastchange = time;
    489 	ifp->if_ibytes += m->m_pkthdr.len;
    490 
    491 	if (arcbroadcastaddr == ah->arc_dhost) {
    492 		m->m_flags |= M_BCAST|M_MCAST;
    493 		ifp->if_imcasts++;
    494 	}
    495 
    496 	atype = ah->arc_type;
    497 	switch (atype) {
    498 #ifdef INET
    499 	case ARCTYPE_IP:
    500 		m_adj(m, ARC_HDRNEWLEN);
    501 		schednetisr(NETISR_IP);
    502 		inq = &ipintrq;
    503 		break;
    504 
    505 	case ARCTYPE_IP_OLD:
    506 		m_adj(m, ARC_HDRLEN);
    507 		schednetisr(NETISR_IP);
    508 		inq = &ipintrq;
    509 		break;
    510 #endif
    511 	default:
    512 		m_freem(m);
    513 		return;
    514 	}
    515 
    516 	s = splimp();
    517 	if (IF_QFULL(inq)) {
    518 		IF_DROP(inq);
    519 		m_freem(m);
    520 	} else
    521 		IF_ENQUEUE(inq, m);
    522 	splx(s);
    523 }
    524 
    525 /*
    526  * Convert Arcnet address to printable (loggable) representation.
    527  */
    528 static char digits[] = "0123456789abcdef";
    529 char *
    530 arc_sprintf(ap)
    531 	register u_int8_t *ap;
    532 {
    533 	static char arcbuf[3];
    534 	register char *cp = arcbuf;
    535 
    536 	*cp++ = digits[*ap >> 4];
    537 	*cp++ = digits[*ap++ & 0xf];
    538 	*cp   = 0;
    539 	return (arcbuf);
    540 }
    541 
    542 /*
    543  * Perform common duties while attaching to interface list
    544  */
    545 void
    546 arc_ifattach(ifp, lla)
    547 	register struct ifnet *ifp;
    548 	u_int8_t lla;
    549 {
    550 	register struct ifaddr *ifa;
    551 	register struct sockaddr_dl *sdl;
    552 	register struct arccom *ac;
    553 
    554 	ifp->if_type = IFT_ARCNET;
    555 	ifp->if_addrlen = 1;
    556 	ifp->if_hdrlen = ARC_HDRLEN;
    557 	if (ifp->if_flags & IFF_BROADCAST)
    558 		ifp->if_flags |= IFF_MULTICAST|IFF_ALLMULTI;
    559 	if (ifp->if_flags & IFF_LINK0 && arc_phdsmtu > 60480)
    560 		log(LOG_ERR,
    561 		    "%s: arc_phdsmtu is %d, but must not exceed 60480",
    562 		    ifp->if_xname, arc_phdsmtu);
    563 
    564 	ifp->if_mtu = (ifp->if_flags & IFF_LINK0 ? arc_phdsmtu : ARCMTU);
    565 	ac = (struct arccom *)ifp;
    566 	ac->ac_seqid = (time.tv_sec) & 0xFFFF; /* try to make seqid unique */
    567 	if (lla == 0) {
    568 		/* XXX this message isn't entirely clear, to me -- cgd */
    569 		log(LOG_ERR,"%s: link address 0 reserved for broadcasts.  Please change it and ifconfig %s down up\n",
    570 		   ifp->if_xname, ifp->if_xname);
    571 	}
    572 	if ((sdl = ifp->if_sadl) &&
    573 	   sdl->sdl_family == AF_LINK) {
    574 		sdl->sdl_type = IFT_ARCNET;
    575 		sdl->sdl_alen = ifp->if_addrlen;
    576 		bcopy((caddr_t)&lla, LLADDR(sdl), ifp->if_addrlen);
    577 	}
    578 }
    579