Home | History | Annotate | Line # | Download | only in libsa
ether.c revision 1.12.16.1
      1  1.12.16.1      fvdl /*	$NetBSD: ether.c,v 1.12.16.1 1999/11/15 00:42:01 fvdl Exp $	*/
      2        1.2       cgd 
      3        1.1    brezak /*
      4        1.1    brezak  * Copyright (c) 1992 Regents of the University of California.
      5        1.1    brezak  * All rights reserved.
      6        1.1    brezak  *
      7        1.1    brezak  * This software was developed by the Computer Systems Engineering group
      8        1.1    brezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9        1.1    brezak  * contributed to Berkeley.
     10        1.1    brezak  *
     11        1.1    brezak  * Redistribution and use in source and binary forms, with or without
     12        1.1    brezak  * modification, are permitted provided that the following conditions
     13        1.1    brezak  * are met:
     14        1.1    brezak  * 1. Redistributions of source code must retain the above copyright
     15        1.1    brezak  *    notice, this list of conditions and the following disclaimer.
     16        1.1    brezak  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1    brezak  *    notice, this list of conditions and the following disclaimer in the
     18        1.1    brezak  *    documentation and/or other materials provided with the distribution.
     19        1.1    brezak  * 3. All advertising materials mentioning features or use of this software
     20        1.1    brezak  *    must display the following acknowledgement:
     21        1.1    brezak  *	This product includes software developed by the University of
     22        1.1    brezak  *	California, Lawrence Berkeley Laboratory and its contributors.
     23        1.1    brezak  * 4. Neither the name of the University nor the names of its contributors
     24        1.1    brezak  *    may be used to endorse or promote products derived from this software
     25        1.1    brezak  *    without specific prior written permission.
     26        1.1    brezak  *
     27        1.1    brezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28        1.1    brezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29        1.1    brezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30        1.1    brezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31        1.1    brezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32        1.1    brezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33        1.1    brezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34        1.1    brezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35        1.1    brezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36        1.1    brezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37        1.1    brezak  * SUCH DAMAGE.
     38        1.1    brezak  *
     39        1.2       cgd  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
     40        1.1    brezak  */
     41        1.1    brezak 
     42        1.1    brezak #include <sys/param.h>
     43        1.1    brezak #include <sys/socket.h>
     44       1.10  drochner #ifdef _STANDALONE
     45       1.10  drochner #include <lib/libkern/libkern.h>
     46       1.10  drochner #else
     47        1.1    brezak #include <string.h>
     48       1.10  drochner #endif
     49        1.1    brezak 
     50        1.1    brezak #include <net/if.h>
     51       1.11  drochner #include <net/if_ether.h>
     52        1.1    brezak 
     53        1.1    brezak #include <netinet/in.h>
     54        1.1    brezak #include <netinet/in_systm.h>
     55        1.1    brezak #include <netinet/ip.h>
     56        1.1    brezak 
     57        1.1    brezak #include "stand.h"
     58        1.1    brezak #include "net.h"
     59        1.1    brezak #include "netif.h"
     60        1.1    brezak 
     61        1.1    brezak /* Caller must leave room for ethernet header in front!! */
     62        1.6        pk ssize_t
     63        1.4   mycroft sendether(d, pkt, len, dea, etype)
     64        1.1    brezak 	struct iodesc *d;
     65        1.4   mycroft 	void *pkt;
     66        1.4   mycroft 	size_t len;
     67        1.1    brezak 	u_char *dea;
     68        1.1    brezak 	int etype;
     69        1.1    brezak {
     70        1.6        pk 	register ssize_t n;
     71        1.1    brezak 	register struct ether_header *eh;
     72        1.1    brezak 
     73        1.1    brezak #ifdef ETHER_DEBUG
     74        1.1    brezak  	if (debug)
     75        1.8  christos 		printf("sendether: called\n");
     76        1.1    brezak #endif
     77        1.4   mycroft 
     78        1.4   mycroft 	eh = (struct ether_header *)pkt - 1;
     79        1.4   mycroft 	len += sizeof(*eh);
     80        1.1    brezak 
     81        1.1    brezak 	MACPY(d->myea, eh->ether_shost);		/* by byte */
     82        1.1    brezak 	MACPY(dea, eh->ether_dhost);			/* by byte */
     83        1.1    brezak 	eh->ether_type = htons(etype);
     84        1.4   mycroft 
     85        1.6        pk 	n = netif_put(d, eh, len);
     86        1.6        pk 	if (n == -1 || n < sizeof(*eh))
     87        1.4   mycroft 		return (-1);
     88        1.4   mycroft 
     89        1.6        pk 	n -= sizeof(*eh);
     90        1.6        pk 	return (n);
     91        1.4   mycroft }
     92        1.4   mycroft 
     93        1.5   thorpej /*
     94        1.5   thorpej  * Get a packet of any Ethernet type, with our address or
     95        1.5   thorpej  * the broadcast address.  Save the Ether type in arg 5.
     96        1.5   thorpej  * NOTE: Caller must leave room for the Ether header.
     97        1.5   thorpej  */
     98        1.6        pk ssize_t
     99        1.5   thorpej readether(d, pkt, len, tleft, etype)
    100        1.4   mycroft 	register struct iodesc *d;
    101        1.4   mycroft 	register void *pkt;
    102        1.4   mycroft 	register size_t len;
    103        1.4   mycroft 	time_t tleft;
    104        1.5   thorpej 	register u_int16_t *etype;
    105        1.4   mycroft {
    106        1.6        pk 	register ssize_t n;
    107        1.4   mycroft 	register struct ether_header *eh;
    108        1.4   mycroft 
    109        1.4   mycroft #ifdef ETHER_DEBUG
    110        1.4   mycroft  	if (debug)
    111        1.8  christos 		printf("readether: called\n");
    112        1.4   mycroft #endif
    113        1.4   mycroft 
    114        1.4   mycroft 	eh = (struct ether_header *)pkt - 1;
    115        1.4   mycroft 	len += sizeof(*eh);
    116        1.4   mycroft 
    117        1.6        pk 	n = netif_get(d, eh, len, tleft);
    118        1.6        pk 	if (n == -1 || n < sizeof(*eh))
    119        1.4   mycroft 		return (-1);
    120        1.5   thorpej 
    121        1.5   thorpej 	/* Validate Ethernet address. */
    122        1.5   thorpej 	if (bcmp(d->myea, eh->ether_dhost, 6) != 0 &&
    123        1.5   thorpej 	    bcmp(bcea, eh->ether_dhost, 6) != 0) {
    124        1.5   thorpej #ifdef ETHER_DEBUG
    125        1.5   thorpej 		if (debug)
    126        1.8  christos 			printf("readether: not ours (ea=%s)\n",
    127        1.7  christos 			    ether_sprintf(eh->ether_dhost));
    128        1.5   thorpej #endif
    129        1.5   thorpej 		return (-1);
    130        1.5   thorpej 	}
    131        1.5   thorpej 	*etype = ntohs(eh->ether_type);
    132        1.4   mycroft 
    133        1.6        pk 	n -= sizeof(*eh);
    134        1.6        pk 	return (n);
    135        1.1    brezak }
    136        1.1    brezak 
    137        1.1    brezak /*
    138        1.1    brezak  * Convert Ethernet address to printable (loggable) representation.
    139        1.1    brezak  */
    140        1.1    brezak static char digits[] = "0123456789abcdef";
    141        1.1    brezak char *
    142        1.1    brezak ether_sprintf(ap)
    143        1.1    brezak         register u_char *ap;
    144        1.1    brezak {
    145       1.12  drochner 	register int i;
    146        1.1    brezak 	static char etherbuf[18];
    147        1.1    brezak 	register char *cp = etherbuf;
    148        1.1    brezak 
    149        1.1    brezak 	for (i = 0; i < 6; i++) {
    150        1.1    brezak 		*cp++ = digits[*ap >> 4];
    151        1.1    brezak 		*cp++ = digits[*ap++ & 0xf];
    152        1.1    brezak 		*cp++ = ':';
    153        1.1    brezak 	}
    154        1.1    brezak 	*--cp = 0;
    155        1.1    brezak 	return (etherbuf);
    156        1.1    brezak }
    157