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