xref: /minix3/sys/lib/libsa/rarp.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: rarp.c,v 1.32 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: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
4058a2b000SEvgeniy Ivanov  */
4158a2b000SEvgeniy Ivanov #include <sys/param.h>
4258a2b000SEvgeniy Ivanov #include <sys/socket.h>
4358a2b000SEvgeniy Ivanov #include <net/if.h>
4458a2b000SEvgeniy Ivanov #include <net/if_ether.h>
4558a2b000SEvgeniy Ivanov #include <netinet/in.h>
4658a2b000SEvgeniy Ivanov 
4758a2b000SEvgeniy Ivanov #include <netinet/in_systm.h>
4858a2b000SEvgeniy Ivanov 
4958a2b000SEvgeniy Ivanov #ifdef _STANDALONE
5058a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
5158a2b000SEvgeniy Ivanov #else
5258a2b000SEvgeniy Ivanov #include <string.h>
5358a2b000SEvgeniy Ivanov #endif
5458a2b000SEvgeniy Ivanov 
5558a2b000SEvgeniy Ivanov #include "stand.h"
5658a2b000SEvgeniy Ivanov #include "net.h"
5758a2b000SEvgeniy Ivanov 
5858a2b000SEvgeniy Ivanov 
5958a2b000SEvgeniy Ivanov /*
6058a2b000SEvgeniy Ivanov  * Ethernet Address Resolution Protocol.
6158a2b000SEvgeniy Ivanov  *
6258a2b000SEvgeniy Ivanov  * See RFC 826 for protocol description.  Structure below is adapted
6358a2b000SEvgeniy Ivanov  * to resolving internet addresses.  Field names used correspond to
6458a2b000SEvgeniy Ivanov  * RFC 826.
6558a2b000SEvgeniy Ivanov  */
6658a2b000SEvgeniy Ivanov struct ether_arp {
6758a2b000SEvgeniy Ivanov 	struct	 arphdr ea_hdr;			/* fixed-size header */
6858a2b000SEvgeniy Ivanov 	u_int8_t arp_sha[ETHER_ADDR_LEN];	/* sender hardware address */
6958a2b000SEvgeniy Ivanov 	u_int8_t arp_spa[4];			/* sender protocol address */
7058a2b000SEvgeniy Ivanov 	u_int8_t arp_tha[ETHER_ADDR_LEN];	/* target hardware address */
7158a2b000SEvgeniy Ivanov 	u_int8_t arp_tpa[4];			/* target protocol address */
7258a2b000SEvgeniy Ivanov };
7358a2b000SEvgeniy Ivanov #define	arp_hrd	ea_hdr.ar_hrd
7458a2b000SEvgeniy Ivanov #define	arp_pro	ea_hdr.ar_pro
7558a2b000SEvgeniy Ivanov #define	arp_hln	ea_hdr.ar_hln
7658a2b000SEvgeniy Ivanov #define	arp_pln	ea_hdr.ar_pln
7758a2b000SEvgeniy Ivanov #define	arp_op	ea_hdr.ar_op
7858a2b000SEvgeniy Ivanov 
7958a2b000SEvgeniy Ivanov static ssize_t rarpsend(struct iodesc *, void *, size_t);
8058a2b000SEvgeniy Ivanov static ssize_t rarprecv(struct iodesc *, void *, size_t, saseconds_t);
8158a2b000SEvgeniy Ivanov 
8258a2b000SEvgeniy Ivanov /*
8358a2b000SEvgeniy Ivanov  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
8458a2b000SEvgeniy Ivanov  */
8558a2b000SEvgeniy Ivanov int
rarp_getipaddress(int sock)8658a2b000SEvgeniy Ivanov rarp_getipaddress(int sock)
8758a2b000SEvgeniy Ivanov {
8858a2b000SEvgeniy Ivanov 	struct iodesc *d;
8958a2b000SEvgeniy Ivanov 	struct ether_arp *ap;
9058a2b000SEvgeniy Ivanov 	struct {
9158a2b000SEvgeniy Ivanov 		u_char header[ETHERNET_HEADER_SIZE];
9258a2b000SEvgeniy Ivanov 		struct {
9358a2b000SEvgeniy Ivanov 			struct ether_arp arp;
9458a2b000SEvgeniy Ivanov 			u_char pad[18]; 	/* 60 - sizeof(arp) */
9558a2b000SEvgeniy Ivanov 		} data;
9658a2b000SEvgeniy Ivanov 	} wbuf;
9758a2b000SEvgeniy Ivanov 	struct {
9858a2b000SEvgeniy Ivanov 		u_char header[ETHERNET_HEADER_SIZE];
9958a2b000SEvgeniy Ivanov 		struct {
10058a2b000SEvgeniy Ivanov 			struct ether_arp arp;
10158a2b000SEvgeniy Ivanov 			u_char pad[24]; 	/* extra space */
10258a2b000SEvgeniy Ivanov 		} data;
10358a2b000SEvgeniy Ivanov 	} rbuf;
10458a2b000SEvgeniy Ivanov 
10558a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
10658a2b000SEvgeniy Ivanov  	if (debug)
10758a2b000SEvgeniy Ivanov 		printf("rarp: socket=%d\n", sock);
10858a2b000SEvgeniy Ivanov #endif
10958a2b000SEvgeniy Ivanov 	if (!(d = socktodesc(sock))) {
11058a2b000SEvgeniy Ivanov 		printf("rarp: bad socket. %d\n", sock);
11158a2b000SEvgeniy Ivanov 		return -1;
11258a2b000SEvgeniy Ivanov 	}
11358a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
11458a2b000SEvgeniy Ivanov  	if (debug)
11558a2b000SEvgeniy Ivanov 		printf("rarp: d=%lx\n", (u_long)d);
11658a2b000SEvgeniy Ivanov #endif
11758a2b000SEvgeniy Ivanov 
11858a2b000SEvgeniy Ivanov 	(void)memset(&wbuf.data, 0, sizeof(wbuf.data));
11958a2b000SEvgeniy Ivanov 	ap = &wbuf.data.arp;
12058a2b000SEvgeniy Ivanov 	ap->arp_hrd = htons(ARPHRD_ETHER);
12158a2b000SEvgeniy Ivanov 	ap->arp_pro = htons(ETHERTYPE_IP);
12258a2b000SEvgeniy Ivanov 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
12358a2b000SEvgeniy Ivanov 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
12458a2b000SEvgeniy Ivanov 	ap->arp_op = htons(ARPOP_REVREQUEST);
125*0a6a1f1dSLionel Sambuc 	(void)memcpy(ap->arp_sha, d->myea, ETHER_ADDR_LEN);
126*0a6a1f1dSLionel Sambuc 	(void)memcpy(ap->arp_tha, d->myea, ETHER_ADDR_LEN);
12758a2b000SEvgeniy Ivanov 
12858a2b000SEvgeniy Ivanov 	if (sendrecv(d,
12958a2b000SEvgeniy Ivanov 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
13058a2b000SEvgeniy Ivanov 	    rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
13158a2b000SEvgeniy Ivanov 	{
13258a2b000SEvgeniy Ivanov 		printf("No response for RARP request\n");
13358a2b000SEvgeniy Ivanov 		return -1;
13458a2b000SEvgeniy Ivanov 	}
13558a2b000SEvgeniy Ivanov 
13658a2b000SEvgeniy Ivanov 	ap = &rbuf.data.arp;
13758a2b000SEvgeniy Ivanov 	(void)memcpy(&myip, ap->arp_tpa, sizeof(myip));
13858a2b000SEvgeniy Ivanov #if 0
13958a2b000SEvgeniy Ivanov 	/* XXX - Can NOT assume this is our root server! */
14058a2b000SEvgeniy Ivanov 	(void)memcpy(&rootip, ap->arp_spa, sizeof(rootip));
14158a2b000SEvgeniy Ivanov #endif
14258a2b000SEvgeniy Ivanov 
14358a2b000SEvgeniy Ivanov 	/* Compute our "natural" netmask. */
14458a2b000SEvgeniy Ivanov 	if (IN_CLASSA(myip.s_addr))
14558a2b000SEvgeniy Ivanov 		netmask = IN_CLASSA_NET;
14658a2b000SEvgeniy Ivanov 	else if (IN_CLASSB(myip.s_addr))
14758a2b000SEvgeniy Ivanov 		netmask = IN_CLASSB_NET;
14858a2b000SEvgeniy Ivanov 	else
14958a2b000SEvgeniy Ivanov 		netmask = IN_CLASSC_NET;
15058a2b000SEvgeniy Ivanov 
15158a2b000SEvgeniy Ivanov 	d->myip = myip;
15258a2b000SEvgeniy Ivanov 	return 0;
15358a2b000SEvgeniy Ivanov }
15458a2b000SEvgeniy Ivanov 
15558a2b000SEvgeniy Ivanov /*
15658a2b000SEvgeniy Ivanov  * Broadcast a RARP request (i.e. who knows who I am)
15758a2b000SEvgeniy Ivanov  */
15858a2b000SEvgeniy Ivanov static ssize_t
rarpsend(struct iodesc * d,void * pkt,size_t len)15958a2b000SEvgeniy Ivanov rarpsend(struct iodesc *d, void *pkt, size_t len)
16058a2b000SEvgeniy Ivanov {
16158a2b000SEvgeniy Ivanov 
16258a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
16358a2b000SEvgeniy Ivanov  	if (debug)
16458a2b000SEvgeniy Ivanov 		printf("rarpsend: called\n");
16558a2b000SEvgeniy Ivanov #endif
16658a2b000SEvgeniy Ivanov 
16758a2b000SEvgeniy Ivanov 	return sendether(d, pkt, len, bcea, ETHERTYPE_REVARP);
16858a2b000SEvgeniy Ivanov }
16958a2b000SEvgeniy Ivanov 
17058a2b000SEvgeniy Ivanov /*
17158a2b000SEvgeniy Ivanov  * Returns 0 if this is the packet we're waiting for
17258a2b000SEvgeniy Ivanov  * else -1 (and errno == 0)
17358a2b000SEvgeniy Ivanov  */
17458a2b000SEvgeniy Ivanov static ssize_t
rarprecv(struct iodesc * d,void * pkt,size_t len,saseconds_t tleft)17558a2b000SEvgeniy Ivanov rarprecv(struct iodesc *d, void *pkt, size_t len, saseconds_t tleft)
17658a2b000SEvgeniy Ivanov {
17758a2b000SEvgeniy Ivanov 	ssize_t n;
17858a2b000SEvgeniy Ivanov 	struct ether_arp *ap;
17958a2b000SEvgeniy Ivanov 	u_int16_t etype;	/* host order */
18058a2b000SEvgeniy Ivanov 
18158a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
18258a2b000SEvgeniy Ivanov  	if (debug)
18358a2b000SEvgeniy Ivanov 		printf("rarprecv: ");
18458a2b000SEvgeniy Ivanov #endif
18558a2b000SEvgeniy Ivanov 
18658a2b000SEvgeniy Ivanov 	n = readether(d, pkt, len, tleft, &etype);
18758a2b000SEvgeniy Ivanov 	errno = 0;	/* XXX */
18858a2b000SEvgeniy Ivanov 	if (n == -1 || (size_t)n < sizeof(struct ether_arp)) {
18958a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
19058a2b000SEvgeniy Ivanov 		if (debug)
19158a2b000SEvgeniy Ivanov 			printf("bad len=%d\n", (int)n);
19258a2b000SEvgeniy Ivanov #endif
19358a2b000SEvgeniy Ivanov 		return -1;
19458a2b000SEvgeniy Ivanov 	}
19558a2b000SEvgeniy Ivanov 
19658a2b000SEvgeniy Ivanov 	if (etype != ETHERTYPE_REVARP) {
19758a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
19858a2b000SEvgeniy Ivanov 		if (debug)
19958a2b000SEvgeniy Ivanov 			printf("bad type=0x%x\n", etype);
20058a2b000SEvgeniy Ivanov #endif
20158a2b000SEvgeniy Ivanov 		return -1;
20258a2b000SEvgeniy Ivanov 	}
20358a2b000SEvgeniy Ivanov 
20458a2b000SEvgeniy Ivanov 	ap = (struct ether_arp *)pkt;
20558a2b000SEvgeniy Ivanov 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
20658a2b000SEvgeniy Ivanov 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
20758a2b000SEvgeniy Ivanov 	    ap->arp_hln != sizeof(ap->arp_sha) ||
20858a2b000SEvgeniy Ivanov 	    ap->arp_pln != sizeof(ap->arp_spa) )
20958a2b000SEvgeniy Ivanov 	{
21058a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
21158a2b000SEvgeniy Ivanov 		if (debug)
21258a2b000SEvgeniy Ivanov 			printf("bad hrd/pro/hln/pln\n");
21358a2b000SEvgeniy Ivanov #endif
21458a2b000SEvgeniy Ivanov 		return -1;
21558a2b000SEvgeniy Ivanov 	}
21658a2b000SEvgeniy Ivanov 
21758a2b000SEvgeniy Ivanov 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
21858a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
21958a2b000SEvgeniy Ivanov 		if (debug)
22058a2b000SEvgeniy Ivanov 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
22158a2b000SEvgeniy Ivanov #endif
22258a2b000SEvgeniy Ivanov 		return -1;
22358a2b000SEvgeniy Ivanov 	}
22458a2b000SEvgeniy Ivanov 
22558a2b000SEvgeniy Ivanov 	/* Is the reply for our Ethernet address? */
226*0a6a1f1dSLionel Sambuc 	if (memcmp(ap->arp_tha, d->myea, ETHER_ADDR_LEN)) {
22758a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
22858a2b000SEvgeniy Ivanov 		if (debug)
22958a2b000SEvgeniy Ivanov 			printf("unwanted address\n");
23058a2b000SEvgeniy Ivanov #endif
23158a2b000SEvgeniy Ivanov 		return -1;
23258a2b000SEvgeniy Ivanov 	}
23358a2b000SEvgeniy Ivanov 
23458a2b000SEvgeniy Ivanov 	/* We have our answer. */
23558a2b000SEvgeniy Ivanov #ifdef RARP_DEBUG
23658a2b000SEvgeniy Ivanov  	if (debug)
23758a2b000SEvgeniy Ivanov 		printf("got it\n");
23858a2b000SEvgeniy Ivanov #endif
23958a2b000SEvgeniy Ivanov 	return n;
24058a2b000SEvgeniy Ivanov }
241