xref: /minix3/sys/lib/libsa/ether.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ether.c,v 1.23 2014/03/29 14:30:16 jakllsch Exp $	*/
258a2b000SEvgeniy Ivanov 
358a2b000SEvgeniy Ivanov /*
458a2b000SEvgeniy Ivanov  * Copyright (c) 1992 Regents of the University of California.
558a2b000SEvgeniy Ivanov  * All rights reserved.
658a2b000SEvgeniy Ivanov  *
758a2b000SEvgeniy Ivanov  * This software was developed by the Computer Systems Engineering group
858a2b000SEvgeniy Ivanov  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
958a2b000SEvgeniy Ivanov  * contributed to Berkeley.
1058a2b000SEvgeniy Ivanov  *
1158a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
1258a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
1358a2b000SEvgeniy Ivanov  * are met:
1458a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
1558a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
1658a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
1758a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
1858a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
1958a2b000SEvgeniy Ivanov  * 3. All advertising materials mentioning features or use of this software
2058a2b000SEvgeniy Ivanov  *    must display the following acknowledgement:
2158a2b000SEvgeniy Ivanov  *	This product includes software developed by the University of
2258a2b000SEvgeniy Ivanov  *	California, Lawrence Berkeley Laboratory and its contributors.
2358a2b000SEvgeniy Ivanov  * 4. Neither the name of the University nor the names of its contributors
2458a2b000SEvgeniy Ivanov  *    may be used to endorse or promote products derived from this software
2558a2b000SEvgeniy Ivanov  *    without specific prior written permission.
2658a2b000SEvgeniy Ivanov  *
2758a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2858a2b000SEvgeniy Ivanov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2958a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3058a2b000SEvgeniy Ivanov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3158a2b000SEvgeniy Ivanov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3258a2b000SEvgeniy Ivanov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3358a2b000SEvgeniy Ivanov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3458a2b000SEvgeniy Ivanov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3558a2b000SEvgeniy Ivanov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3658a2b000SEvgeniy Ivanov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3758a2b000SEvgeniy Ivanov  * SUCH DAMAGE.
3858a2b000SEvgeniy Ivanov  *
3958a2b000SEvgeniy Ivanov  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
4058a2b000SEvgeniy Ivanov  */
4158a2b000SEvgeniy Ivanov 
4258a2b000SEvgeniy Ivanov #include <sys/param.h>
4358a2b000SEvgeniy Ivanov #include <sys/socket.h>
4458a2b000SEvgeniy Ivanov #ifdef _STANDALONE
4558a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
4658a2b000SEvgeniy Ivanov #else
4758a2b000SEvgeniy Ivanov #include <string.h>
4858a2b000SEvgeniy Ivanov #endif
4958a2b000SEvgeniy Ivanov 
5058a2b000SEvgeniy Ivanov #include <net/if.h>
5158a2b000SEvgeniy Ivanov #include <net/if_ether.h>
5258a2b000SEvgeniy Ivanov 
5358a2b000SEvgeniy Ivanov #include <netinet/in.h>
5458a2b000SEvgeniy Ivanov #include <netinet/in_systm.h>
5558a2b000SEvgeniy Ivanov 
5658a2b000SEvgeniy Ivanov #include "stand.h"
5758a2b000SEvgeniy Ivanov #include "net.h"
5858a2b000SEvgeniy Ivanov 
5958a2b000SEvgeniy Ivanov /* Caller must leave room for ethernet header in front!! */
6058a2b000SEvgeniy Ivanov ssize_t
sendether(struct iodesc * d,void * pkt,size_t len,u_char * dea,int etype)6158a2b000SEvgeniy Ivanov sendether(struct iodesc *d, void *pkt, size_t len, u_char *dea, int etype)
6258a2b000SEvgeniy Ivanov {
6358a2b000SEvgeniy Ivanov 	ssize_t n;
6458a2b000SEvgeniy Ivanov 	struct ether_header *eh;
6558a2b000SEvgeniy Ivanov 
6658a2b000SEvgeniy Ivanov #ifdef ETHER_DEBUG
6758a2b000SEvgeniy Ivanov  	if (debug)
6858a2b000SEvgeniy Ivanov 		printf("sendether: called\n");
6958a2b000SEvgeniy Ivanov #endif
7058a2b000SEvgeniy Ivanov 
7158a2b000SEvgeniy Ivanov 	eh = (struct ether_header *)pkt - 1;
7258a2b000SEvgeniy Ivanov 	len += sizeof(*eh);
7358a2b000SEvgeniy Ivanov 
7458a2b000SEvgeniy Ivanov 	MACPY(d->myea, eh->ether_shost);		/* by byte */
7558a2b000SEvgeniy Ivanov 	MACPY(dea, eh->ether_dhost);			/* by byte */
7658a2b000SEvgeniy Ivanov 	eh->ether_type = htons(etype);
7758a2b000SEvgeniy Ivanov 
7858a2b000SEvgeniy Ivanov 	n = netif_put(d, eh, len);
7958a2b000SEvgeniy Ivanov 	if (n == -1 || (size_t)n < sizeof(*eh))
8058a2b000SEvgeniy Ivanov 		return -1;
8158a2b000SEvgeniy Ivanov 
8258a2b000SEvgeniy Ivanov 	n -= sizeof(*eh);
8358a2b000SEvgeniy Ivanov 	return n;
8458a2b000SEvgeniy Ivanov }
8558a2b000SEvgeniy Ivanov 
8658a2b000SEvgeniy Ivanov /*
8758a2b000SEvgeniy Ivanov  * Get a packet of any Ethernet type, with our address or
8858a2b000SEvgeniy Ivanov  * the broadcast address.  Save the Ether type in arg 5.
8958a2b000SEvgeniy Ivanov  * NOTE: Caller must leave room for the Ether header.
9058a2b000SEvgeniy Ivanov  */
9158a2b000SEvgeniy Ivanov ssize_t
readether(struct iodesc * d,void * pkt,size_t len,saseconds_t tleft,u_int16_t * etype)9258a2b000SEvgeniy Ivanov readether(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft,
9358a2b000SEvgeniy Ivanov 	u_int16_t *etype)
9458a2b000SEvgeniy Ivanov {
9558a2b000SEvgeniy Ivanov 	ssize_t n;
9658a2b000SEvgeniy Ivanov 	struct ether_header *eh;
9758a2b000SEvgeniy Ivanov 
9858a2b000SEvgeniy Ivanov #ifdef ETHER_DEBUG
9958a2b000SEvgeniy Ivanov  	if (debug)
10058a2b000SEvgeniy Ivanov 		printf("readether: called\n");
10158a2b000SEvgeniy Ivanov #endif
10258a2b000SEvgeniy Ivanov 
10358a2b000SEvgeniy Ivanov 	eh = (struct ether_header *)pkt - 1;
10458a2b000SEvgeniy Ivanov 	len += sizeof(*eh);
10558a2b000SEvgeniy Ivanov 
10658a2b000SEvgeniy Ivanov 	n = netif_get(d, eh, len, tleft);
10758a2b000SEvgeniy Ivanov 	if (n == -1 || (size_t)n < sizeof(*eh))
10858a2b000SEvgeniy Ivanov 		return -1;
10958a2b000SEvgeniy Ivanov 
11058a2b000SEvgeniy Ivanov 	/* Validate Ethernet address. */
111*0a6a1f1dSLionel Sambuc 	if (memcmp(d->myea, eh->ether_dhost, ETHER_ADDR_LEN) != 0 &&
112*0a6a1f1dSLionel Sambuc 	    memcmp(bcea, eh->ether_dhost, ETHER_ADDR_LEN) != 0) {
11358a2b000SEvgeniy Ivanov #ifdef ETHER_DEBUG
11458a2b000SEvgeniy Ivanov 		if (debug)
11558a2b000SEvgeniy Ivanov 			printf("readether: not ours (ea=%s)\n",
11658a2b000SEvgeniy Ivanov 			    ether_sprintf(eh->ether_dhost));
11758a2b000SEvgeniy Ivanov #endif
11858a2b000SEvgeniy Ivanov 		return -1;
11958a2b000SEvgeniy Ivanov 	}
12058a2b000SEvgeniy Ivanov 	*etype = ntohs(eh->ether_type);
12158a2b000SEvgeniy Ivanov 
12258a2b000SEvgeniy Ivanov 	n -= sizeof(*eh);
12358a2b000SEvgeniy Ivanov 	return n;
12458a2b000SEvgeniy Ivanov }
125