xref: /titanic_51/usr/src/boot/lib/libstand/rarp.c (revision 7b2a1233f92b2b3cb858f2fdb69378a9ab0a42f1)
14a5d661aSToomas Soome /*	$NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner Exp $	*/
24a5d661aSToomas Soome 
34a5d661aSToomas Soome /*
44a5d661aSToomas Soome  * Copyright (c) 1992 Regents of the University of California.
54a5d661aSToomas Soome  * All rights reserved.
64a5d661aSToomas Soome  *
74a5d661aSToomas Soome  * This software was developed by the Computer Systems Engineering group
84a5d661aSToomas Soome  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
94a5d661aSToomas Soome  * contributed to Berkeley.
104a5d661aSToomas Soome  *
114a5d661aSToomas Soome  * Redistribution and use in source and binary forms, with or without
124a5d661aSToomas Soome  * modification, are permitted provided that the following conditions
134a5d661aSToomas Soome  * are met:
144a5d661aSToomas Soome  * 1. Redistributions of source code must retain the above copyright
154a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer.
164a5d661aSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
174a5d661aSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
184a5d661aSToomas Soome  *    documentation and/or other materials provided with the distribution.
194a5d661aSToomas Soome  * 4. Neither the name of the University nor the names of its contributors
204a5d661aSToomas Soome  *    may be used to endorse or promote products derived from this software
214a5d661aSToomas Soome  *    without specific prior written permission.
224a5d661aSToomas Soome  *
234a5d661aSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244a5d661aSToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254a5d661aSToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264a5d661aSToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274a5d661aSToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284a5d661aSToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294a5d661aSToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304a5d661aSToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314a5d661aSToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324a5d661aSToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334a5d661aSToomas Soome  * SUCH DAMAGE.
344a5d661aSToomas Soome  *
354a5d661aSToomas Soome  * @(#) Header: arp.c,v 1.5 93/07/15 05:52:26 leres Exp  (LBL)
364a5d661aSToomas Soome  */
374a5d661aSToomas Soome 
384a5d661aSToomas Soome #include <sys/cdefs.h>
394a5d661aSToomas Soome 
404a5d661aSToomas Soome #include <sys/param.h>
414a5d661aSToomas Soome #include <sys/socket.h>
424a5d661aSToomas Soome #include <net/if.h>
434a5d661aSToomas Soome #include <netinet/in.h>
444a5d661aSToomas Soome #include <netinet/if_ether.h>
454a5d661aSToomas Soome 
464a5d661aSToomas Soome #include <netinet/in_systm.h>
474a5d661aSToomas Soome 
484a5d661aSToomas Soome #include <string.h>
494a5d661aSToomas Soome 
504a5d661aSToomas Soome #include "stand.h"
514a5d661aSToomas Soome #include "net.h"
524a5d661aSToomas Soome #include "netif.h"
534a5d661aSToomas Soome 
544a5d661aSToomas Soome 
554a5d661aSToomas Soome static ssize_t rarpsend(struct iodesc *, void *, size_t);
56*7b2a1233SToomas Soome static ssize_t rarprecv(struct iodesc *, void **, void **, time_t);
574a5d661aSToomas Soome 
584a5d661aSToomas Soome /*
594a5d661aSToomas Soome  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
604a5d661aSToomas Soome  */
614a5d661aSToomas Soome int
62*7b2a1233SToomas Soome rarp_getipaddress(int sock)
634a5d661aSToomas Soome {
644a5d661aSToomas Soome 	struct iodesc *d;
654a5d661aSToomas Soome 	struct ether_arp *ap;
66*7b2a1233SToomas Soome 	void *pkt;
674a5d661aSToomas Soome 	struct {
684a5d661aSToomas Soome 		u_char header[ETHER_SIZE];
694a5d661aSToomas Soome 		struct {
704a5d661aSToomas Soome 			struct ether_arp arp;
714a5d661aSToomas Soome 			u_char pad[18]; 	/* 60 - sizeof(arp) */
724a5d661aSToomas Soome 		} data;
734a5d661aSToomas Soome 	} wbuf;
744a5d661aSToomas Soome 
754a5d661aSToomas Soome #ifdef RARP_DEBUG
764a5d661aSToomas Soome  	if (debug)
774a5d661aSToomas Soome 		printf("rarp: socket=%d\n", sock);
784a5d661aSToomas Soome #endif
794a5d661aSToomas Soome 	if (!(d = socktodesc(sock))) {
804a5d661aSToomas Soome 		printf("rarp: bad socket. %d\n", sock);
814a5d661aSToomas Soome 		return (-1);
824a5d661aSToomas Soome 	}
834a5d661aSToomas Soome #ifdef RARP_DEBUG
844a5d661aSToomas Soome  	if (debug)
854a5d661aSToomas Soome 		printf("rarp: d=%x\n", (u_int)d);
864a5d661aSToomas Soome #endif
874a5d661aSToomas Soome 
884a5d661aSToomas Soome 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
894a5d661aSToomas Soome 	ap = &wbuf.data.arp;
904a5d661aSToomas Soome 	ap->arp_hrd = htons(ARPHRD_ETHER);
914a5d661aSToomas Soome 	ap->arp_pro = htons(ETHERTYPE_IP);
924a5d661aSToomas Soome 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
934a5d661aSToomas Soome 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
944a5d661aSToomas Soome 	ap->arp_op = htons(ARPOP_REVREQUEST);
954a5d661aSToomas Soome 	bcopy(d->myea, ap->arp_sha, 6);
964a5d661aSToomas Soome 	bcopy(d->myea, ap->arp_tha, 6);
97*7b2a1233SToomas Soome 	pkt = NULL;
984a5d661aSToomas Soome 
994a5d661aSToomas Soome 	if (sendrecv(d,
1004a5d661aSToomas Soome 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
101*7b2a1233SToomas Soome 	    rarprecv, &pkt, (void *)&ap) < 0) {
1024a5d661aSToomas Soome 		printf("No response for RARP request\n");
1034a5d661aSToomas Soome 		return (-1);
1044a5d661aSToomas Soome 	}
1054a5d661aSToomas Soome 
1064a5d661aSToomas Soome 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
1074a5d661aSToomas Soome #if 0
1084a5d661aSToomas Soome 	/* XXX - Can NOT assume this is our root server! */
1094a5d661aSToomas Soome 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
1104a5d661aSToomas Soome #endif
111*7b2a1233SToomas Soome 	free(pkt);
1124a5d661aSToomas Soome 
1134a5d661aSToomas Soome 	/* Compute our "natural" netmask. */
1144a5d661aSToomas Soome 	if (IN_CLASSA(myip.s_addr))
1154a5d661aSToomas Soome 		netmask = IN_CLASSA_NET;
1164a5d661aSToomas Soome 	else if (IN_CLASSB(myip.s_addr))
1174a5d661aSToomas Soome 		netmask = IN_CLASSB_NET;
1184a5d661aSToomas Soome 	else
1194a5d661aSToomas Soome 		netmask = IN_CLASSC_NET;
1204a5d661aSToomas Soome 
1214a5d661aSToomas Soome 	d->myip = myip;
1224a5d661aSToomas Soome 	return (0);
1234a5d661aSToomas Soome }
1244a5d661aSToomas Soome 
1254a5d661aSToomas Soome /*
1264a5d661aSToomas Soome  * Broadcast a RARP request (i.e. who knows who I am)
1274a5d661aSToomas Soome  */
1284a5d661aSToomas Soome static ssize_t
129*7b2a1233SToomas Soome rarpsend(struct iodesc *d, void *pkt, size_t len)
1304a5d661aSToomas Soome {
1314a5d661aSToomas Soome 
1324a5d661aSToomas Soome #ifdef RARP_DEBUG
1334a5d661aSToomas Soome  	if (debug)
1344a5d661aSToomas Soome 		printf("rarpsend: called\n");
1354a5d661aSToomas Soome #endif
1364a5d661aSToomas Soome 
1374a5d661aSToomas Soome 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
1384a5d661aSToomas Soome }
1394a5d661aSToomas Soome 
1404a5d661aSToomas Soome /*
1414a5d661aSToomas Soome  * Returns 0 if this is the packet we're waiting for
1424a5d661aSToomas Soome  * else -1 (and errno == 0)
1434a5d661aSToomas Soome  */
1444a5d661aSToomas Soome static ssize_t
145*7b2a1233SToomas Soome rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft)
1464a5d661aSToomas Soome {
1474a5d661aSToomas Soome 	ssize_t n;
1484a5d661aSToomas Soome 	struct ether_arp *ap;
149*7b2a1233SToomas Soome 	void *ptr = NULL;
150*7b2a1233SToomas Soome 	uint16_t etype;		/* host order */
1514a5d661aSToomas Soome 
1524a5d661aSToomas Soome #ifdef RARP_DEBUG
1534a5d661aSToomas Soome  	if (debug)
1544a5d661aSToomas Soome 		printf("rarprecv: ");
1554a5d661aSToomas Soome #endif
1564a5d661aSToomas Soome 
157*7b2a1233SToomas Soome 	n = readether(d, &ptr, (void **)&ap, tleft, &etype);
1584a5d661aSToomas Soome 	errno = 0;	/* XXX */
1594a5d661aSToomas Soome 	if (n == -1 || n < sizeof(struct ether_arp)) {
1604a5d661aSToomas Soome #ifdef RARP_DEBUG
1614a5d661aSToomas Soome 		if (debug)
1624a5d661aSToomas Soome 			printf("bad len=%d\n", n);
1634a5d661aSToomas Soome #endif
164*7b2a1233SToomas Soome 		free(ptr);
1654a5d661aSToomas Soome 		return (-1);
1664a5d661aSToomas Soome 	}
1674a5d661aSToomas Soome 
1684a5d661aSToomas Soome 	if (etype != ETHERTYPE_REVARP) {
1694a5d661aSToomas Soome #ifdef RARP_DEBUG
1704a5d661aSToomas Soome 		if (debug)
1714a5d661aSToomas Soome 			printf("bad type=0x%x\n", etype);
1724a5d661aSToomas Soome #endif
173*7b2a1233SToomas Soome 		free(ptr);
1744a5d661aSToomas Soome 		return (-1);
1754a5d661aSToomas Soome 	}
1764a5d661aSToomas Soome 
1774a5d661aSToomas Soome 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
1784a5d661aSToomas Soome 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
1794a5d661aSToomas Soome 	    ap->arp_hln != sizeof(ap->arp_sha) ||
1804a5d661aSToomas Soome 	    ap->arp_pln != sizeof(ap->arp_spa) )
1814a5d661aSToomas Soome 	{
1824a5d661aSToomas Soome #ifdef RARP_DEBUG
1834a5d661aSToomas Soome 		if (debug)
1844a5d661aSToomas Soome 			printf("bad hrd/pro/hln/pln\n");
1854a5d661aSToomas Soome #endif
186*7b2a1233SToomas Soome 		free(ptr);
1874a5d661aSToomas Soome 		return (-1);
1884a5d661aSToomas Soome 	}
1894a5d661aSToomas Soome 
1904a5d661aSToomas Soome 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
1914a5d661aSToomas Soome #ifdef RARP_DEBUG
1924a5d661aSToomas Soome 		if (debug)
1934a5d661aSToomas Soome 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
1944a5d661aSToomas Soome #endif
195*7b2a1233SToomas Soome 		free(ptr);
1964a5d661aSToomas Soome 		return (-1);
1974a5d661aSToomas Soome 	}
1984a5d661aSToomas Soome 
1994a5d661aSToomas Soome 	/* Is the reply for our Ethernet address? */
2004a5d661aSToomas Soome 	if (bcmp(ap->arp_tha, d->myea, 6)) {
2014a5d661aSToomas Soome #ifdef RARP_DEBUG
2024a5d661aSToomas Soome 		if (debug)
2034a5d661aSToomas Soome 			printf("unwanted address\n");
2044a5d661aSToomas Soome #endif
205*7b2a1233SToomas Soome 		free(ptr);
2064a5d661aSToomas Soome 		return (-1);
2074a5d661aSToomas Soome 	}
2084a5d661aSToomas Soome 
2094a5d661aSToomas Soome 	/* We have our answer. */
2104a5d661aSToomas Soome #ifdef RARP_DEBUG
2114a5d661aSToomas Soome  	if (debug)
2124a5d661aSToomas Soome 		printf("got it\n");
2134a5d661aSToomas Soome #endif
214*7b2a1233SToomas Soome 	*pkt = ptr;
215*7b2a1233SToomas Soome 	*payload = ap;
2164a5d661aSToomas Soome 	return (n);
2174a5d661aSToomas Soome }
218