138451Smsmith/* $NetBSD: ether.c,v 1.11 1997/07/07 15:52:50 drochner Exp $ */ 238451Smsmith 338451Smsmith/* 438451Smsmith * Copyright (c) 1992 Regents of the University of California. 538451Smsmith * All rights reserved. 638451Smsmith * 738451Smsmith * This software was developed by the Computer Systems Engineering group 838451Smsmith * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 938451Smsmith * contributed to Berkeley. 1038451Smsmith * 1138451Smsmith * Redistribution and use in source and binary forms, with or without 1238451Smsmith * modification, are permitted provided that the following conditions 1338451Smsmith * are met: 1438451Smsmith * 1. Redistributions of source code must retain the above copyright 1538451Smsmith * notice, this list of conditions and the following disclaimer. 1638451Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1738451Smsmith * notice, this list of conditions and the following disclaimer in the 1838451Smsmith * documentation and/or other materials provided with the distribution. 1938451Smsmith * 4. Neither the name of the University nor the names of its contributors 2038451Smsmith * may be used to endorse or promote products derived from this software 2138451Smsmith * without specific prior written permission. 2238451Smsmith * 2338451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2438451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2538451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2638451Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2738451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2838451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2938451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3038451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3138451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3238451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3338451Smsmith * SUCH DAMAGE. 3438451Smsmith * 3538451Smsmith * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp (LBL) 3638451Smsmith */ 3738451Smsmith 3884221Sdillon#include <sys/cdefs.h> 3984221Sdillon__FBSDID("$FreeBSD$"); 4084221Sdillon 4138451Smsmith#include <sys/param.h> 4238451Smsmith#include <sys/socket.h> 4338451Smsmith#include <string.h> 4438451Smsmith 4538451Smsmith#include <net/if.h> 4638451Smsmith#include <netinet/in.h> 4738451Smsmith#include <netinet/if_ether.h> 4838451Smsmith#include <netinet/in_systm.h> 4938451Smsmith#include <netinet/ip.h> 5038451Smsmith 5138451Smsmith#include "stand.h" 5238451Smsmith#include "net.h" 5338451Smsmith#include "netif.h" 5438451Smsmith 5538451Smsmith/* Caller must leave room for ethernet header in front!! */ 5638451Smsmithssize_t 5738451Smsmithsendether(d, pkt, len, dea, etype) 5838451Smsmith struct iodesc *d; 5938451Smsmith void *pkt; 6038451Smsmith size_t len; 6138451Smsmith u_char *dea; 6238451Smsmith int etype; 6338451Smsmith{ 6492913Sobrien ssize_t n; 6592913Sobrien struct ether_header *eh; 6638451Smsmith 6738451Smsmith#ifdef ETHER_DEBUG 6838451Smsmith if (debug) 6938451Smsmith printf("sendether: called\n"); 7038451Smsmith#endif 7138451Smsmith 7238451Smsmith eh = (struct ether_header *)pkt - 1; 7338451Smsmith len += sizeof(*eh); 7438451Smsmith 7538451Smsmith MACPY(d->myea, eh->ether_shost); /* by byte */ 7638451Smsmith MACPY(dea, eh->ether_dhost); /* by byte */ 7738451Smsmith eh->ether_type = htons(etype); 7838451Smsmith 7938451Smsmith n = netif_put(d, eh, len); 8038451Smsmith if (n == -1 || n < sizeof(*eh)) 8138451Smsmith return (-1); 8238451Smsmith 8338451Smsmith n -= sizeof(*eh); 8438451Smsmith return (n); 8538451Smsmith} 8638451Smsmith 8738451Smsmith/* 8838451Smsmith * Get a packet of any Ethernet type, with our address or 8938451Smsmith * the broadcast address. Save the Ether type in arg 5. 9038451Smsmith * NOTE: Caller must leave room for the Ether header. 9138451Smsmith */ 9238451Smsmithssize_t 9338451Smsmithreadether(d, pkt, len, tleft, etype) 9492913Sobrien struct iodesc *d; 9592913Sobrien void *pkt; 9692913Sobrien size_t len; 9738451Smsmith time_t tleft; 9892913Sobrien u_int16_t *etype; 9938451Smsmith{ 10092913Sobrien ssize_t n; 10192913Sobrien struct ether_header *eh; 10238451Smsmith 10338451Smsmith#ifdef ETHER_DEBUG 10438451Smsmith if (debug) 10538451Smsmith printf("readether: called\n"); 10638451Smsmith#endif 10738451Smsmith 10838451Smsmith eh = (struct ether_header *)pkt - 1; 10938451Smsmith len += sizeof(*eh); 11038451Smsmith 11138451Smsmith n = netif_get(d, eh, len, tleft); 11238451Smsmith if (n == -1 || n < sizeof(*eh)) 11338451Smsmith return (-1); 11438451Smsmith 11538451Smsmith /* Validate Ethernet address. */ 11638451Smsmith if (bcmp(d->myea, eh->ether_dhost, 6) != 0 && 11738451Smsmith bcmp(bcea, eh->ether_dhost, 6) != 0) { 11838451Smsmith#ifdef ETHER_DEBUG 11938451Smsmith if (debug) 12038451Smsmith printf("readether: not ours (ea=%s)\n", 12138451Smsmith ether_sprintf(eh->ether_dhost)); 12238451Smsmith#endif 12338451Smsmith return (-1); 12438451Smsmith } 12538451Smsmith *etype = ntohs(eh->ether_type); 12638451Smsmith 12738451Smsmith n -= sizeof(*eh); 12838451Smsmith return (n); 12938451Smsmith} 13038451Smsmith 13138451Smsmith/* 13238451Smsmith * Convert Ethernet address to printable (loggable) representation. 13338451Smsmith */ 13438451Smsmithstatic char digits[] = "0123456789abcdef"; 13538451Smsmithchar * 13638451Smsmithether_sprintf(ap) 13792913Sobrien u_char *ap; 13838451Smsmith{ 13992913Sobrien int i; 14038451Smsmith static char etherbuf[18]; 14192913Sobrien char *cp = etherbuf; 14238451Smsmith 14338451Smsmith for (i = 0; i < 6; i++) { 14438451Smsmith *cp++ = digits[*ap >> 4]; 14538451Smsmith *cp++ = digits[*ap++ & 0xf]; 14638451Smsmith *cp++ = ':'; 14738451Smsmith } 14838451Smsmith *--cp = 0; 14938451Smsmith return (etherbuf); 15038451Smsmith} 151