Home | History | Annotate | Line # | Download | only in net
if_ethersubr.c revision 1.2.4.1
      1 /*
      2  * Copyright (c) 1982, 1989 Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)if_ethersubr.c	7.13 (Berkeley) 4/20/91
     34  *	$Id: if_ethersubr.c,v 1.2.4.1 1993/10/16 10:49:24 mycroft Exp $
     35  */
     36 
     37 #include "param.h"
     38 #include "systm.h"
     39 #include "kernel.h"
     40 #include "malloc.h"
     41 #include "mbuf.h"
     42 #include "protosw.h"
     43 #include "socket.h"
     44 #include "ioctl.h"
     45 #include "errno.h"
     46 #include "syslog.h"
     47 
     48 #include "if.h"
     49 #include "netisr.h"
     50 #include "route.h"
     51 #include "if_llc.h"
     52 #include "if_dl.h"
     53 
     54 #ifdef INET
     55 #include "../netinet/in.h"
     56 #include "../netinet/in_var.h"
     57 #endif
     58 #include "../netinet/if_ether.h"
     59 
     60 #ifdef NS
     61 #include "../netns/ns.h"
     62 #include "../netns/ns_if.h"
     63 #endif
     64 
     65 #ifdef ISO
     66 #include "../netiso/argo_debug.h"
     67 #include "../netiso/iso.h"
     68 #include "../netiso/iso_var.h"
     69 #include "../netiso/iso_snpac.h"
     70 #endif
     71 
     72 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
     73 extern	struct ifnet loif;
     74 
     75 /*
     76  * Ethernet output routine.
     77  * Encapsulate a packet of type family for the local net.
     78  * Use trailer local net encapsulation if enough data in first
     79  * packet leaves a multiple of 512 bytes of data in remainder.
     80  * Assumes that ifp is actually pointer to arpcom structure.
     81  */
     82 ether_output(ifp, m0, dst, rt)
     83 	register struct ifnet *ifp;
     84 	struct mbuf *m0;
     85 	struct sockaddr *dst;
     86 	struct rtentry *rt;
     87 {
     88 	short type;
     89 	int s, error = 0;
     90  	u_char edst[6];
     91 	struct in_addr idst;
     92 	register struct mbuf *m = m0;
     93 	struct mbuf *mcopy = (struct mbuf *)0;
     94 	register struct ether_header *eh;
     95 	int usetrailers, off, len = m->m_pkthdr.len;
     96 #define	ac ((struct arpcom *)ifp)
     97 
     98 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
     99 		error = ENETDOWN;
    100 		goto bad;
    101 	}
    102 	ifp->if_lastchange = time;
    103 	switch (dst->sa_family) {
    104 
    105 #ifdef INET
    106 	case AF_INET:
    107 		idst = ((struct sockaddr_in *)dst)->sin_addr;
    108  		if (!arpresolve(ac, m, &idst, edst, &usetrailers))
    109 			return (0);	/* if not yet resolved */
    110 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
    111 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    112 		off = m->m_pkthdr.len - m->m_len;
    113 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
    114 		    (m->m_flags & M_EXT) == 0 &&
    115 		    m->m_data >= m->m_pktdat + 2 * sizeof (u_short)) {
    116 			type = ETHERTYPE_TRAIL + (off>>9);
    117 			m->m_data -= 2 * sizeof (u_short);
    118 			m->m_len += 2 * sizeof (u_short);
    119 			len += 2 * sizeof (u_short);
    120 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
    121 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
    122 			goto gottrailertype;
    123 		}
    124 		type = ETHERTYPE_IP;
    125 		goto gottype;
    126 #endif
    127 #ifdef NS
    128 	case AF_NS:
    129 		type = ETHERTYPE_NS;
    130  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
    131 		    (caddr_t)edst, sizeof (edst));
    132 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst)))
    133 			return (looutput(ifp, m, dst, rt));
    134 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1))
    135 			mcopy = m_copy(m, 0, (int)M_COPYALL);
    136 		goto gottype;
    137 #endif
    138 #ifdef	ISO
    139 	case AF_ISO: {
    140 		int	snpalen;
    141 		struct	llc *l;
    142 
    143 	iso_again:
    144 		if (rt && rt->rt_gateway && (rt->rt_flags & RTF_UP)) {
    145 			if (rt->rt_flags & RTF_GATEWAY) {
    146 				if (rt->rt_llinfo) {
    147 					rt = (struct rtentry *)rt->rt_llinfo;
    148 					goto iso_again;
    149 				}
    150 			} else {
    151 				register struct sockaddr_dl *sdl =
    152 					(struct sockaddr_dl *)rt->rt_gateway;
    153 				if (sdl && sdl->sdl_family == AF_LINK
    154 				    && sdl->sdl_alen > 0) {
    155 					bcopy(LLADDR(sdl), (char *)edst,
    156 								sizeof(edst));
    157 					goto iso_resolved;
    158 				}
    159 			}
    160 		}
    161 		if ((error = iso_snparesolve(ifp, (struct sockaddr_iso *)dst,
    162 					(char *)edst, &snpalen)) > 0)
    163 			goto bad; /* Not Resolved */
    164 	iso_resolved:
    165 		if ((ifp->if_flags & IFF_SIMPLEX) && (*edst & 1) &&
    166 		    (mcopy = m_copy(m, 0, (int)M_COPYALL))) {
    167 			M_PREPEND(mcopy, sizeof (*eh), M_DONTWAIT);
    168 			if (mcopy) {
    169 				eh = mtod(mcopy, struct ether_header *);
    170 				bcopy((caddr_t)edst,
    171 				      (caddr_t)eh->ether_dhost, sizeof (edst));
    172 				bcopy((caddr_t)ac->ac_enaddr,
    173 				      (caddr_t)eh->ether_shost, sizeof (edst));
    174 			}
    175 		}
    176 		M_PREPEND(m, 3, M_DONTWAIT);
    177 		if (m == NULL)
    178 			return (0);
    179 		type = m->m_pkthdr.len;
    180 		l = mtod(m, struct llc *);
    181 		l->llc_dsap = l->llc_ssap = LLC_ISO_LSAP;
    182 		l->llc_control = LLC_UI;
    183 		len += 3;
    184 		IFDEBUG(D_ETHER)
    185 			int i;
    186 			printf("unoutput: sending pkt to: ");
    187 			for (i=0; i<6; i++)
    188 				printf("%x ", edst[i] & 0xff);
    189 			printf("\n");
    190 		ENDDEBUG
    191 		} goto gottype;
    192 #endif	ISO
    193 #ifdef RMP
    194 	case AF_RMP:
    195 		/*
    196 		 *  This is IEEE 802.3 -- the Ethernet `type' field is
    197 		 *  really a `length' field.
    198 		 */
    199 		type = m->m_len;
    200  		bcopy((caddr_t)dst->sa_data, (caddr_t)edst, sizeof(edst));
    201 		break;
    202 #endif
    203 
    204 	case AF_UNSPEC:
    205 		eh = (struct ether_header *)dst->sa_data;
    206  		bcopy((caddr_t)eh->ether_dhost, (caddr_t)edst, sizeof (edst));
    207 		type = eh->ether_type;
    208 		goto gottype;
    209 
    210 	default:
    211 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
    212 			dst->sa_family);
    213 		error = EAFNOSUPPORT;
    214 		goto bad;
    215 	}
    216 
    217 gottrailertype:
    218 	/*
    219 	 * Packet to be sent as trailer: move first packet
    220 	 * (control information) to end of chain.
    221 	 */
    222 	while (m->m_next)
    223 		m = m->m_next;
    224 	m->m_next = m0;
    225 	m = m0->m_next;
    226 	m0->m_next = 0;
    227 
    228 gottype:
    229 	if (mcopy)
    230 		(void) looutput(ifp, mcopy, dst, rt);
    231 	/*
    232 	 * Add local net header.  If no space in first mbuf,
    233 	 * allocate another.
    234 	 */
    235 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
    236 	if (m == 0) {
    237 		error = ENOBUFS;
    238 		goto bad;
    239 	}
    240 	eh = mtod(m, struct ether_header *);
    241 	type = htons((u_short)type);
    242 	bcopy((caddr_t)&type,(caddr_t)&eh->ether_type,
    243 		sizeof(eh->ether_type));
    244  	bcopy((caddr_t)edst, (caddr_t)eh->ether_dhost, sizeof (edst));
    245  	bcopy((caddr_t)ac->ac_enaddr, (caddr_t)eh->ether_shost,
    246 	    sizeof(eh->ether_shost));
    247 	s = splimp();
    248 	/*
    249 	 * Queue message on interface, and start output if interface
    250 	 * not yet active.
    251 	 */
    252 	if (IF_QFULL(&ifp->if_snd)) {
    253 		IF_DROP(&ifp->if_snd);
    254 		splx(s);
    255 		error = ENOBUFS;
    256 		goto bad;
    257 	}
    258 	IF_ENQUEUE(&ifp->if_snd, m);
    259 	if ((ifp->if_flags & IFF_OACTIVE) == 0)
    260 		(*ifp->if_start)(ifp);
    261 	splx(s);
    262 	ifp->if_obytes += len + sizeof (struct ether_header);
    263 	if (edst[0] & 1)
    264 		ifp->if_omcasts++;
    265 	return (error);
    266 
    267 bad:
    268 	if (m)
    269 		m_freem(m);
    270 	return (error);
    271 }
    272 
    273 /*
    274  * Process a received Ethernet packet;
    275  * the packet is in the mbuf chain m without
    276  * the ether header, which is provided separately.
    277  */
    278 ether_input(ifp, eh, m)
    279 	struct ifnet *ifp;
    280 	register struct ether_header *eh;
    281 	struct mbuf *m;
    282 {
    283 	register struct ifqueue *inq;
    284 	register struct llc *l;
    285 	int s;
    286 
    287 	ifp->if_lastchange = time;
    288 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
    289 	if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
    290 	    sizeof(etherbroadcastaddr)) == 0)
    291 		m->m_flags |= M_BCAST;
    292 	else if (eh->ether_dhost[0] & 1)
    293 		m->m_flags |= M_MCAST;
    294 	if (m->m_flags & (M_BCAST|M_MCAST))
    295 		ifp->if_imcasts++;
    296 
    297 	switch (eh->ether_type) {
    298 #ifdef INET
    299 	case ETHERTYPE_IP:
    300 		schednetisr(NETISR_IP);
    301 		inq = &ipintrq;
    302 		break;
    303 
    304 	case ETHERTYPE_ARP:
    305 		arpinput((struct arpcom *)ifp, m);
    306 		return;
    307 #endif
    308 #ifdef NS
    309 	case ETHERTYPE_NS:
    310 		schednetisr(NETISR_NS);
    311 		inq = &nsintrq;
    312 		break;
    313 
    314 #endif
    315 	default:
    316 #ifdef	ISO
    317 		if (eh->ether_type > ETHERMTU)
    318 			goto dropanyway;
    319 		l = mtod(m, struct llc *);
    320 		switch (l->llc_control) {
    321 		case LLC_UI:
    322 		/* LLC_UI_P forbidden in class 1 service */
    323 		    if ((l->llc_dsap == LLC_ISO_LSAP) &&
    324 			(l->llc_ssap == LLC_ISO_LSAP)) {
    325 				/* LSAP for ISO */
    326 			if (m->m_pkthdr.len > eh->ether_type)
    327 				m_adj(m, eh->ether_type - m->m_pkthdr.len);
    328 			m->m_data += 3;		/* XXX */
    329 			m->m_len -= 3;		/* XXX */
    330 			m->m_pkthdr.len -= 3;	/* XXX */
    331 			M_PREPEND(m, sizeof *eh, M_DONTWAIT);
    332 			if (m == 0)
    333 				return;
    334 			*mtod(m, struct ether_header *) = *eh;
    335 			IFDEBUG(D_ETHER)
    336 			    printf("clnp packet");
    337 			ENDDEBUG
    338 			schednetisr(NETISR_ISO);
    339 			inq = &clnlintrq;
    340 			break;
    341 		    }
    342 		    goto dropanyway;
    343 
    344 		case LLC_XID:
    345 		case LLC_XID_P:
    346 		    if(m->m_len < 6)
    347 			goto dropanyway;
    348 		    l->llc_window = 0;
    349 		    l->llc_fid = 9;
    350 		    l->llc_class = 1;
    351 		    l->llc_dsap = l->llc_ssap = 0;
    352 		    /* Fall through to */
    353 		case LLC_TEST:
    354 		case LLC_TEST_P:
    355 		{
    356 		    struct sockaddr sa;
    357 		    register struct ether_header *eh2;
    358 		    int i;
    359 		    u_char c = l->llc_dsap;
    360 		    l->llc_dsap = l->llc_ssap;
    361 		    l->llc_ssap = c;
    362 		    if (m->m_flags & (M_BCAST | M_MCAST))
    363 			bcopy((caddr_t)ac->ac_enaddr,
    364 			      (caddr_t)eh->ether_dhost, 6);
    365 		    sa.sa_family = AF_UNSPEC;
    366 		    sa.sa_len = sizeof(sa);
    367 		    eh2 = (struct ether_header *)sa.sa_data;
    368 		    for (i = 0; i < 6; i++) {
    369 			eh2->ether_shost[i] = c = eh->ether_dhost[i];
    370 			eh2->ether_dhost[i] =
    371 				eh->ether_dhost[i] = eh->ether_shost[i];
    372 			eh->ether_shost[i] = c;
    373 		    }
    374 		    ifp->if_output(ifp, m, &sa);
    375 		    return;
    376 		}
    377 		dropanyway:
    378 		default:
    379 		    m_freem(m);
    380 		    return;
    381 	    }
    382 #else
    383 	    m_freem(m);
    384 	    return;
    385 #endif	ISO
    386 	}
    387 
    388 	s = splimp();
    389 	if (IF_QFULL(inq)) {
    390 		IF_DROP(inq);
    391 		m_freem(m);
    392 	} else
    393 		IF_ENQUEUE(inq, m);
    394 	splx(s);
    395 }
    396 
    397 /*
    398  * Convert Ethernet address to printable (loggable) representation.
    399  */
    400 static char digits[] = "0123456789abcdef";
    401 char *
    402 ether_sprintf(ap)
    403 	register u_char *ap;
    404 {
    405 	register i;
    406 	static char etherbuf[18];
    407 	register char *cp = etherbuf;
    408 
    409 	for (i = 0; i < 6; i++) {
    410 		*cp++ = digits[*ap >> 4];
    411 		*cp++ = digits[*ap++ & 0xf];
    412 		*cp++ = ':';
    413 	}
    414 	*--cp = 0;
    415 	return (etherbuf);
    416 }
    417