xref: /netbsd-src/sys/lib/libsa/ether.c (revision 10497fd285fb21c25f76e74beeb53b30864bc3f3)
1*10497fd2Schristos /*	$NetBSD: ether.c,v 1.24 2019/03/31 20:08:45 christos Exp $	*/
26668f51cScgd 
3e03842d0Sbrezak /*
4e03842d0Sbrezak  * Copyright (c) 1992 Regents of the University of California.
5e03842d0Sbrezak  * All rights reserved.
6e03842d0Sbrezak  *
7e03842d0Sbrezak  * This software was developed by the Computer Systems Engineering group
8e03842d0Sbrezak  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9e03842d0Sbrezak  * contributed to Berkeley.
10e03842d0Sbrezak  *
11e03842d0Sbrezak  * Redistribution and use in source and binary forms, with or without
12e03842d0Sbrezak  * modification, are permitted provided that the following conditions
13e03842d0Sbrezak  * are met:
14e03842d0Sbrezak  * 1. Redistributions of source code must retain the above copyright
15e03842d0Sbrezak  *    notice, this list of conditions and the following disclaimer.
16e03842d0Sbrezak  * 2. Redistributions in binary form must reproduce the above copyright
17e03842d0Sbrezak  *    notice, this list of conditions and the following disclaimer in the
18e03842d0Sbrezak  *    documentation and/or other materials provided with the distribution.
19e03842d0Sbrezak  * 3. All advertising materials mentioning features or use of this software
20e03842d0Sbrezak  *    must display the following acknowledgement:
21e03842d0Sbrezak  *	This product includes software developed by the University of
22e03842d0Sbrezak  *	California, Lawrence Berkeley Laboratory and its contributors.
23e03842d0Sbrezak  * 4. Neither the name of the University nor the names of its contributors
24e03842d0Sbrezak  *    may be used to endorse or promote products derived from this software
25e03842d0Sbrezak  *    without specific prior written permission.
26e03842d0Sbrezak  *
27e03842d0Sbrezak  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28e03842d0Sbrezak  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29e03842d0Sbrezak  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30e03842d0Sbrezak  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31e03842d0Sbrezak  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32e03842d0Sbrezak  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33e03842d0Sbrezak  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34e03842d0Sbrezak  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35e03842d0Sbrezak  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36e03842d0Sbrezak  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37e03842d0Sbrezak  * SUCH DAMAGE.
38e03842d0Sbrezak  *
396668f51cScgd  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
40e03842d0Sbrezak  */
41e03842d0Sbrezak 
42e03842d0Sbrezak #include <sys/param.h>
43e03842d0Sbrezak #include <sys/socket.h>
4436ff5d93Sthorpej #ifdef _STANDALONE
4536ff5d93Sthorpej #include <lib/libkern/libkern.h>
4636ff5d93Sthorpej #else
4736ff5d93Sthorpej #include <string.h>
4836ff5d93Sthorpej #endif
49e03842d0Sbrezak 
50e03842d0Sbrezak #include <net/if.h>
5164cd693aSdrochner #include <net/if_ether.h>
52e03842d0Sbrezak 
53e03842d0Sbrezak #include <netinet/in.h>
54e03842d0Sbrezak #include <netinet/in_systm.h>
55e03842d0Sbrezak 
56e03842d0Sbrezak #include "stand.h"
57e03842d0Sbrezak #include "net.h"
58e03842d0Sbrezak 
59e03842d0Sbrezak /* Caller must leave room for ethernet header in front!! */
6084c517c1Spk ssize_t
sendether(struct iodesc * d,void * pkt,size_t len,u_char * dea,int etype)611c038e68Sisaki sendether(struct iodesc *d, void *pkt, size_t len, u_char *dea, int etype)
62e03842d0Sbrezak {
631279e67bSaugustss 	ssize_t n;
641279e67bSaugustss 	struct ether_header *eh;
65e03842d0Sbrezak 
66e03842d0Sbrezak #ifdef ETHER_DEBUG
67e03842d0Sbrezak  	if (debug)
68*10497fd2Schristos 		printf("%s: called\n", __func__);
69e03842d0Sbrezak #endif
70258efc53Smycroft 
71258efc53Smycroft 	eh = (struct ether_header *)pkt - 1;
72258efc53Smycroft 	len += sizeof(*eh);
73e03842d0Sbrezak 
74e03842d0Sbrezak 	MACPY(d->myea, eh->ether_shost);		/* by byte */
75e03842d0Sbrezak 	MACPY(dea, eh->ether_dhost);			/* by byte */
76e03842d0Sbrezak 	eh->ether_type = htons(etype);
77258efc53Smycroft 
7884c517c1Spk 	n = netif_put(d, eh, len);
7960ae17c7Sfvdl 	if (n == -1 || (size_t)n < sizeof(*eh))
801c038e68Sisaki 		return -1;
81258efc53Smycroft 
82*10497fd2Schristos 	n -= (ssize_t)sizeof(*eh);
831c038e68Sisaki 	return n;
84258efc53Smycroft }
85258efc53Smycroft 
86eb50acd9Sthorpej /*
87eb50acd9Sthorpej  * Get a packet of any Ethernet type, with our address or
88eb50acd9Sthorpej  * the broadcast address.  Save the Ether type in arg 5.
89eb50acd9Sthorpej  * NOTE: Caller must leave room for the Ether header.
90eb50acd9Sthorpej  */
9184c517c1Spk ssize_t
readether(struct iodesc * d,void * pkt,size_t len,saseconds_t tleft,u_int16_t * etype)9269cf32a7Stsutsui readether(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft,
931c038e68Sisaki 	u_int16_t *etype)
94258efc53Smycroft {
951279e67bSaugustss 	ssize_t n;
961279e67bSaugustss 	struct ether_header *eh;
97258efc53Smycroft 
98258efc53Smycroft #ifdef ETHER_DEBUG
99258efc53Smycroft  	if (debug)
100*10497fd2Schristos 		printf("%s: called\n", __func__);
101258efc53Smycroft #endif
102258efc53Smycroft 
103258efc53Smycroft 	eh = (struct ether_header *)pkt - 1;
104258efc53Smycroft 	len += sizeof(*eh);
105258efc53Smycroft 
10684c517c1Spk 	n = netif_get(d, eh, len, tleft);
10760ae17c7Sfvdl 	if (n == -1 || (size_t)n < sizeof(*eh))
1081c038e68Sisaki 		return -1;
109258efc53Smycroft 
110eb50acd9Sthorpej 	/* Validate Ethernet address. */
111edd6a404Sjakllsch 	if (memcmp(d->myea, eh->ether_dhost, ETHER_ADDR_LEN) != 0 &&
112edd6a404Sjakllsch 	    memcmp(bcea, eh->ether_dhost, ETHER_ADDR_LEN) != 0) {
113eb50acd9Sthorpej #ifdef ETHER_DEBUG
114eb50acd9Sthorpej 		if (debug)
115*10497fd2Schristos 			printf("%s: not ours (ea=%s)\n", __func__,
116eb50acd9Sthorpej 			    ether_sprintf(eh->ether_dhost));
117eb50acd9Sthorpej #endif
1181c038e68Sisaki 		return -1;
119eb50acd9Sthorpej 	}
120eb50acd9Sthorpej 	*etype = ntohs(eh->ether_type);
121eb50acd9Sthorpej 
122*10497fd2Schristos 	n -= (ssize_t)sizeof(*eh);
1231c038e68Sisaki 	return n;
124e03842d0Sbrezak }
125