xref: /freebsd/stand/libsa/rarp.c (revision 7c43148a974877188a930e4078a164f83da8e652)
1ca987d46SWarner Losh /*	$NetBSD: rarp.c,v 1.16 1997/07/07 15:52:52 drochner Exp $	*/
2ca987d46SWarner Losh 
3ca987d46SWarner Losh /*
4ca987d46SWarner Losh  * Copyright (c) 1992 Regents of the University of California.
5ca987d46SWarner Losh  * All rights reserved.
6ca987d46SWarner Losh  *
7ca987d46SWarner Losh  * This software was developed by the Computer Systems Engineering group
8ca987d46SWarner Losh  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9ca987d46SWarner Losh  * contributed to Berkeley.
10ca987d46SWarner Losh  *
11ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
12ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
13ca987d46SWarner Losh  * are met:
14ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
15ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
16ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
17ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
18ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
19ca987d46SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
20ca987d46SWarner Losh  *    may be used to endorse or promote products derived from this software
21ca987d46SWarner Losh  *    without specific prior written permission.
22ca987d46SWarner Losh  *
23ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca987d46SWarner Losh  * SUCH DAMAGE.
34ca987d46SWarner Losh  */
35ca987d46SWarner Losh 
36ca987d46SWarner Losh #include <sys/param.h>
37ca987d46SWarner Losh #include <sys/socket.h>
38ca987d46SWarner Losh #include <net/if.h>
39ca987d46SWarner Losh #include <netinet/in.h>
40ca987d46SWarner Losh #include <netinet/if_ether.h>
41ca987d46SWarner Losh 
42ca987d46SWarner Losh #include <netinet/in_systm.h>
43ca987d46SWarner Losh 
44ca987d46SWarner Losh #include <string.h>
45ca987d46SWarner Losh 
46ca987d46SWarner Losh #include "stand.h"
47ca987d46SWarner Losh #include "net.h"
48ca987d46SWarner Losh #include "netif.h"
49ca987d46SWarner Losh 
50ca987d46SWarner Losh 
51ca987d46SWarner Losh static ssize_t rarpsend(struct iodesc *, void *, size_t);
52c5b86c3bSKyle Evans static ssize_t rarprecv(struct iodesc *, void **, void **, time_t, void *);
53ca987d46SWarner Losh 
54ca987d46SWarner Losh /*
55ca987d46SWarner Losh  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
56ca987d46SWarner Losh  */
57ca987d46SWarner Losh int
rarp_getipaddress(int sock)58ca987d46SWarner Losh rarp_getipaddress(int sock)
59ca987d46SWarner Losh {
60ca987d46SWarner Losh 	struct iodesc *d;
61ca987d46SWarner Losh 	struct ether_arp *ap;
62ca987d46SWarner Losh 	void *pkt;
63ca987d46SWarner Losh 	struct {
64ca987d46SWarner Losh 		u_char header[ETHER_SIZE];
65ca987d46SWarner Losh 		struct {
66ca987d46SWarner Losh 			struct ether_arp arp;
67ca987d46SWarner Losh 			u_char pad[18]; 	/* 60 - sizeof(arp) */
68ca987d46SWarner Losh 		} data;
69ca987d46SWarner Losh 	} wbuf;
70ca987d46SWarner Losh 
71ca987d46SWarner Losh #ifdef RARP_DEBUG
72ca987d46SWarner Losh  	if (debug)
73ca987d46SWarner Losh 		printf("rarp: socket=%d\n", sock);
74ca987d46SWarner Losh #endif
75ca987d46SWarner Losh 	if (!(d = socktodesc(sock))) {
76ca987d46SWarner Losh 		printf("rarp: bad socket. %d\n", sock);
77ca987d46SWarner Losh 		return (-1);
78ca987d46SWarner Losh 	}
79ca987d46SWarner Losh #ifdef RARP_DEBUG
80ca987d46SWarner Losh  	if (debug)
81*ee0d06faSMichał Grzelak 		printf("rarp: d=%lx\n", (long)d);
82ca987d46SWarner Losh #endif
83ca987d46SWarner Losh 
84ca987d46SWarner Losh 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
85ca987d46SWarner Losh 	ap = &wbuf.data.arp;
86ca987d46SWarner Losh 	ap->arp_hrd = htons(ARPHRD_ETHER);
87ca987d46SWarner Losh 	ap->arp_pro = htons(ETHERTYPE_IP);
88ca987d46SWarner Losh 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
89ca987d46SWarner Losh 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
90ca987d46SWarner Losh 	ap->arp_op = htons(ARPOP_REVREQUEST);
91ca987d46SWarner Losh 	bcopy(d->myea, ap->arp_sha, 6);
92ca987d46SWarner Losh 	bcopy(d->myea, ap->arp_tha, 6);
93ca987d46SWarner Losh 	pkt = NULL;
94ca987d46SWarner Losh 
95ca987d46SWarner Losh 	if (sendrecv(d,
96ca987d46SWarner Losh 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
97c5b86c3bSKyle Evans 	    rarprecv, &pkt, (void *)&ap, NULL) < 0) {
98ca987d46SWarner Losh 		printf("No response for RARP request\n");
99ca987d46SWarner Losh 		return (-1);
100ca987d46SWarner Losh 	}
101ca987d46SWarner Losh 
102ca987d46SWarner Losh 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
103ca987d46SWarner Losh #if 0
104ca987d46SWarner Losh 	/* XXX - Can NOT assume this is our root server! */
105ca987d46SWarner Losh 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
106ca987d46SWarner Losh #endif
107ca987d46SWarner Losh 	free(pkt);
108ca987d46SWarner Losh 
109ca987d46SWarner Losh 	/* Compute our "natural" netmask. */
110ca987d46SWarner Losh 	if (IN_CLASSA(myip.s_addr))
111ca987d46SWarner Losh 		netmask = IN_CLASSA_NET;
112ca987d46SWarner Losh 	else if (IN_CLASSB(myip.s_addr))
113ca987d46SWarner Losh 		netmask = IN_CLASSB_NET;
114ca987d46SWarner Losh 	else
115ca987d46SWarner Losh 		netmask = IN_CLASSC_NET;
116ca987d46SWarner Losh 
117ca987d46SWarner Losh 	d->myip = myip;
118ca987d46SWarner Losh 	return (0);
119ca987d46SWarner Losh }
120ca987d46SWarner Losh 
121ca987d46SWarner Losh /*
122ca987d46SWarner Losh  * Broadcast a RARP request (i.e. who knows who I am)
123ca987d46SWarner Losh  */
124ca987d46SWarner Losh static ssize_t
rarpsend(struct iodesc * d,void * pkt,size_t len)125ca987d46SWarner Losh rarpsend(struct iodesc *d, void *pkt, size_t len)
126ca987d46SWarner Losh {
127ca987d46SWarner Losh 
128ca987d46SWarner Losh #ifdef RARP_DEBUG
129ca987d46SWarner Losh  	if (debug)
130ca987d46SWarner Losh 		printf("rarpsend: called\n");
131ca987d46SWarner Losh #endif
132ca987d46SWarner Losh 
133ca987d46SWarner Losh 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
134ca987d46SWarner Losh }
135ca987d46SWarner Losh 
136ca987d46SWarner Losh /*
137ca987d46SWarner Losh  * Returns 0 if this is the packet we're waiting for
138ca987d46SWarner Losh  * else -1 (and errno == 0)
139ca987d46SWarner Losh  */
140ca987d46SWarner Losh static ssize_t
rarprecv(struct iodesc * d,void ** pkt,void ** payload,time_t tleft,void * extra)141c5b86c3bSKyle Evans rarprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft,
142c5b86c3bSKyle Evans     void *extra)
143ca987d46SWarner Losh {
144ca987d46SWarner Losh 	ssize_t n;
145ca987d46SWarner Losh 	struct ether_arp *ap;
146ca987d46SWarner Losh 	void *ptr = NULL;
147ca987d46SWarner Losh 	uint16_t etype;		/* host order */
148ca987d46SWarner Losh 
149ca987d46SWarner Losh #ifdef RARP_DEBUG
150ca987d46SWarner Losh  	if (debug)
151ca987d46SWarner Losh 		printf("rarprecv: ");
152ca987d46SWarner Losh #endif
153ca987d46SWarner Losh 
154ca987d46SWarner Losh 	n = readether(d, &ptr, (void **)&ap, tleft, &etype);
155ca987d46SWarner Losh 	errno = 0;	/* XXX */
156ca987d46SWarner Losh 	if (n == -1 || n < sizeof(struct ether_arp)) {
157ca987d46SWarner Losh #ifdef RARP_DEBUG
158ca987d46SWarner Losh 		if (debug)
1591ed79161SMichał Grzelak 			printf("bad len=%zd\n", n);
160ca987d46SWarner Losh #endif
161ca987d46SWarner Losh 		free(ptr);
162ca987d46SWarner Losh 		return (-1);
163ca987d46SWarner Losh 	}
164ca987d46SWarner Losh 
165ca987d46SWarner Losh 	if (etype != ETHERTYPE_REVARP) {
166ca987d46SWarner Losh #ifdef RARP_DEBUG
167ca987d46SWarner Losh 		if (debug)
168ca987d46SWarner Losh 			printf("bad type=0x%x\n", etype);
169ca987d46SWarner Losh #endif
170ca987d46SWarner Losh 		free(ptr);
171ca987d46SWarner Losh 		return (-1);
172ca987d46SWarner Losh 	}
173ca987d46SWarner Losh 
174ca987d46SWarner Losh 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
175ca987d46SWarner Losh 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
176ca987d46SWarner Losh 	    ap->arp_hln != sizeof(ap->arp_sha) ||
177ca987d46SWarner Losh 	    ap->arp_pln != sizeof(ap->arp_spa) )
178ca987d46SWarner Losh 	{
179ca987d46SWarner Losh #ifdef RARP_DEBUG
180ca987d46SWarner Losh 		if (debug)
181ca987d46SWarner Losh 			printf("bad hrd/pro/hln/pln\n");
182ca987d46SWarner Losh #endif
183ca987d46SWarner Losh 		free(ptr);
184ca987d46SWarner Losh 		return (-1);
185ca987d46SWarner Losh 	}
186ca987d46SWarner Losh 
187ca987d46SWarner Losh 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
188ca987d46SWarner Losh #ifdef RARP_DEBUG
189ca987d46SWarner Losh 		if (debug)
190ca987d46SWarner Losh 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
191ca987d46SWarner Losh #endif
192ca987d46SWarner Losh 		free(ptr);
193ca987d46SWarner Losh 		return (-1);
194ca987d46SWarner Losh 	}
195ca987d46SWarner Losh 
196ca987d46SWarner Losh 	/* Is the reply for our Ethernet address? */
197ca987d46SWarner Losh 	if (bcmp(ap->arp_tha, d->myea, 6)) {
198ca987d46SWarner Losh #ifdef RARP_DEBUG
199ca987d46SWarner Losh 		if (debug)
200ca987d46SWarner Losh 			printf("unwanted address\n");
201ca987d46SWarner Losh #endif
202ca987d46SWarner Losh 		free(ptr);
203ca987d46SWarner Losh 		return (-1);
204ca987d46SWarner Losh 	}
205ca987d46SWarner Losh 
206ca987d46SWarner Losh 	/* We have our answer. */
207ca987d46SWarner Losh #ifdef RARP_DEBUG
208ca987d46SWarner Losh  	if (debug)
209ca987d46SWarner Losh 		printf("got it\n");
210ca987d46SWarner Losh #endif
211ca987d46SWarner Losh 	*pkt = ptr;
212ca987d46SWarner Losh 	*payload = ap;
213ca987d46SWarner Losh 	return (n);
214ca987d46SWarner Losh }
215