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