xref: /netbsd-src/sys/lib/libsa/rarp.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: rarp.c,v 1.13 1996/10/13 02:29:05 christos 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 <netinet/in.h>
45 
46 #include <netinet/if_ether.h>
47 #include <netinet/in_systm.h>
48 
49 #include <string.h>
50 
51 #include "stand.h"
52 #include "net.h"
53 #include "netif.h"
54 
55 static ssize_t rarpsend __P((struct iodesc *, void *, size_t));
56 static ssize_t rarprecv __P((struct iodesc *, void *, size_t, time_t));
57 
58 /*
59  * Ethernet (Reverse) Address Resolution Protocol (see RFC 903, and 826).
60  */
61 int
62 rarp_getipaddress(sock)
63 	int sock;
64 {
65 	struct iodesc *d;
66 	register struct ether_arp *ap;
67 	struct {
68 		u_char header[ETHER_SIZE];
69 		struct {
70 			struct ether_arp arp;
71 			u_char pad[18]; 	/* 60 - sizeof(arp) */
72 		} data;
73 	} wbuf;
74 	struct {
75 		u_char header[ETHER_SIZE];
76 		struct {
77 			struct ether_arp arp;
78 			u_char pad[24]; 	/* extra space */
79 		} data;
80 	} rbuf;
81 
82 #ifdef RARP_DEBUG
83  	if (debug)
84 		printf("rarp: socket=%d\n", sock);
85 #endif
86 	if (!(d = socktodesc(sock))) {
87 		printf("rarp: bad socket. %d\n", sock);
88 		return (-1);
89 	}
90 #ifdef RARP_DEBUG
91  	if (debug)
92 		printf("rarp: d=%x\n", (u_int)d);
93 #endif
94 
95 	bzero((char*)&wbuf.data, sizeof(wbuf.data));
96 	ap = &wbuf.data.arp;
97 	ap->arp_hrd = htons(ARPHRD_ETHER);
98 	ap->arp_pro = htons(ETHERTYPE_IP);
99 	ap->arp_hln = sizeof(ap->arp_sha); /* hardware address length */
100 	ap->arp_pln = sizeof(ap->arp_spa); /* protocol address length */
101 	ap->arp_op = htons(ARPOP_REVREQUEST);
102 	bcopy(d->myea, ap->arp_sha, 6);
103 	bcopy(d->myea, ap->arp_tha, 6);
104 
105 	if (sendrecv(d,
106 	    rarpsend, &wbuf.data, sizeof(wbuf.data),
107 	    rarprecv, &rbuf.data, sizeof(rbuf.data)) < 0)
108 	{
109 		printf("No response for RARP request\n");
110 		return (-1);
111 	}
112 
113 	ap = &rbuf.data.arp;
114 	bcopy(ap->arp_tpa, (char *)&myip, sizeof(myip));
115 #if 0
116 	/* XXX - Can NOT assume this is our root server! */
117 	bcopy(ap->arp_spa, (char *)&rootip, sizeof(rootip));
118 #endif
119 
120 	/* Compute our "natural" netmask. */
121 	if (IN_CLASSA(myip.s_addr))
122 		netmask = IN_CLASSA_NET;
123 	else if (IN_CLASSB(myip.s_addr))
124 		netmask = IN_CLASSB_NET;
125 	else
126 		netmask = IN_CLASSC_NET;
127 
128 	d->myip = myip;
129 	return (0);
130 }
131 
132 /*
133  * Broadcast a RARP request (i.e. who knows who I am)
134  */
135 static ssize_t
136 rarpsend(d, pkt, len)
137 	register struct iodesc *d;
138 	register void *pkt;
139 	register size_t len;
140 {
141 
142 #ifdef RARP_DEBUG
143  	if (debug)
144 		printf("rarpsend: called\n");
145 #endif
146 
147 	return (sendether(d, pkt, len, bcea, ETHERTYPE_REVARP));
148 }
149 
150 /*
151  * Returns 0 if this is the packet we're waiting for
152  * else -1 (and errno == 0)
153  */
154 static ssize_t
155 rarprecv(d, pkt, len, tleft)
156 	register struct iodesc *d;
157 	register void *pkt;
158 	register size_t len;
159 	time_t tleft;
160 {
161 	register ssize_t n;
162 	register struct ether_arp *ap;
163 	u_int16_t etype;	/* host order */
164 
165 #ifdef RARP_DEBUG
166  	if (debug)
167 		printf("rarprecv: ");
168 #endif
169 
170 	n = readether(d, pkt, len, tleft, &etype);
171 	errno = 0;	/* XXX */
172 	if (n == -1 || n < sizeof(struct ether_arp)) {
173 #ifdef RARP_DEBUG
174 		if (debug)
175 			printf("bad len=%d\n", n);
176 #endif
177 		return (-1);
178 	}
179 
180 	if (etype != ETHERTYPE_REVARP) {
181 #ifdef RARP_DEBUG
182 		if (debug)
183 			printf("bad type=0x%x\n", etype);
184 #endif
185 		return (-1);
186 	}
187 
188 	ap = (struct ether_arp *)pkt;
189 	if (ap->arp_hrd != htons(ARPHRD_ETHER) ||
190 	    ap->arp_pro != htons(ETHERTYPE_IP) ||
191 	    ap->arp_hln != sizeof(ap->arp_sha) ||
192 	    ap->arp_pln != sizeof(ap->arp_spa) )
193 	{
194 #ifdef RARP_DEBUG
195 		if (debug)
196 			printf("bad hrd/pro/hln/pln\n");
197 #endif
198 		return (-1);
199 	}
200 
201 	if (ap->arp_op != htons(ARPOP_REVREPLY)) {
202 #ifdef RARP_DEBUG
203 		if (debug)
204 			printf("bad op=0x%x\n", ntohs(ap->arp_op));
205 #endif
206 		return (-1);
207 	}
208 
209 	/* Is the reply for our Ethernet address? */
210 	if (bcmp(ap->arp_tha, d->myea, 6)) {
211 #ifdef RARP_DEBUG
212 		if (debug)
213 			printf("unwanted address\n");
214 #endif
215 		return (-1);
216 	}
217 
218 	/* We have our answer. */
219 #ifdef RARP_DEBUG
220  	if (debug)
221 		printf("got it\n");
222 #endif
223 	return (n);
224 }
225