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